_Goodnight @ 2022-01-01 17:44:43
今天把这题搞完再AFO了
#include <bits/stdc++.h>
using namespace std;
#define lk k<<1
#define rk k<<1|1
#define mlr l+r>>1
typedef long long ll;
const int N = 1e5 + 5;
int a[N], num[4 * N], add[4 * N];
struct node {
int l, mid, r;
}mx[4 * N], mx0[4 * N];//储存最长1,最长0
void swap(int& a, int& b) { a = a + b; b = a - b; a = a - b; }
void pushup(int k, int l, int r) {
/*
mx[k].l = mx[lk].l; mx[k].r = mx[rk].r;
mx[k].mid = max(mx[lk].r + mx[rk].l, max(mx[lk].mid, mx[rk].mid));
mx0[k].l = mx0[lk].l; mx0[k].r = mx0[rk].r;
mx0[k].mid = max(mx0[lk].r + mx0[rk].l, max(mx0[lk].mid, mx0[rk].mid));
return;
*/
//但是这部分是错的。
}
void modify(int k, int l, int r, int v) {
//pushup(k, l, r);
if (v == -1) { num[k] = 0; mx[k].l = mx[k].mid = mx[k].r = 0; mx0[k].l = mx0[k].mid = mx0[k].r = (r - l + 1);
}
if (v == 1) { num[k] = (r - l + 1); mx[k].l = mx[k].mid = mx[k].r = (r - l + 1); mx0[k].l = mx0[k].mid = mx0[k].r = 0;
}
if (v == 2) {
num[k] = (r - l + 1) - num[k];
swap(mx[k].l, mx0[k].l);
swap(mx[k].mid, mx0[k].mid);
swap(mx[k].r, mx0[k].r);
}
add[k] = v;
}
void pushdown(int k, int l, int r, int mid) {
if (add[k] != 0) {
modify(lk, l, mid, add[k]);
modify(rk, mid + 1, r, add[k]);
add[k] = 0;
}
}
void build(int k, int l, int r) {
if (l == r) {
num[k] = a[l];
mx[k].l = mx[k].r = mx[k].mid = a[l];
mx0[k].l = mx0[k].r = mx0[k].mid = (1-a[l]);
return;
}
int mid = mlr;
build(lk, l, mid); build(rk, mid + 1, r);
num[k] = num[lk] + num[rk];
pushup(k, l, r);
}
void update(int k, int l, int r, int x, int y, int v) {
if (l > y || r < x) {
return;
}//-1->0 1->1 2->fanzhuan
if (l >= x && r <= y) {
modify(k, l, r, v);
return;
}
int mid = mlr;
pushdown(k, l, r, mid);
update(lk, l, mid, x, y, v); update(rk, mid + 1, r, x, y, v);
num[k] = num[lk] + num[rk];
pushup(k, l, r);
}
ll query(int k,int l,int r,int x,int y){
if (l > y || r < x)return 0;
if (l >= x && r <= y) {
return num[k];
}
int mid = mlr;
pushdown(k, l, r, mid);
return query(lk, l, mid, x, y) + query(rk, mid + 1, r, x, y);
}
node querymx(int k, int l, int r, int x, int y) {
if (l > y || r < x)return {0,0,0};
if (l >= x && r <= y) {
return mx[k];
}
int mid = mlr;
pushdown(k, l, r, mid);
node t1=querymx(lk, l, mid, x, y),t2=querymx(rk, mid + 1, r, x, y);
node tmp;
tmp.l = t1.l, tmp.r = t2.r;
tmp.mid = max(t1.r + t2.l, max(t1.mid, t2.mid));
return tmp;
}
int n, m,l,r,opt;
node t;
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
build(1, 1, n);
while (m--) {
scanf("%d%d%d", &opt, &l, &r);
l++, r++;
switch (opt){
case 0:
update(1, 1, n, l, r, -1);
break;
case 1:
update(1, 1, n, l, r, 1);
break;
case 2:
update(1, 1, n, l, r, 2);
break;
case 3:
printf("%d\n", query(1, 1, n, l, r));
break;
case 4:
t = querymx(1, 1, n, l, r);
printf("%d\n",max(t.mid,max(t.l,t.r)));
break;
default:
break;
}
}
}
by zwx2007 @ 2022-01-01 18:34:59
就是0的个数是否为区间长度
by _Goodnight @ 2022-01-01 18:35:54
@zwx2007 区间全部为0的话,sum1就是0了吧
by zwx2007 @ 2022-01-01 18:37:30
@_Goodnight 是的
这里的 sum0/1 是统计个数用的
by __zzy__ @ 2022-01-01 18:38:29
也可以不维护,就是用区间长度-sum1就行
by zwx2007 @ 2022-01-01 18:39:03
甚至sum1都不用
by __zzy__ @ 2022-01-01 18:40:24
@zwx2007 ??那你怎么合并两个区间
by zwx2007 @ 2022-01-01 18:42:06
@zzy 一个充当两个,你可以看看我前面发的,分别为
by _Goodnight @ 2022-01-01 18:44:09
void pushup(int k, int l, int r) {
int mid = mlr, llen = mid - l + 1, rlen = r - mid;
mx[k].l = (num[lk] == llen) ? mx[rk].l + num[lk] : mx[lk].l;
mx[k].r = (num[rk] == rlen) ? mx[lk].r + num[rk] : mx[rk].r;
mx0[k].l = (num[lk] == 0) ? (mx0[rk].l + (llen - num[lk])) : mx0[lk].l;
mx0[k].r = (num[rk] == 0) ? (mx0[lk].r + (rlen - num[rk])):mx0[rk].r;
mx[k].mid = max(mx[lk].r + mx[rk].l, max(mx[lk].mid, mx[rk].mid));
mx0[k].mid = max(mx0[lk].r + mx0[rk].l, max(mx0[lk].mid, mx0[rk].mid));
}
by zwx2007 @ 2022-01-01 18:44:16
@zzy 当我没说
by zwx2007 @ 2022-01-01 18:47:14
@_Goodnight 现在这样貌似就可以了吧