Description
lxhgww最近收到了一个01序列,序列里面包含了n个数,这些数要么是0,要么是1,现在对于这个序列有五种变换操作和询问操作: 0 a b 把[a, b]区间内的所有数全变成0 1 a b 把[a, b]区间内的所有数全变成1 2 a b 把[a,b]区间内的所有数全部取反,也就是说把所有的0变成1,把所有的1变成0 3 a b 询问[a, b]区间内总共有多少个1 4 a b 询问[a, b]区间内最多有多少个连续的1 对于每一种询问操作,lxhgww都需要给出回答,聪明的程序员们,你们能帮助他吗?
Input
输入数据第一行包括2个数,n和m,分别表示序列的长度和操作数目 第二行包括n个数,表示序列的初始状态 接下来m行,每行3个数,op, a, b,(0 < = op < = 4,0 < = a < = b)
Output
对于每一个询问操作,输出一行,包括1个数,表示其对应的答案
Sample Input
10 10
0 0 0 1 1 0 1 0 1 1
1 0 2
3 0 5
2 2 2
4 0 4
0 3 6
2 3 7
4 2 8
1 0 5
0 5 6
3 3 9
0 0 0 1 1 0 1 0 1 1
1 0 2
3 0 5
2 2 2
4 0 4
0 3 6
2 3 7
4 2 8
1 0 5
0 5 6
3 3 9
Sample Output
5
2
6
5
2
6
5
HINT
对于30%的数据,1<=n, m<=1000 对于100%的数据,1< = n, m < = 100000
Source
wa。。。这个神题调试了我一晚上。
维护区间...貌似都有区间可加性..线段树!
维护区间$1$的个数,从左端点开始的最长的1/0的个数,从右端点开始最长的1/0的个数。
区间中最长一段的1/0.
然后这题坑的就是标记的下传。
我们发现区间修改可以覆盖区间翻转,就是我们在$change$和$pushdown$的时候,如果这个节点被新修改,就抹去它的翻转标记。
然后$opt=4$的查询不好处理,我们要传下去一个结构体,里面存和线段树节点一样的东西,然后边递归边修改。
#include <iostream> #include <cstdio> using namespace std; #define reg register inline char gc() { static const int BS = 1 << 22; static unsigned char buf[BS], *st, *ed; if (st == ed) ed = buf + fread(st = buf, BS, 1, stdin); return st == ed ? EOF : *st++; } #define gc getchar inline int read() { int res=0;char ch=gc();bool fu=0; while(!isdigit(ch))fu|=(ch=='-'),ch=gc(); while(isdigit(ch))res=(res<<3)+(res<<1)+(ch^48),ch=gc(); return fu?-res:res; } #define N 100005 int n, m; #define ls o << 1 #define rs o << 1 | 1 int a[N]; int tr[N<<2], L1[N<<2], R1[N<<2], L0[N<<2], R0[N<<2], mx1[N<<2], mx0[N<<2]; int rev[N<<2], lzy[N<<2]; inline void pushup(int o, int l, int r) { int mid = l + r >> 1; tr[o] = tr[ls] + tr[rs]; L1[o] = L1[ls] + (L1[ls] == mid - l + 1 ? L1[rs] : 0); L0[o] = L0[ls] + (L0[ls] == mid - l + 1 ? L0[rs] : 0); R1[o] = R1[rs] + (R1[rs] == r - mid ? R1[ls] : 0); R0[o] = R0[rs] + (R0[rs] == r - mid ? R0[ls] : 0); mx1[o] = max(mx1[ls], max(mx1[rs], R1[ls] + L1[rs])); mx0[o] = max(mx0[ls], max(mx0[rs], R0[ls] + L0[rs])); } //lzy : 0 means none , 1 means turn to 1, 2 means turn to 0 //rev : 1 means reverse, 0 means none //lzy first ans rev second void pushdown(int o, int l, int r) { int mid = l + r >> 1; if (lzy[o] == 1) { tr[ls] = mid - l + 1, tr[rs] = r - mid; L1[ls] = R1[ls] = mx1[ls] = mid - l + 1; L1[rs] = R1[rs] = mx1[rs] = r - mid; L0[ls] = R0[ls] = mx0[ls] = 0; L0[rs] = R0[rs] = mx0[rs] = 0; lzy[o] = 0; lzy[ls] = lzy[rs] = 1; rev[ls] = rev[rs] = 0; } else if (lzy[o] == 2) { tr[ls] = tr[rs] = 0; L1[ls] = R1[ls] = mx1[ls] = 0; L1[rs] = R1[rs] = mx1[rs] = 0; L0[ls] = R0[ls] = mx0[ls] = mid - l + 1; L0[rs] = R0[rs] = mx0[rs] = r - mid; lzy[o] = 0; lzy[ls] = lzy[rs] = 2; rev[ls] = rev[rs] = 0; } if (rev[o]) { tr[ls] = mid - l + 1 - tr[ls]; tr[rs] = r - mid - tr[rs]; swap(L1[ls], L0[ls]); swap(L1[rs], L0[rs]); swap(R1[ls], R0[ls]); swap(R1[rs], R0[rs]); swap(mx1[ls], mx0[ls]); swap(mx1[rs], mx0[rs]); rev[o] = 0; rev[ls] ^= 1, rev[rs] ^= 1; } } void Build(int l, int r, int o) { if (l == r) { if (a[l] == 1) L1[o] = R1[o] = mx1[o] = tr[o] = 1; else L0[o] = R0[o] = mx0[o] = 1; return ; } int mid = l + r >> 1; Build(l, mid, ls); Build(mid + 1, r, rs); pushup(o, l, r); } void change(int l, int r, int o, int ql, int qr, int opt) { if (l >= ql and r <= qr) { if (opt == 0) { tr[o] = 0; L1[o] = R1[o] = mx1[o] = 0; L0[o] = R0[o] = mx0[o] = r - l + 1; lzy[o] = 2, rev[o] = 0; } if (opt == 1) { tr[o] = r - l + 1; L0[o] = R0[o] = mx0[o] = 0; L1[o] = R1[o] = mx1[o] = r - l + 1; lzy[o] = 1, rev[o] = 0; } if (opt == 2) { tr[o] = r - l + 1 - tr[o]; swap(L1[o], L0[o]); swap(R1[o], R0[o]); swap(mx1[o], mx0[o]); rev[o] ^= 1; } return ; } pushdown(o, l, r); int mid = l + r >> 1; if (ql <= mid) change(l, mid, ls, ql, qr, opt); if (qr > mid) change(mid + 1, r, rs, ql, qr, opt); pushup(o, l, r); } int query1(int l, int r, int o, int ql, int qr) { if (l >= ql and r <= qr) return tr[o]; int res = 0, mid = l + r >> 1; pushdown(o, l, r); if (ql <= mid) res += query1(l, mid, ls, ql, qr); if (qr > mid) res += query1(mid + 1, r, rs, ql, qr); return res; } struct node { int tr, l1, r1, mx; }; node query2(int l, int r, int o, int ql, int qr) { // printf("%d %d %d %d %d\n", l, r ,o, ql, qr); if (l >= ql and r <= qr) return (node){tr[o], L1[o], R1[o], mx1[o]}; pushdown(o, l, r); int mid = l + r >> 1; if (qr <= mid) return query2(l, mid, ls, ql, qr); if (ql > mid) return query2(mid + 1, r, rs, ql, qr); node r1, r2, res; r1 = query2(l, mid, ls, ql, qr), r2 = query2(mid + 1, r, rs, ql, qr); res.tr = r1.tr + r2.tr; res.mx = max(r1.mx, max(r2.mx, r1.r1 + r2.l1)); res.l1 = r1.l1 + (r1.l1 == mid - l + 1 ? r2.l1 : 0); res.r1 = r2.r1 + (r2.r1 == r - mid ? r1.r1 : 0); return res; } int main() { n = read(), m = read(); for (reg int i = 1 ; i <= n ; i ++) a[i] = read(); Build(1, n, 1); while(m--) { int opt = read(), x = read() + 1, y = read() + 1; if (opt == 0) change(1, n, 1, x, y, 0); if (opt == 1) change(1, n, 1, x, y, 1); if (opt == 2) change(1, n, 1, x, y, 2); if (opt == 3) printf("%d\n", query2(1, n, 1, x, y).tr); if (opt == 4) printf("%d\n", query2(1, n, 1, x, y).mx); // printf("sum = %d\n", tr[1]); } return 0; }