Sequence operation
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4362 Accepted Submission(s): 1258
Problem Description
lxhgww got a sequence contains n characters which are all '0's or '1's.
We have five operations here:
Change operations:
0 a b change all characters into '0's in [a , b]
1 a b change all characters into '1's in [a , b]
2 a b change all '0's into '1's and change all '1's into '0's in [a, b]
Output operations:
3 a b output the number of '1's in [a, b]
4 a b output the length of the longest continuous '1' string in [a , b]
Input
T(T<=10) in the first line is the case number.
Each case has two integers in the first line: n and m (1 <= n , m <= 100000).
The next line contains n characters, '0' or '1' separated by spaces.
Then m lines are the operations:
op a b: 0 <= op <= 4 , 0 <= a <= b < n.
Output
For each output operation , output the result.
Sample Input
1
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
Sample Output
5
2
6
5
线段树区间合并。每个节点维护当前区间1个总数sum,当前区间最长序列msum,左端点往右的最长序列lsum,右端点往左的最长序列rsum。注意把异或操作转换成成段更新即可,既当异或操作覆盖整个区间的时候,如果该区间有懒惰标记,把标记进行异或。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define SIZE 111111
#define ls l,mid,rt<<1
#define rs mid+1,r,rt<<1|1
using namespace std;
int sum[SIZE<<2];
int msum[SIZE<<2],lsum[SIZE<<2],rsum[SIZE<<2],cv[SIZE<<2];
void pushUp(int rt,int itv)
{
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
lsum[rt] = lsum[rt<<1];
rsum[rt] = rsum[rt<<1|1];
if(lsum[rt] == (itv - (itv>>1)))
lsum[rt] += lsum[rt<<1|1];
if(rsum[rt] == (itv >> 1))
rsum[rt] += rsum[rt<<1];
msum[rt] = max(max(msum[rt<<1],msum[rt<<1|1]),lsum[rt<<1|1]+rsum[rt<<1]);
}
void pushDown(int rt,int itv)
{
if(cv[rt] != -1)
{
cv[rt<<1] = cv[rt<<1|1] = cv[rt];
sum[rt<<1] = cv[rt]*(itv - (itv>>1));
sum[rt<<1|1] = cv[rt]*(itv >> 1);
msum[rt<<1] = lsum[rt<<1] = rsum[rt<<1] = sum[rt<<1];
msum[rt<<1|1] = lsum[rt<<1|1] = rsum[rt<<1|1] = sum[rt<<1|1];
cv[rt] = -1;
}
}
void build(int l,int r,int rt)
{
if(l == r)
{
scanf("%d",&cv[rt]);
msum[rt] = lsum[rt] = rsum[rt] = sum[rt] = cv[rt]?1:0;
return ;
}
cv[rt] = -1;
int mid = (l + r) >> 1;
build(ls);
build(rs);
pushUp(rt,r-l+1);
}
void update(int l,int r,int rt,int L,int R,int w)
{
if(L <= l && r <= R)
{
sum[rt] = w*(r-l+1);
msum[rt] = lsum[rt] = rsum[rt] = sum[rt];
cv[rt] = w;
return ;
}
pushDown(rt,r-l+1);
int mid = (l + r) >> 1;
if(L <= mid) update(ls,L,R,w);
if(R > mid) update(rs,L,R,w);
pushUp(rt,r-l+1);
}
void do_XOR(int l,int r,int rt,int L,int R)
{
if(L <= l && r <= R && cv[rt] != -1)
{
cv[rt] ^= 1;
sum[rt] = msum[rt] = lsum[rt] = rsum[rt] = cv[rt]?(r-l+1):0;
return ;
}
pushDown(rt,r-l+1);
int mid = (l + r) >> 1;
if(L <= mid) do_XOR(ls,L,R);
if(R > mid) do_XOR(rs,L,R);
pushUp(rt,r-l+1);
}
int querySum(int l,int r,int rt,int L,int R)
{
if(L <= l && r <= R)
return sum[rt];
pushDown(rt,r-l+1);
int mid = (l + r) >> 1;
int ret = 0;
if(L <= mid) ret += querySum(ls,L,R);
if(R > mid) ret += querySum(rs,L,R);
return ret;
}
int queryNum(int l,int r,int rt,int L,int R)
{
if(L <= l && r <= R)
return msum[rt];
pushDown(rt,r-l+1);
int mid = (l + r) >> 1;
if(L > mid)
return queryNum(rs,L,R);
else if(R <= mid)
return queryNum(ls,L,R);
else
{
int ll = queryNum(ls,L,R);
int rr = queryNum(rs,L,R);
int Lt = min(rsum[rt<<1],mid-L+1);
int Rt = min(lsum[rt<<1|1],R-mid);
return max(max(ll,rr),Lt+Rt);
}
}
int T;
int N,M;
int o,s,e;
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&N,&M);
build(1,N,1);
while(M--)
{
scanf("%d%d%d",&o,&s,&e);
s ++; e ++;
if(o == 0 || o == 1)
update(1,N,1,s,e,o);
else if(o == 2)
do_XOR(1,N,1,s,e);
else if(o == 3)
printf("%d\n",querySum(1,N,1,s,e));
else
printf("%d\n",queryNum(1,N,1,s,e));
}
}
return 0;
}