代码太长了,改了好久还是WA,样例过了。。。改的太难受了,不改了,先存起来等心情好再重写一遍吧
下面是错误代码
#include<iostream>
#include<algorithm>
using namespace std;
#define mid ((l+r)/2)
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define lowbit(i) (i&-i)
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define mem(arr,a) memset(arr,a,sizeof(arr))
#define L (rt<<1)
#define R (rt<<1|1)
#define INF 0f3f3f3f3f
#define N 100000+5
#define INF 0x3f3f3f3f
#define LL long long int
#define sd1(a) scanf("%d",&a)
#define sd2(a,b) scanf("%d%d",&a,&b)
#define sd3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define pd1(a) printf("%d",a)
#define pd2(a,b) printf("%d%d",a,b)
/******************/
struct node{
int l, r, m;
int lmin, rmax;
int ll, rr, mm;
int num;
};
node s[N << 2];
int lazy[N << 2];
void pushup(int rt, int m){
s[rt].m = 0;
s[rt].mm = 0;
s[rt].l = s[L].l;
s[rt].r = s[R].r;
s[rt].rmax = s[R].rmax;
s[rt].lmin = s[L].lmin;
s[rt].num = s[L].num + s[R].num;
s[rt].ll = s[L].ll;
s[rt].rr = s[R].rr;
if (s[rt].l == (m - (m >> 1)))
s[rt].l += s[R].l;
if (s[rt].r == (m >> 1))
s[rt].r += s[L].r;
if (s[rt].ll == (m - (m >> 1)))
s[rt].ll += s[R].ll;
if (s[rt].rr == (m >> 1))
s[rt].rr += s[L].rr;
if (s[L].rmax&&s[R].lmin)
s[rt].m = s[L].r + s[R].l;
if (!s[L].rmax&&!s[R].lmin)
s[rt].mm = s[L].rr + s[R].ll;
s[rt].m = max(s[rt].m, max(s[rt].l, s[rt].r));
s[rt].mm = max(s[rt].mm, max(s[rt].ll, s[rt].rr));
}
void pushdown(int rt, int op, int m){
if (lazy[rt] != -1)
{
lazy[L] = lazy[R] = lazy[rt];
if (lazy[rt] >= 0 && lazy[rt] <= 1){
s[L].l = s[L].r = s[L].m = s[L].num = lazy[L] * (m - (m >> 1));
s[L].lmin = s[L].rmax = lazy[L];
s[L].ll = s[L].rr = s[L].mm = (lazy[L] + 1) % 2;
s[R].l = s[R].r = s[R].m = s[R].num = lazy[R] * (m >> 1);
s[R].lmin = s[R].rmax = lazy[R];
s[R].ll = s[R].rr = s[R].mm = (lazy[R] + 1) % 2;
}
else if (lazy[rt] == 2){
s[L].lmin = (s[L].lmin + 1) % 2;
s[L].rmax = (s[L].rmax + 1) % 2;
s[L].num = (m-(m>>1)) - s[L].num;
swap(s[L].l, s[L].ll);
swap(s[L].r, s[L].rr);
swap(s[L].m, s[L].mm);
s[R].num = (m >> 1) - s[R].num;
s[R].lmin = (s[R].lmin + 1) % 2;
s[R].rmax = (s[R].rmax + 1) % 2;
swap(s[R].l, s[R].ll);
swap(s[R].r, s[R].rr);
swap(s[R].m, s[R].mm);
}
lazy[rt] = -1;
}
}
void build(int l, int r, int rt){
lazy[rt] = -1;
if (l == r){
sd1(s[rt].num);
s[rt].l = s[rt].r = s[rt].m = s[rt].lmin = s[rt].rmax = s[rt].num;
s[rt].ll = s[rt].rr = s[rt].mm = (s[rt].num + 1) % 2;
return;
}
build(lson);
build(rson);
pushup(rt, r - l + 1);
}
void update(int a, int b, int op, int l, int r, int rt){
if (a <= l&&r <= b){
lazy[rt] = op;
if (op <= 1){
s[rt].l = s[rt].r = s[rt].m = s[rt].num = op*(r - l + 1);
s[rt].lmin = s[rt].rmax = op;
s[rt].ll = s[rt].rr = s[rt].mm = (op + 1) % 2 * (r - l + 1);
}
else{
s[rt].lmin = (s[rt].lmin + 1) % 2;
s[rt].rmax = (s[rt].rmax + 1) % 2;
s[rt].num = r - l + 1 - s[rt].num;
swap(s[rt].l, s[rt].ll);
swap(s[rt].r, s[rt].rr);
swap(s[rt].m, s[rt].mm);
}
return;
}
pushdown(rt, op, r - l + 1);
if (a <= mid)update(a, b, op, lson);
if (b>mid)update(a, b, op, rson);
pushup(rt, r - l + 1);
}
int query(int a, int b, int op, int l, int r, int rt){
if (op == 3){
if (a <= l&&r <= b)return s[rt].num;
pushdown(rt, op, r - l + 1);
int res = 0;
if (a <= mid)res += query(a, b, op, lson);
if (mid<b)res += query(a, b, op, rson);
return res;
}
else if (op == 4){
if (a <= l&&r <= b)return s[rt].m;
pushdown(rt, op, r - l + 1);
int res = 0;
if (b <= mid)return query(a, b, op, lson);
else if (a>mid)return query(a, b, op, rson);
else{
int v = 0;
int v1 = query(a, b, op, lson);
int v2 = query(a, b, op, rson);
if (s[L].rmax&&s[R].lmin){
v = min(s[L].r, mid - max(a, l) + 1) + min(s[R].l, min(b, r) - mid);
}
return max(v, max(v1, v2));
}
}
}
int main(){
int t, n, m;
cin >> t;
while (t--){
cin >> n >> m;
build(1, n, 1);
while (m--){
int op, a, b;
cin >> op >> a >> b;
if (op >= 0 && op <= 2){
update(a + 1, b + 1, op, 1, n, 1);
}
else {
cout << query(a + 1, b + 1, op, 1, n, 1) << endl;
}
}
}
}