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
|
#include <bits/stdc++.h>
const int MAXN = 500010; const int INIT_VAL = 1001; const int INF = 2000000000; const int OUT_LEN = 10000000;
inline int read() { char c = getchar(); while (!isdigit(c) && c != '-') c = getchar(); int x = 0, f = 1; if (c == '-') f = -1; else x = c - '0'; while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c ^ '0'); return x * f; }
char obuf[OUT_LEN], *oh = obuf;
inline void writeChar(const char c) { if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf; *oh++ = c; }
template<class T> inline void write(T x) { static int buf[30], cnt; if (x == 0) { writeChar(48); } else { if (x < 0) writeChar('-'), x = -x; for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48; while (cnt) writeChar(buf[cnt--]); } }
inline void flush() { fwrite(obuf, 1, oh - obuf, stdout); }
struct Node *null;
struct Node { Node *lc, *rc; int key, rank, rev, tag, size; int sum, lSum, rSum, maxSum;
Node(int key = 0) : key(key), rank(rand()), lc(null), rc(null), rev(0), tag(INIT_VAL), size(1) { sum = lSum = rSum = maxSum = key; }
inline void maintain() { size = lc->size + rc->size + 1; sum = lc->sum + rc->sum + key; lSum = std::max(lc->lSum, lc->sum + key + std::max(0, rc->lSum)); rSum = std::max(rc->rSum, rc->sum + key + std::max(0, lc->rSum)); maxSum = std::max(0, lc->rSum) + key + std::max(0, rc->lSum); maxSum = std::max(maxSum, std::max(lc->maxSum, rc->maxSum)); }
inline void cover(int v) { if (this == null) return; key = v, sum = v * size; lSum = rSum = maxSum = std::max(sum, v); tag = v; }
inline void reverse() { if (this == null) return; std::swap(lc, rc); std::swap(lSum, rSum); rev ^= 1; }
inline void pushDown() { if (this == null) return; if (tag != INIT_VAL) { lc->cover(tag), rc->cover(tag); tag = INIT_VAL; } if (rev) { lc->reverse(), rc->reverse(); rev = 0; } } } data[MAXN], *pool[MAXN], *rt;
int poolTop, dataTop;
typedef std::pair<Node *, Node *> Pair;
inline Node *newNode(int key) { Node *u; if (poolTop) u = pool[poolTop--]; else u = &data[dataTop++]; *u = Node(key); return u; }
inline void remove(Node *u) { pool[++poolTop] = u; }
inline Node *build(int *a, int n) { static Node *stack[MAXN], *u, *pre; int top = 0; for (register int i = 1; i <= n; i++) { u = newNode(a[i]); pre = null; while (top && stack[top]->rank > u->rank) { stack[top]->maintain(); pre = stack[top]; stack[top--] = null; } if (top) stack[top]->rc = u; u->lc = pre; stack[++top] = u; } while (top) stack[top--]->maintain(); return stack[1]; }
inline Node *merge(Node *u, Node *v) { if (u == null) return v; if (v == null) return u; if (u->rank < v->rank) { u->pushDown(); u->rc = merge(u->rc, v); u->maintain(); return u; } else { v->pushDown(); v->lc = merge(u, v->lc); v->maintain(); return v; } }
inline Pair split(Node *u, int k) { if (u == null) return Pair(null, null); Pair t; u->pushDown(); if (u->lc->size >= k) { t = split(u->lc, k); u->lc = t.second, t.second = u; } else { t = split(u->rc, k - u->lc->size - 1); u->rc = t.first, t.first = u; } u->maintain(); return t; }
inline void recycle(Node *u) { if (u == null) return; recycle(u->lc), recycle(u->rc); remove(u); }
int pos, cnt;
inline void insert(int *a) { for (register int i = 1; i <= cnt; i++) a[i] = read(); Node *nr = build(a, cnt); Pair t = split(rt, pos); rt = merge(merge(t.first, nr), t.second); }
inline void remove() { Pair t1 = split(rt, pos + cnt - 1), t2 = split(t1.first, pos - 1); recycle(t2.second); rt = merge(t2.first, t1.second); }
inline void reverse() { Pair t1 = split(rt, pos + cnt - 1), t2 = split(t1.first, pos - 1); t2.second->reverse(); rt = merge(merge(t2.first, t2.second), t1.second); }
inline void makeSame() { int v = read(); Pair t1 = split(rt, pos + cnt - 1), t2 = split(t1.first, pos - 1); t2.second->cover(v); rt = merge(merge(t2.first, t2.second), t1.second); }
inline void querySum() { Pair t1 = split(rt, pos + cnt - 1), t2 = split(t1.first, pos - 1); write(t2.second->sum), writeChar('\n'); rt = merge(merge(t2.first, t2.second), t1.second); }
inline void queryMaxSum() { write(rt->maxSum), writeChar('\n'); }
void solve() { static int n, m, a[MAXN];
n = read(), m = read(); for (register int i = 1; i <= n; i++) a[i] = read(); null = newNode(-INF); null->size = null->sum = 0; rt = build(a, n); char cmd[20]; for (register int i = 1; i <= m; i++) { scanf("%s", cmd); if (cmd[2] != 'X') pos = read(), cnt = read(); switch (cmd[2]) { case 'S' : insert(a); break; case 'L' : remove(); break; case 'V' : reverse(); break; case 'T' : querySum(); break; case 'X' : queryMaxSum(); break; case 'K' : makeSame(); break; } } }
int main() { srand(495); solve(); flush(); return 0; }
|