题意: 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]
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
嗯嗯
2015-07-26:专题复习
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
using namespace std;
#define MOD 1000000007
const int INF=0x3f3f3f3f;
const double eps=1e-;
#define cl(a) memset(a,0,sizeof(a))
#define ts printf("*****\n");
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root 1,n,1
#define mid ((l+r)>>1)
const int MAXN=;
int n,m,t,Min,tt;
int msum1[MAXN<<],col[MAXN<<],lsum1[MAXN<<],rsum1[MAXN<<];
int msum0[MAXN<<],lsum0[MAXN<<],rsum0[MAXN<<],xxor[MAXN<<];
int sum1[MAXN<<],sum0[MAXN<<];
void fun1(int rt,int m,int val) //改变区间内0/1的变化
{
msum1[rt]=lsum1[rt]=rsum1[rt]=sum1[rt]=val?m:;
msum0[rt]=lsum0[rt]=rsum0[rt]=sum0[rt]=val?:m;
}
void fun2(int rt)
{
swap(msum1[rt],msum0[rt]);
swap(lsum1[rt],lsum0[rt]);
swap(rsum1[rt],rsum0[rt]);
swap(sum1[rt],sum0[rt]);
} void pushup(int rt,int m)
{
sum1[rt]=sum1[rt<<]+sum1[rt<<|];
lsum1[rt]=lsum1[rt<<];
rsum1[rt]=rsum1[rt<<|];
if(lsum1[rt]==(m-(m>>))) lsum1[rt]+=lsum1[rt<<|];
if(rsum1[rt]==(m>>)) rsum1[rt]+=rsum1[rt<<];
msum1[rt]=max(lsum1[rt<<|]+rsum1[rt<<],max(msum1[rt<<],msum1[rt<<|])); sum0[rt]=sum0[rt<<]+sum0[rt<<|];
lsum0[rt]=lsum0[rt<<];
rsum0[rt]=rsum0[rt<<|];
if(lsum0[rt]==(m-(m>>))) lsum0[rt]+=lsum0[rt<<|];
if(rsum0[rt]==(m>>)) rsum0[rt]+=rsum0[rt<<];
msum0[rt]=max(lsum0[rt<<|]+rsum0[rt<<],max(msum0[rt<<],msum0[rt<<|]));
}
void build(int l,int r,int rt)
{
col[rt]=-;
xxor[rt]=;
if(l==r)
{
int num;
scanf("%d",&num);
msum1[rt]=lsum1[rt]=rsum1[rt]=sum1[rt]=num?:;
msum0[rt]=lsum0[rt]=rsum0[rt]=sum0[rt]=num?:;
return;
}
build(lson);
build(rson);
pushup(rt,r-l+);
}
void pushdown(int rt,int m)
{
if(col[rt]!=-)
{
xxor[rt<<]=xxor[rt<<|]=;
col[rt<<]=col[rt<<|]=col[rt];
fun1(rt<<,m-(m>>),col[rt]);
fun1(rt<<|,m>>,col[rt]);
col[rt]=-;
}
else if(xxor[rt])
{
if(col[rt<<]!=-)
{
col[rt<<]^=;
fun1(rt<<,m-(m>>),col[rt<<]);
}
else
{
xxor[rt<<]^=;
fun2(rt<<); //最大连续1的变化有点麻烦,必须要重新开一个最大连续0的变量
}
if(col[rt<<|]!=-)
{
col[rt<<|]^=;
fun1(rt<<|,(m>>),col[rt<<|]);
}
else
{
xxor[rt<<|]^=;
fun2(rt<<|);
}
xxor[rt]=;
}
}
void update(int L,int R,int val,int l,int r,int rt)
{
if(l>=L&&r<=R)
{
if(val<=)
{
xxor[rt]=;
col[rt]=val;
fun1(rt,r-l+,val);
}
else
{
if(col[rt]!=-)
{
col[rt]^=;
fun1(rt,r-l+,col[rt]);
}
else
{
xxor[rt]^=;
fun2(rt);
}
}
return;
}
pushdown(rt,r-l+);
if(L<=mid) update(L,R,val,lson);
if(R>mid) update(L,R,val,rson);
pushup(rt,r-l+);
}
int query1(int L,int R,int l,int r,int rt) //查询区间内1的个数
{
if(l>=L&&r<=R)
{
return sum1[rt];
}
pushdown(rt,r-l+);
int ans=;
if(L<=mid) ans+=query1(L,R,lson);
if(R>mid) ans+=query1(L,R,rson);
return ans;
}
int query2(int L,int R,int l,int r,int rt) //查询区间内最长连续1的个数
{
if(l>=L&&r<=R)
{
return msum1[rt];
}
pushdown(rt,r-l+); if(R<=mid) return query2(L,R,lson);
if(L>mid) return query2(L,R,rson);
int a=query2(L,R,lson);
int b=query2(L,R,rson);
a=max(a,b);
b=min(mid-L+,rsum1[rt<<])+min(R-mid,lsum1[rt<<|]);
return max(a,b);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
#endif
int t,n,m,op,a,b;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
build(root);
while(m--)
{
scanf("%d%d%d",&op,&a,&b);
a++,b++;
if(op<=) update(a,b,op,root);
else
{
if(op==) printf("%d\n",query1(a,b,root));
else printf("%d\n",query2(a,b,root));
}
}
}
return ;
}
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = ;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int num0[maxn<<],num1[maxn<<];
int lb0[maxn<<],lb1[maxn<<];
int rb0[maxn<<],rb1[maxn<<];
int mx1[maxn<<],mx0[maxn<<];
int col[maxn<<];
int xxor[maxn<<];
int num;
int max(int a,int b){
return a>b?a:b;
}
int min(int a,int b){
return a<b?a:b;
}
void pushup(int rt,int m)
{
lb0[rt]=lb0[rt<<];rb0[rt]=rb0[rt<<|];
if(lb0[rt]==m-(m>>)) lb0[rt]+=lb0[rt<<|];
if(rb0[rt]==(m>>)) rb0[rt]+=rb0[rt<<]; lb1[rt]=lb1[rt<<];rb1[rt]=rb1[rt<<|];
if(lb1[rt]==m-(m>>)) lb1[rt]+=lb1[rt<<|];
if(rb1[rt]==(m>>)) rb1[rt]+=rb1[rt<<]; num0[rt]=num0[rt<<]+num0[rt<<|];
num1[rt]=num1[rt<<]+num1[rt<<|]; mx1[rt]=max(mx1[rt<<],mx1[rt<<|]);
mx1[rt]=max(mx1[rt],rb1[rt<<]+lb1[rt<<|]); mx0[rt]=max(mx0[rt<<],mx0[rt<<|]);
mx0[rt]=max(mx0[rt],rb0[rt<<]+lb0[rt<<|]);
}
void build(int l,int r,int rt){
col[rt]=-; xxor[rt]=;
if(l==r){
scanf("%d",&num);
num0[rt]=lb0[rt]=rb0[rt]=mx0[rt]=(num?:);
num1[rt]=lb1[rt]=rb1[rt]=mx1[rt]=(num?:);
return ;
}
int m=(l+r)>>;
build(lson);
build(rson);
pushup(rt,r-l+);
}
void init(int rt,int m,int cmd){
lb0[rt]=rb0[rt]=mx0[rt]=num0[rt]=m*(-cmd);
lb1[rt]=rb1[rt]=mx1[rt]=num1[rt]=m*cmd;
}
void change(int rt){
swap(lb0[rt],lb1[rt]);
swap(rb0[rt],rb1[rt]);
swap(mx0[rt],mx1[rt]);
swap(num0[rt],num1[rt]);
}
void pushdown(int rt,int m){
if(col[rt]!=-){
col[rt<<]=col[rt<<|]=col[rt];
xxor[rt<<]=xxor[rt<<|]=;
init(rt<<,m-(m>>),col[rt]);
init(rt<<|,(m>>),col[rt]);
col[rt]=-;
}
else if(xxor[rt]){
if(col[rt<<]!=-){
col[rt<<]^=;
init(rt<<,m-(m>>),col[rt<<]);
}
else {
xxor[rt<<]^=;
change(rt<<);
}
if(col[rt<<|]!=-){
col[rt<<|]^=;
init(rt<<|,(m>>),col[rt<<|]);
}
else {
xxor[rt<<|]^=;
change(rt<<|);
}
xxor[rt]=;
}
}
void update(int L,int R,int cmd,int l,int r,int rt){
if(L<=l&&r<=R){
if(cmd<=){
xxor[rt]=;//取消异或标记
col[rt]=cmd;//真个区间被完全覆盖为cmd
init(rt,r-l+,cmd);
}
else {
if(col[rt]!=-){//被完全覆盖,全是0或者全是1,相当于取反
col[rt]^=;
init(rt,r-l+,col[rt]);
}
else {
xxor[rt]^=;
change(rt);
}
}
return ;
}
pushdown(rt,r-l+);
int m=(l+r)>>;
if(L<=m) update(L,R,cmd,lson);
if(R>m) update(L,R,cmd,rson);
pushup(rt,r-l+);
}
int query1(int L,int R,int l,int r,int rt){
if(L<=l&&r<=R) return num1[rt];
pushdown(rt,r-l+);
int m=(l+r)>>;
int ans=;
if(L<=m) ans+=query1(L,R,lson);
if(R>m) ans+=query1(L,R,rson);
return ans;
}
int query2(int L,int R,int l,int r,int rt){
if(L<=l&&r<=R) {
return mx1[rt];
}
pushdown(rt,r-l+);
int m=(l+r)>>;
if(R<=m) return query2(L,R,lson);
if(L>m) return query2(L,R,rson);
int a=query2(L,R,lson);
int b=query2(L,R,rson);
a=a>b?a:b;
b=min(m-L+,rb1[rt<<])+min(R-m,lb1[rt<<|]);
return a>b?a:b;
}
int main(){
int t,n,m,op,a,b;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
build(,n-,);
while(m--){
scanf("%d%d%d",&op,&a,&b);
if(op<=) update(a,b,op,,n-,);
else {
if(op==) printf("%d\n",query1(a,b,,n-,));
else printf("%d\n",query2(a,b,,n-,));
}
}
}
return ;
}