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
| #include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
namespace IO { template <typename T> void read(T &x) { x = 0; char c = getchar(); while(!isdigit(c)) c = getchar(); while(isdigit(c)) x = x * 10 + c - '0', c = getchar(); return; }
template <typename T> void write(T x) { if(x > 9) write(x / 10); putchar(x % 10 + '0'); return; } } using namespace IO;
namespace SegTree { #define ls (rt << 1) #define rs (rt << 1 | 1) #define swap(a, b) a ^= b ^= a ^= b
int len[N << 2], sum[N << 2], tag[N << 2], rev[N << 2]; int mx[N << 2][2], lmx[N << 2][2], rmx[N << 2][2];
void pushup(int rt) { sum[rt] = sum[ls] + sum[rs];
lmx[rt][0] = lmx[ls][0]; rmx[rt][0] = rmx[rs][0]; lmx[rt][1] = lmx[ls][1]; rmx[rt][1] = rmx[rs][1];
if(lmx[rt][0] == len[ls]) lmx[rt][0] += lmx[rs][0]; if(rmx[rt][0] == len[rs]) rmx[rt][0] += rmx[ls][0]; if(lmx[rt][1] == len[ls]) lmx[rt][1] += lmx[rs][1]; if(rmx[rt][1] == len[rs]) rmx[rt][1] += rmx[ls][1];
mx[rt][0] = max(max(mx[ls][0], mx[rs][0]), rmx[ls][0] + lmx[rs][0]); mx[rt][1] = max(max(mx[ls][1], mx[rs][1]), rmx[ls][1] + lmx[rs][1]); }
void Swap(int x) { swap(mx[x][0], mx[x][1]); swap(lmx[x][0], lmx[x][1]); swap(rmx[x][0], rmx[x][1]); }
void pushdown(int rt) { if(~tag[rt]) { sum[ls] = len[ls] * tag[rt]; sum[rs] = len[rs] * tag[rt];
tag[ls] = tag[rt]; tag[rs] = tag[rt];
rev[ls] = rev[rs] = 0;
int x = tag[rt], y = tag[rt] ^ 1; mx[ls][x] = lmx[ls][x] = rmx[ls][x] = len[ls]; mx[rs][x] = lmx[rs][x] = rmx[rs][x] = len[rs]; mx[ls][y] = lmx[ls][y] = rmx[ls][y] = 0; mx[rs][y] = lmx[rs][y] = rmx[rs][y] = 0;
tag[rt] = -1; }
if(rev[rt]) { sum[ls] = len[ls] - sum[ls]; sum[rs] = len[rs] - sum[rs];
rev[ls] ^= rev[rt]; rev[rs] ^= rev[rt];
Swap(ls), Swap(rs);
rev[rt] = 0; } }
void build(int l, int r, int rt) { len[rt] = r - l + 1; tag[rt] = -1; if(l == r) { int a; read(a); sum[rt] = a; mx[rt][a] = lmx[rt][a] = rmx[rt][a] = 1; return; } int mid = (l + r) >> 1; build(l, mid, ls); build(mid + 1, r, rs); pushup(rt); }
void update(int L, int R, int x, int l, int r, int rt) { if(l > R || r < L) return; if(L <= l && r <= R) { if(~x) { sum[rt] = len[rt] * x; tag[rt] = x; rev[rt] = 0; mx[rt][x] = lmx[rt][x] = rmx[rt][x] = len[rt]; mx[rt][x ^ 1] = lmx[rt][x ^ 1] = rmx[rt][x ^ 1] = 0; } else { rev[rt] ^= 1; sum[rt] = len[rt] - sum[rt]; Swap(rt); } return; } pushdown(rt); int mid = (l + r) >> 1; update(L, R, x, l, mid, ls); update(L, R, x, mid + 1, r, rs); pushup(rt); }
int qry_cnt(int L, int R, int l, int r, int rt) { if(l > R || r < L) return 0; if(L <= l && r <= R) return sum[rt]; pushdown(rt); int mid = (l + r) >> 1; return qry_cnt(L, R, l, mid, ls) + qry_cnt(L, R, mid + 1, r, rs); }
int qry_max(int L, int R, int l, int r, int rt) { if(l > R || r < L) return 0; if(L <= l && r <= R) return mx[rt][1]; pushdown(rt); int mid = (l + r) >> 1, res = min(rmx[ls][1], mid - L + 1) + min(lmx[rs][1], R - mid); return max(res, max(qry_max(L, R, l, mid, ls), qry_max(L, R, mid + 1, r, rs))); } } using namespace SegTree;
namespace Acestar { void main() { int n, m; read(n), read(m);
build(1, n, 1);
while(m--) { int op, l, r; read(op), read(l), read(r); l++ , r++; if(op == 0) update(l, r, 0, 1, n, 1); if(op == 1) update(l, r, 1, 1, n, 1); if(op == 2) update(l, r, -1, 1, n, 1); if(op == 3) write(qry_cnt(l, r, 1, n, 1)), putchar('\n'); if(op == 4) write(qry_max(l, r, 1, n, 1)), putchar('\n'); }
return; } }
int main() { Acestar :: main(); return 0; }
|