水题笔记_ [SCOI2010]序列操作 [线段树]

一些题目没有你想象的那么麻烦

懵逼的 题目

传送至 Luogu

扯淡的 题解

非常水啊..裸的线段树…
注意细节…尤其pushdown…

沙茶的 代码

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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// luogu-judger-enable-o2
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#define MAXN (1000000 + 5)
#define LS(dq) ((dq) << 1)
#define RS(dq) (((dq) << 1) | 1)
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define swap(a, b) {int ____TemP = a; a = b; b = ____TemP;}
using namespace std;
struct node
{
int lazy; // 1: 0; 2: 1; 3: 鍙栧弽
int le;
int ri;
int l1;
int l0;
int r1;
int r0;
int zh1;
int zh0;
int lx0;
int lx1;
} b[MAXN << 2];

int n;
int a[MAXN];
void init(int, int, int);
void change(int, int, int, int);
node ask(int, int, int);
inline void pushup(int);
inline void pushdown(int);
inline void ms(int);
int main()
{
int q;
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
}
init(1, 1, n);
int sre, srx, sry;
for (int i = 1; i <= q; i++)
{
scanf("%d%d%d", &sre, &srx, &sry);
++sre, ++srx, ++sry;
if (sre <= 3)
{
change(1, srx, sry, sre);
}
else
{
node out = ask(1, srx, sry);
if (sre == 4)
{
printf("%d\n", out.zh1);
}
else
{
printf("%d\n", out.lx1);
}
}
}
return 0;
}
void init(int dq, int le, int ri)
{
b[dq].le = le, b[dq].ri = ri, b[dq].lazy = 0;
if (le == ri)
{
b[dq].lx1 = b[dq].zh1 = b[dq].l1 = b[dq].r1 = a[le];
b[dq].lx0 = b[dq].zh0 = b[dq].l0 = b[dq].r0 = 1 - a[le];
return ;
}
int mi = (le + ri) >> 1;
init(LS(dq), le, mi);
init(RS(dq), mi + 1, ri);
pushup(dq);
}
/*
struct node
{
int lazy; // 0: 0; 1: 1; 2: 鍙栧弽
int le;
int ri;

int l1;
int l0;
int r1;
int r0;
int zh1;
int zh0;
int lx0;
int lx1;
}b[MAXN << 2];
*/
inline void pushup(int dq)
{
b[dq].zh1 = b[LS(dq)].zh1 + b[RS(dq)].zh1;
b[dq].zh0 = b[LS(dq)].zh0 + b[RS(dq)].zh0;
b[dq].l1 = (b[LS(dq)].zh1 == (b[LS(dq)].ri - b[LS(dq)].le + 1) ? (b[LS(dq)].zh1 + b[RS(dq)].l1) : (b[LS(dq)].l1));
b[dq].l0 = (b[LS(dq)].zh0 == (b[LS(dq)].ri - b[LS(dq)].le + 1) ? (b[LS(dq)].zh0 + b[RS(dq)].l0) : (b[LS(dq)].l0));
b[dq].r1 = (b[RS(dq)].zh1 == (b[RS(dq)].ri - b[RS(dq)].le + 1) ? (b[RS(dq)].zh1 + b[LS(dq)].r1) : (b[RS(dq)].r1));
b[dq].r0 = (b[RS(dq)].zh0 == (b[RS(dq)].ri - b[RS(dq)].le + 1) ? (b[RS(dq)].zh0 + b[LS(dq)].r0) : (b[RS(dq)].r0));
b[dq].lx1 = max(max(b[LS(dq)].lx1, b[RS(dq)].lx1), b[LS(dq)].r1 + b[RS(dq)].l1);
b[dq].lx0 = max(max(b[LS(dq)].lx0, b[RS(dq)].lx0), b[LS(dq)].r0 + b[RS(dq)].l0);
}
inline void pushdown(int dq)
{
int x = b[dq].lazy;
if (x == 1 || x == 2)
{
b[RS(dq)].lazy = b[LS(dq)].lazy = x;
}
else
{
if (b[RS(dq)].lazy)
{
if (b[RS(dq)].lazy == 1)
{
b[RS(dq)].lazy = 2;
}
else if (b[RS(dq)].lazy == 2)
{
b[RS(dq)].lazy = 1;
}
else if (b[RS(dq)].lazy == 3)
{
b[RS(dq)].lazy = 0;
}
}
else
{
b[RS(dq)].lazy = x;
}
if (b[LS(dq)].lazy)
{
if (b[LS(dq)].lazy == 1)
{
b[LS(dq)].lazy = 2;
}
else if (b[LS(dq)].lazy == 2)
{
b[LS(dq)].lazy = 1;
}
else if (b[LS(dq)].lazy == 3)
{
b[LS(dq)].lazy = 0;
}
}
else
{
b[LS(dq)].lazy = x;
}
}
if (b[LS(dq)].lazy == 3 || (!b[LS(dq)].lazy))
{
ms(LS(dq));
}
else
{
if (b[LS(dq)].lazy == 2)
{
b[LS(dq)].l0 = b[LS(dq)].r0 = b[LS(dq)].zh0 = b[LS(dq)].lx0 = 0;
b[LS(dq)].l1 = b[LS(dq)].r1 = b[LS(dq)].zh1 = b[LS(dq)].lx1 = (b[LS(dq)].ri - b[LS(dq)].le + 1);
}
else if (b[LS(dq)].lazy == 1)
{
b[LS(dq)].l1 = b[LS(dq)].r1 = b[LS(dq)].zh1 = b[LS(dq)].lx1 = 0;
b[LS(dq)].l0 = b[LS(dq)].r0 = b[LS(dq)].zh0 = b[LS(dq)].lx0 = (b[LS(dq)].ri - b[LS(dq)].le + 1);
}
}
if (b[RS(dq)].lazy == 3 || (!b[RS(dq)].lazy))
{
ms(RS(dq));
}
else
{
if (b[RS(dq)].lazy == 2)
{
b[RS(dq)].l0 = b[RS(dq)].r0 = b[RS(dq)].zh0 = b[RS(dq)].lx0 = 0;
b[RS(dq)].l1 = b[RS(dq)].r1 = b[RS(dq)].zh1 = b[RS(dq)].lx1 = (b[RS(dq)].ri - b[RS(dq)].le + 1);
}
else if (b[RS(dq)].lazy == 1)
{
b[RS(dq)].l1 = b[RS(dq)].r1 = b[RS(dq)].zh1 = b[RS(dq)].lx1 = 0;
b[RS(dq)].l0 = b[RS(dq)].r0 = b[RS(dq)].zh0 = b[RS(dq)].lx0 = (b[RS(dq)].ri - b[RS(dq)].le + 1);
}
}
b[dq].lazy = 0;
}
inline void ms(int dq)
{
swap(b[dq].l0, b[dq].l1);
swap(b[dq].r0, b[dq].r1);
swap(b[dq].zh0, b[dq].zh1);
swap(b[dq].lx0, b[dq].lx1);
}
void change(int dq, int le, int ri, int zh)
{
if (b[dq].lazy)
pushdown(dq);
if (b[dq].le == le && b[dq].ri == ri)
{
b[dq].lazy = zh;
if (zh == 3)
{
ms(dq);
}
else
{
if (zh == 2)
{
b[dq].l0 = b[dq].r0 = b[dq].zh0 = b[dq].lx0 = 0;
b[dq].l1 = b[dq].r1 = b[dq].zh1 = b[dq].lx1 = (b[dq].ri - b[dq].le + 1);
}
else
{
b[dq].l1 =b[dq].r1 = b[dq].zh1 = b[dq].lx1 = 0;
b[dq].l0 = b[dq].r0 = b[dq].zh0 = b[dq].lx0 = (b[dq].ri - b[dq].le + 1);
}
}
return ;
}
int mi = (b[dq].le + b[dq].ri) >> 1;
if (le > mi)
{
change(RS(dq), le, ri, zh);
}
else if (ri <= mi)
{
change(LS(dq), le, ri, zh);
}
else
{
change(LS(dq), le, mi, zh);
change(RS(dq), mi + 1, ri, zh);
}
pushup(dq);
}
node ask(int dq, int le, int ri)
{
if (b[dq].lazy)
pushdown(dq);
if (b[dq].le == le && b[dq].ri == ri)
{
return b[dq];
}
int mi = (b[dq].le + b[dq].ri) >> 1;
if (le > mi)
{
return ask(RS(dq), le, ri);
}
else if (ri <= mi)
{
return ask(LS(dq), le, ri);
}
else
{
node ln = ask(LS(dq), le, mi), rn = ask(RS(dq), mi + 1, ri), re;
re.le = le, re.ri = ri;
re.zh1 = ln.zh1 + rn.zh1;
re.zh0 = ln.zh0 + rn.zh0;
re.l1 = (ln.zh1 == (ln.ri - ln.le + 1) ? (ln.zh1 + rn.l1) : (ln.l1));
re.l0 = (ln.zh0 == (ln.ri - ln.le + 1) ? (ln.zh0 + rn.l0) : (ln.l0));
re.r1 = (rn.zh1 == (rn.ri - rn.le + 1) ? (rn.zh1 + ln.r1) : (rn.r1));
re.r0 = (rn.zh0 == (rn.ri - rn.le + 1) ? (rn.zh0 + ln.r0) : (rn.r0));
re.lx1 = max(max(ln.lx1, rn.lx1), ln.r1 + rn.l1);
re.lx0 = max(max(ln.lx0, rn.lx0), ln.r0 + rn.l0);
return re;
}
}

/*
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
*/

By 被罚了4篇+ 作文的 Cansult