bzoj1858 序列操作

模板题,维护最长连续的1即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cassert>
using namespace std;
const int maxn=100000+5;
#define lson (o<<1)
#define rson (o<<1|1)
int a[maxn],n,m;
namespace segment_tree{
int sumv[maxn*6],setv[maxn*6],lefc[2][maxn*6],rghc[2][maxn*6],cot[2][maxn*6];
bool rev[maxn*6];
void ini()
{
memset(setv,-1,sizeof(setv));
}
void maintain(int o, int l,int r)
{
sumv[o]=sumv[lson]+sumv[rson];
int mid=(l+r)>>1;
cot[0][o]=max(max(cot[0][lson],cot[0][rson]),rghc[0][lson]+lefc[0][rson]);
cot[1][o]=max(max(cot[1][lson],cot[1][rson]),rghc[1][lson]+lefc[1][rson]);
lefc[0][o]=lefc[0][lson];
if(sumv[lson]==0) lefc[0][o]=mid-l+1+lefc[0][rson];
lefc[1][o]=lefc[1][lson];
if(sumv[lson]==(mid-l+1)) lefc[1][o]=mid-l+1+lefc[1][rson];
rghc[0][o]=rghc[0][rson];
if(sumv[rson]==0) rghc[0][o]=r-mid+rghc[0][lson];
rghc[1][o]=rghc[1][rson];
if(sumv[rson]==(r-mid)) rghc[1][o]=r-mid+rghc[1][lson];
if(setv[o]!=-1){
sumv[o]=setv[o]*(r-l+1);
cot[0][o]=lefc[0][o]=rghc[0][o]=(setv[o]==0)*(r-l+1);
cot[1][o]=lefc[1][o]=rghc[1][o]=(setv[o]==1)*(r-l+1);
}
if(rev[o]){
sumv[o]=(r-l+1)-sumv[o];
swap(cot[0][o],cot[1][o]),swap(lefc[0][o],lefc[1][o]),swap(rghc[0][o],rghc[1][o]);
}
}
void getrev(int o,int l,int r)
{
rev[o]^=1;
maintain(o,l,r);
}
void getset(int o,int l,int r,int val)
{
rev[o]=0;
setv[o]=val;
maintain(o,l,r);
}
void pushdown(int o,int l,int r)
{
int mid=(l+r)>>1;
if(setv[o]!=-1){
getset(lson,l,mid,setv[o]);
getset(rson,mid+1,r,setv[o]);
setv[o]=-1;
maintain(o,l,r);
}
if(rev[o]){
getrev(lson,l,mid);getrev(rson,mid+1,r);
rev[o]=0;
maintain(o,l,r);
}
}
void Set(int o,int l,int r,int ql,int qr,int val)
{
if(ql>r || qr<l) return;
if(ql<=l && r<=qr) {getset(o,l,r,val);return;}
else{
int mid=(l+r)>>1;
pushdown(o,l,r);
if(ql<=mid) Set(lson,l,mid,ql,qr,val);
if(qr>mid) Set(rson,mid+1,r,ql,qr,val);
maintain(o,l,r);
}
}
void Rev(int o,int l,int r,int ql,int qr)
{
if(ql>r || qr<l) return;
if(ql<=l && r<=qr) {getrev(o,l,r);return;}
else{
int mid=(l+r)>>1;
pushdown(o,l,r);
if(ql<=mid) Rev(lson,l,mid,ql,qr);
if(qr>mid) Rev(rson,mid+1,r,ql,qr);
maintain(o,l,r);
}
}
int Qsum(int o,int l,int r,int ql,int qr)
{
if(ql>r || qr<l) return 0;
if(ql<=l && r<=qr) return sumv[o];
else{
int mid=(l+r)>>1;
pushdown(o,l,r);
int ret=0;
if(ql<=mid) ret+=Qsum(lson,l,mid,ql,qr);
if(qr>mid) ret+=Qsum(rson,mid+1,r,ql,qr);
return ret;
}
}
int Qcon(int o,int l,int r,int ql,int qr,int dir)
{
if(ql>r || qr<l) return 0;
if(ql<=l && r<=qr){
if(dir==0) return cot[1][o];
if(dir==1) return lefc[1][o];
if(dir==2) return rghc[1][o];
}else{
int mid=(l+r)>>1;
pushdown(o,l,r);
if(qr<=mid) return Qcon(lson,l,mid,ql,qr,dir);
else if(ql>mid) return Qcon(rson,mid+1,r,ql,qr,dir);
else{
int mid=(l+r)>>1;
if(dir==0){
int ret=0;
ret=max(ret,Qcon(lson,l,mid,ql,qr,2)+Qcon(rson,mid+1,r,ql,qr,1));
ret=max(ret,Qcon(lson,l,mid,ql,qr,0));
ret=max(ret,Qcon(rson,mid+1,r,ql,qr,0));
return ret;
}else if(dir==1){
int sm=Qsum(lson,l,mid,ql,qr);
if(sm==mid-max(l,ql)+1){
return sm+Qcon(rson,mid+1,r,ql,qr,1);
}else return Qcon(lson,l,mid,ql,qr,1);
}else{
int sm=Qsum(rson,mid+1,r,ql,qr);
if(sm==min(r,qr)-mid){
return sm+Qcon(lson,l,mid,ql,qr,2);
}else return Qcon(rson,mid+1,r,ql,qr,2);
}
assert(0);
}
assert(0);
}
}
};
using namespace segment_tree;
int main()
{
ini();
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) {
scanf("%d",&a[i]);
Set(1,1,n,i,i,a[i]);
}
int op,l,r;
for(int i=1;i<=m;i++){
scanf("%d%d%d",&op,&l,&r);
l++,r++;
if(op==0) Set(1,1,n,l,r,0);
else if(op==1) Set(1,1,n,l,r,1);
else if(op==2) Rev(1,1,n,l,r);
else if(op==3) printf("%d\n",Qsum(1,1,n,l,r));
else printf("%d\n",Qcon(1,1,n,l,r,0));
}
return 0;
}