比较麻烦的一道题,各种操作
线段树中存储的信息有,覆盖标记,异或标记,0和1的从左到右连续长度,从右到左连续长度,区间最大连续长度
/*
ID: sdj22251
PROG: subset
LANG: C++
*/
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
#define MAXN 100005
#define eps 1e-11
#define L(x) x<<1
#define R(x) x<<1|1
using namespace std;
int a[MAXN];
struct node
{
int left, right, mid;
int Cover, Xor, cnt;
int lmx, rmx, mx;
int lmx0, rmx0, mx0;
}tree[4 * MAXN];
void fun_xor(int C)
{
tree[C].cnt = tree[C].right - tree[C].left + 1 - tree[C].cnt;
swap(tree[C].lmx, tree[C].lmx0);
swap(tree[C].rmx, tree[C].rmx0);
swap(tree[C].mx, tree[C].mx0);
if(tree[C].Cover != -1) tree[C].Cover ^= 1;
else tree[C].Xor ^= 1;
}
void fun_cover(int p, int C)
{
tree[C].Cover = p;
tree[C].Xor = 0;
tree[C].cnt = p ? tree[C].right - tree[C].left + 1 : 0;
tree[C].lmx = tree[C].rmx = tree[C].mx = p ? tree[C].right - tree[C].left + 1 : 0;
tree[C].lmx0 = tree[C].rmx0 = tree[C].mx0 = p ? 0 : tree[C].right - tree[C].left + 1;
}
void up(int C)
{
tree[C].cnt = tree[L(C)].cnt + tree[R(C)].cnt;
tree[C].lmx = tree[L(C)].lmx;
tree[C].rmx = tree[R(C)].rmx;
if(tree[L(C)].lmx == tree[L(C)].right - tree[L(C)].left + 1) tree[C].lmx += tree[R(C)].lmx;
if(tree[R(C)].rmx == tree[R(C)].right - tree[R(C)].left + 1) tree[C].rmx += tree[L(C)].rmx;
tree[C].mx = max(tree[L(C)].rmx + tree[R(C)].lmx, max(tree[L(C)].mx, tree[R(C)].mx));
tree[C].lmx0 = tree[L(C)].lmx0;
tree[C].rmx0 = tree[R(C)].rmx0;
if(tree[L(C)].lmx0 == tree[L(C)].right - tree[L(C)].left + 1) tree[C].lmx0 += tree[R(C)].lmx0;
if(tree[R(C)].rmx0 == tree[R(C)].right - tree[R(C)].left + 1) tree[C].rmx0 += tree[L(C)].rmx0;
tree[C].mx0 = max(tree[L(C)].rmx0 + tree[R(C)].lmx0, max(tree[L(C)].mx0, tree[R(C)].mx0));
}
void down(int C)
{
if(tree[C].Cover != -1)
{
fun_cover(tree[C].Cover, L(C));
fun_cover(tree[C].Cover, R(C));
tree[C].Cover = -1;
}
if(tree[C].Xor)
{
fun_xor(L(C));
fun_xor(R(C));
tree[C].Xor = 0;
}
}
void make_tree(int s, int e, int C)
{
tree[C].left = s;
tree[C].right = e;
tree[C].mid = (s + e) >> 1;
tree[C].Cover = -1;
tree[C].Xor = 0;
if(s == e)
{
tree[C].cnt = a[s] ? 1 : 0;
tree[C].lmx = tree[C].rmx = tree[C].mx = a[s] ? 1 : 0;
tree[C].lmx0 = tree[C].rmx0 = tree[C].mx0 = a[s] ? 0 : 1;
return;
}
make_tree(s, tree[C].mid, L(C));
make_tree(tree[C].mid + 1, e, R(C));
up(C);
}
int query(int s, int e, int op, int C)
{
if(tree[C].left >= s && tree[C].right <= e)
{
if(op == 3) return tree[C].cnt;
else if(op == 4) return tree[C].mx;
}
down(C);
if(op == 3)
{
int t = 0;
if(tree[C].mid >= s) t += query(s, e, op, L(C));
if(tree[C].mid < e) t += query(s, e, op, R(C));
return t;
}
else
{
int t = 0;
if(tree[C].mid >= s) t = max(t, query(s, e, op, L(C)));
if(tree[C].mid < e) t = max(t, query(s, e, op, R(C)));
t = max(t, min(tree[L(C)].rmx, tree[C].mid - s + 1) + min(tree[R(C)].lmx, e - tree[C].mid));
return t;
}
}
void update(int s, int e, int op, int C)
{
if(tree[C].left >= s && tree[C].right <= e)
{
if(op < 2)
fun_cover(op, C);
else if(op == 2)
fun_xor(C);
return;
}
down(C);
if(tree[C].mid >= s) update(s, e, op, L(C));
if(tree[C].mid < e) update(s, e, op, R(C));
up(C);
}
int main()
{
int op, x, y;
int T, n, m;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
make_tree(1, n, 1);
while(m--)
{
scanf("%d%d%d", &op, &x, &y);
x++; y++;
if(op <= 2) update(x, y, op, 1);
else printf("%d\n", query(x, y, op, 1));
}
}
return 0;
}