Luogu P2572 [SCOI2010]序列操作

题目描述

https://www.luogu.com.cn/problem/P2572

Solution

维护十个标记即可

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
#include <iostream>
#include <cstdio>
#include <cctype>
#define maxn 100010
#define cS const Seg
#define gc getchar
using namespace std;

int read() {
int x = 0; char c = gc();
while (!isdigit(c)) c = gc();
while (isdigit(c)) x = x * 10 + c - '0', c = gc();
return x;
}

inline int max(int a, int b, int c) { return max(a, max(b, c)); }

int n, m, a[maxn];

#define lc i << 1
#define rc i << 1 | 1
struct Seg {
int f0, f1, rev, l0, l1, r0, r1, v0, v1, s;

Seg() {}
Seg(int _f0, int _f1, int _rev, int _l0, int _l1, int _r0, int _r1, int _v0, int _v1, int _s) {
f0 = _f0; f1 = _f1; rev = _rev; l0 = _l0; l1 = _l1; r0 = _r0; r1 = _r1; v0 = _v0; v1 = _v1; s = _s;
}
} T[maxn * 4];
inline Seg maintain(int l, int r, cS &ls, cS &rs) {
Seg o; o.f0 = o.f1 = o.rev = 0; int m = l + r >> 1;
o.s = ls.s + rs.s;
o.l0 = ls.l0 + (!ls.s ? rs.l0 : 0);
o.r0 = rs.r0 + (!rs.s ? ls.r0 : 0);
o.l1 = ls.l1 + (ls.s == m - l + 1 ? rs.l1 : 0);
o.r1 = rs.r1 + (rs.s == r - m ? ls.r1 : 0);
o.v0 = max(ls.v0, rs.v0, ls.r0 + rs.l0);
o.v1 = max(ls.v1, rs.v1, ls.r1 + rs.l1);
return o;
}

inline void Up0(int i, int l, int r) {
T[i].l1 = T[i].r1 = T[i].f1 = T[i].rev = T[i].v1 = T[i].s = 0;
T[i].f0 = 1; T[i].l0 = T[i].r0 = T[i].v0 = r - l + 1;
}

inline void Up1(int i, int l, int r) {
T[i].l0 = T[i].r0 = T[i].f0 = T[i].v0 = T[i].rev = 0;
T[i].f1 = 1; T[i].s = T[i].v1 = T[i].l1 = T[i].r1 = r - l + 1;
}

inline void Up2(int i, int l, int r) {
T[i].s = r - l + 1 - T[i].s;
swap(T[i].l0, T[i].l1); swap(T[i].r0, T[i].r1); swap(T[i].v0, T[i].v1);
if (T[i].f0) T[i].f0 = 0, T[i].f1 = 1;
else if (T[i].f1) T[i].f0 = 1, T[i].f1 = 0;
else T[i].rev ^= 1;
}

inline void pushdown(int i, int l, int r) {
int &f0 = T[i].f0, &f1 = T[i].f1, &rev = T[i].rev, m = l + r >> 1;
if (!f0 && !f1 && !rev) return ;
if (f0) Up0(lc, l, m), Up0(rc, m + 1, r);
else if (f1) Up1(lc, l, m), Up1(rc, m + 1, r);
else Up2(lc, l, m), Up2(rc, m + 1, r);
f0 = f1 = rev = 0;
}

void build(int i, int l, int r) {
if (l == r) {
if (!a[l]) T[i] = Seg(0, 0, 0, 1, 0, 1, 0, 1, 0, 0);
else T[i] = Seg(0, 0, 0, 0, 1, 0, 1, 0, 1, 1);
return ;
} int m = l + r >> 1;
build(lc, l, m); build(rc, m + 1, r);
T[i] = maintain(l, r, T[lc], T[rc]);
}


void up0(int i, int l, int r, int L, int R) {
if (l > R || r < L) return ;
if (L <= l && r <= R) return Up0(i, l, r);
int m = l + r >> 1; pushdown(i, l, r);
up0(lc, l, m, L, R); up0(rc, m + 1, r, L, R);
T[i] = maintain(l, r, T[lc], T[rc]);
}

void up1(int i, int l, int r, int L, int R) {
if (l > R || r < L) return ;
if (L <= l && r <= R) return Up1(i, l, r);
int m = l + r >> 1; pushdown(i, l, r);
up1(lc, l, m, L, R); up1(rc, m + 1, r, L, R);
T[i] = maintain(l, r, T[lc], T[rc]);
}

void up2(int i, int l, int r, int L, int R) {
if (l > R || r < L) return ;
if (L <= l && r <= R) return Up2(i, l, r);
int m = l + r >> 1; pushdown(i, l, r);
up2(lc, l, m, L, R); up2(rc, m + 1, r, L, R);
T[i] = maintain(l, r, T[lc], T[rc]);
}

int q1(int i, int l, int r, int L, int R) {
if (l > R || r < L) return 0;
if (L <= l && r <= R) return T[i].s;
int m = l + r >> 1; pushdown(i, l, r);
return q1(lc, l, m, L, R) + q1(rc, m + 1, r, L, R);
}

Seg q2(int i, int l, int r, int L, int R) {
if (l > R || r < L) return Seg(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
if (L <= l && r <= R) return T[i];
int m = l + r >> 1; pushdown(i, l, r);
Seg o, ls = q2(lc, l, m, L, R), rs = q2(rc, m + 1, r, L, R);
o = maintain(l, r, ls, rs); return o;
}

inline void solve_0() {
int x = read() + 1, y = read() + 1;
up0(1, 1, n, x, y);
}

inline void solve_1() {
int x = read() + 1, y = read() + 1;
up1(1, 1, n, x, y);
}

inline void solve_2() {
int x = read() + 1, y = read() + 1;
up2(1, 1, n, x, y);
}

inline void solve_3() {
int x = read() + 1, y = read() + 1;
printf("%d\n", q1(1, 1, n, x, y));
}

inline void solve_4() {
int x = read() + 1, y = read() + 1;
printf("%d\n", q2(1, 1, n, x, y).v1);
}

int main() {
cin >> n >> m;
for (int i = 1; i <= n; ++i) a[i] = read();
build(1, 1, n);
for (int i = 1; i <= m; ++i) {
int opt = read();
switch (opt) {
case 0 : solve_0(); break;
case 1 : solve_1(); break;
case 2 : solve_2(); break;
case 3 : solve_3(); break;
case 4 : solve_4(); break;
}
}
return 0;
}