Run ID 作者 问题 语言 测评结果 分数 时间 内存 代码长度 提交时间
33456 _ [SCOI2010]序列操作 C++ 通过 100 143 MS 6508 KB 2151 2021-12-06 13:51:37

Tests(10/10):


#include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; struct Node { int l, r; mutable int val; Node(int _l = 0, int _r = 0, int _v = 0): l(_l), r(_r), val(_v) {} }; bool operator<(Node lhs, Node rhs) { return lhs.l < rhs.l; } struct KedoriTree { set<Node> nodes; KedoriTree() {} set<Node>::iterator split(int pos) { set<Node>::iterator tg(nodes.lower_bound(Node(pos))); if (tg != nodes.end() && tg->l == pos) return tg; --tg; if (pos >= tg->r) return nodes.end(); int l(tg->l), r(tg->r), v(tg->val); nodes.erase(tg); nodes.insert(Node(l, pos, v)); return nodes.insert(Node(pos, r, v)).first; } void addnode(int l, int r, int v) { nodes.insert(Node(l, r, v)); } void assign(int l, int r, int v) { set<Node>::iterator rp(split(r)), lp(split(l)); nodes.erase(lp, rp); nodes.insert(Node(l, r, v)); } void flip(int l, int r) { set<Node>::iterator rp(split(r)), lp(split(l)); while (lp != rp) lp->val ^= 1, ++lp; } int count(int l, int r) { set<Node>::iterator rp(split(r)), lp(split(l)); int res(0); while (lp != rp) { if (lp->val) res += lp->r - lp->l; ++lp; } return res; } int cntd(int l, int r) { set<Node>::iterator rp(split(r)), lp(split(l)); int res(0), cur(0); while (lp != rp) { if (lp->val) cur += lp->r - lp->l; else res = max(res, cur), cur = 0; ++lp; } return max(res, cur); } }; int read() { int x(0); char c(getchar()); while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); return x; } int main() { int N(read()), M(read()); KedoriTree tree; for (int i(0), x(0); i != N; ++i) { x = read(); tree.addnode(i, i + 1, x); } for (int op, a, b; M--; ) { op = read(), a = read(), b = read() + 1; switch (op) { case 0: case 1: // assign 0/1 tree.assign(a, b, op); break; case 2: tree.flip(a, b); break; case 3: printf("%d\n", tree.count(a, b)); break; case 4: printf("%d\n", tree.cntd(a, b)); break; } } return 0; }


测评信息: