hdu 3397 Sequence operation

本文从WordPress迁移而来, 查看全部WordPress迁移文章

线段树

和UVA那题01串是一样的

记录的信息后,0,1的总数,前缀0和前缀1的总数,后缀0和后缀1的总数,最长0和最长1的长度

对于交换操作,就是总数,前缀,后缀,最长长度对应的0,1的数目交换

设置3个lazy记录之前是进行了变0,变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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define N 100010
#define lson(i) ((i)<<1)
#define rson(i) ((i)<<1|1)

struct SegTree{
int l,r,mid,len;
int sum[2],pre[2],suf[2],Max[2];
bool lazy[3];
SegTree(){}
SegTree(int __l,int __r){
l = __l; r = __r; mid = (l+r) >> 1; len = r - l + 1;
for(int i = 0; i < 2; i++)
sum[i] = pre[i] = suf[i] = Max[i] = 0;
for(int i = 0; i < 3; i++)
lazy[i] = false;
}
}t[N<<2];

void Reverse(int rt){
swap(t[rt].sum[0],t[rt].sum[1]);
swap(t[rt].pre[0],t[rt].pre[1]);
swap(t[rt].suf[0],t[rt].suf[1]);
swap(t[rt].Max[0],t[rt].Max[1]);
t[rt].lazy[2] = true;
t[rt].lazy[0] = t[rt].lazy[1] = false;
}

void Del(int rt,int val){
if(val == 0){
t[rt].lazy[0] = true; t[rt].lazy[1] = t[rt].lazy[2] = false;
t[rt].sum[0] = t[rt].pre[0] = t[rt].suf[0] = t[rt].Max[0] = t[rt].len;
t[rt].sum[1] = t[rt].pre[1] = t[rt].suf[1] = t[rt].Max[1] = 0;
}
else{
t[rt].lazy[1] = true; t[rt].lazy[0] = t[rt].lazy[2] = false;
t[rt].sum[1] = t[rt].pre[1] = t[rt].suf[1] = t[rt].Max[1] = t[rt].len;
t[rt].sum[0] = t[rt].pre[0] = t[rt].suf[0] = t[rt].Max[0] = 0;
}
}

void PushUp(SegTree &A,SegTree L,SegTree R){
for(int i = 0; i < 2; i++)
A.sum[i] = L.sum[i] + R.sum[i];
//pre[0],suf[0]
A.pre[0] = L.pre[0]; A.suf[0] = R.suf[0];
if(L.pre[0] == L.len){
A.pre[0] = L.len + R.pre[0];
}
if(R.suf[0] == R.len){
A.suf[0] = L.suf[0] + R.len;
}
//pre[1],suf[1]
A.pre[1] = L.pre[1]; A.suf[1] = R.suf[1];
if(L.pre[1] == L.len){
A.pre[1] = L.len + R.pre[1];
}
if(R.suf[1] == R.len){
A.suf[1] = L.suf[1] + R.len;
}
//Max[0],Max[1]
A.Max[0] = max(max(L.Max[0],R.Max[0]) , L.suf[0]+R.pre[0]);
A.Max[1] = max(max(L.Max[1],R.Max[1]) , L.suf[1]+R.pre[1]);
}

void PushDown(int rt){
if(t[rt].lazy[0]){
Del(lson(rt),0);
Del(rson(rt),0);
t[rt].lazy[0] = false;
}
if(t[rt].lazy[1]){
Del(lson(rt),1);
Del(rson(rt),1);
t[rt].lazy[1] = false;
}
if(t[rt].lazy[2]){
if(t[lson(rt)].lazy[0])
Del(lson(rt),1);
else if(t[lson(rt)].lazy[1])
Del(lson(rt),0);
else if(t[lson(rt)].lazy[2]){
Reverse(lson(rt)); t[lson(rt)].lazy[2] = false;
}
else Reverse(lson(rt));

if(t[rson(rt)].lazy[0])
Del(rson(rt),1);
else if(t[rson(rt)].lazy[1])
Del(rson(rt),0);
else if(t[rson(rt)].lazy[2]){
Reverse(rson(rt)); t[rson(rt)].lazy[2] = false;
}
else Reverse(rson(rt));

t[rt].lazy[2] = false;
}
}

void build(int rt,int a,int b){
t[rt] = SegTree(a,b);
if(a == b){
int x;
scanf("%d",&x);
if(x == 0){
t[rt].sum[0] = t[rt].pre[0] = t[rt].suf[0] = t[rt].Max[0] = 1;
t[rt].lazy[0] = true;
}
else{
t[rt].sum[1] = t[rt].pre[1] = t[rt].suf[1] = t[rt].Max[1] = 1;
t[rt].lazy[1] = true;
}
return ;
}
int mid = t[rt].mid;
build(lson(rt),a,mid);
build(rson(rt),mid+1,b);
PushUp(t[rt],t[lson(rt)],t[rson(rt)]);
}

SegTree query(int rt,int a,int b){
if(t[rt].l == a && t[rt].r == b){
return t[rt];
}
PushDown(rt);
int mid = t[rt].mid;
if(b <= mid){
return query(lson(rt),a,b);
}
else if(a > mid){
return query(rson(rt),a,b);
}
else{
SegTree L = query(lson(rt),a,mid);
SegTree R = query(rson(rt),mid+1,b);
SegTree A = SegTree(a,b);
PushUp(A,L,R);
return A;
}
}

void updata_set(int rt,int a,int b,int val){
if(t[rt].l == a && t[rt].r == b){
Del(rt,val);
return ;
}
PushDown(rt);
int mid = t[rt].mid;
if(b <= mid)
updata_set(lson(rt),a,b,val);
else if(a > mid)
updata_set(rson(rt),a,b,val);
else{
updata_set(lson(rt),a,mid,val);
updata_set(rson(rt),mid+1,b,val);
}
PushUp(t[rt],t[lson(rt)],t[rson(rt)]);
}

void updata_reverse(int rt,int a,int b){
if(t[rt].l == a && t[rt].r == b){
if(t[rt].lazy[0]) Del(rt,1);
else if(t[rt].lazy[1]) Del(rt,0);
else if(t[rt].lazy[2]){
Reverse(rt); t[rt].lazy[2] = false;
}
else Reverse(rt);
return ;
}
PushDown(rt);
int mid = t[rt].mid;
if(b <= mid)
updata_reverse(lson(rt),a,b);
else if(a > mid)
updata_reverse(rson(rt),a,b);
else{
updata_reverse(lson(rt),a,mid);
updata_reverse(rson(rt),mid+1,b);
}
PushUp(t[rt],t[lson(rt)],t[rson(rt)]);
}

int main(){
int n,q,cas;
scanf("%d",&cas);
while(cas--){
scanf("%d%d",&n,&q);
build(1,0,n-1);
while(q--){
int op,a,b;
scanf("%d%d%d",&op,&a,&b);
if(op == 0 || op == 1){
int val = (op == 0 ? 0 : 1);
updata_set(1,a,b,val);
}
else if(op == 2){
updata_reverse(1,a,b);
}
else{
SegTree res = query(1,a,b);
if(op == 3) printf("%d\n",res.sum[1]);
else printf("%d\n",res.Max[1]);
}
}
}
return 0;
}