QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#451511 | #7872. 崩坏天际线 | zqs | 0 | 1068ms | 19748kb | C++14 | 5.7kb | 2024-06-23 15:49:51 | 2024-06-23 15:49:51 |
Judging History
answer
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
using std::min; using std::swap;
const int mod = 998244353, B = 230, N = 50005;
inline void add(int &x, const int y) {if ((x += y) >= mod) x -= mod;}
inline int plus(const int x, const int y) {return x + y < mod ? x + y : x + y - mod;}
int al[N], ar[N], op[N], p2[N], a[B + 5], tme[B + 5], n, q, ans;
int bl[B * B + 5], br[B * B + 5], bp[B * B + 5], cl[B + 5], cr[B + 5], stk[B + 5], cnt;
struct node {int ls, rs, sum, tag;} tr[2500005];
int id[N], pre[N], nxt[N], len[N];
bool vis[N], ban[N];
std::vector<int> vec[50005];
struct RMQ {
int st[17][50005], Log[50005];
void clear() {memset(st[0], 0x3f, sizeof st[0]);}
void set(int x, int v) {st[0][x] = v;}
void init() {
for (int i = 2; i <= n; ++ i) Log[i] = Log[i >> 1] + 1;
for (int i = 1; i <= 15; ++ i)
for (int j = 1; j + (1 << i) - 1 <= n; ++ j)
st[i][j] = min(st[i - 1][j], st[i - 1][j + (1 << i - 1)]);
}
int query(int l, int r) {
if (l > r) return q + 1;
int k = Log[r - l + 1];
return min(st[k][l], st[k][r - (1 << k) + 1]);
}
} rmq;
int prob[N], rt1[N], rt2[N], tot;
void spread(int p) {
if (!tr[p].tag) return;
int ls = tr[p].ls, rs = tr[p].rs, tmp = tr[p].tag; tr[p].tag = 0;
if (ls) tr[ls].sum = 1ll * tr[ls].sum * p2[tmp] % mod, tr[ls].tag += tmp;
if (rs) tr[rs].sum = 1ll * tr[rs].sum * p2[tmp] % mod, tr[rs].tag += tmp;
}
void insert(int &p, int l, int r, int x, int v) {
if (!p) p = ++ tot, tr[p].ls = tr[p].rs = tr[p].sum = tr[p].tag = 0;
add(tr[p].sum, v); if (l == r) return;
int mid = l + r >> 1; spread(p);
x <= mid ? insert(tr[p].ls, l, mid, x, v) : insert(tr[p].rs, mid + 1, r, x, v);
}
void pushup(int p) {if (p) tr[p].sum = plus(tr[tr[p].ls].sum, tr[tr[p].rs].sum);}
void split(int x, int &y, int l, int r, int k) {
if (!x || r <= k) return;
if (k < l) {y = x, x = 0; return;}
spread(x);
int mid = l + r >> 1, z = 0; tr[y = ++ tot].tag = 0;
if (k <= mid) {
split(tr[x].ls, z, l, mid, k), tr[y].ls = z, tr[y].rs = tr[x].rs, tr[x].rs = 0;
} else split(tr[x].rs, z, mid + 1, r, k), tr[y].ls = 0, tr[y].rs = z;
pushup(x), pushup(y);
}
void query(int p, int l, int r, int x) {
if (!p) return;
if (l == r) {ans = (ans + 1ll * abs(x - l) * tr[p].sum) % mod; return;}
spread(p);
query(tr[p].ls, l, l + r >> 1, x), query(tr[p].rs, (l + r >> 1) + 1, r, x);
}
void solve(int st) {
memset(pre, 0, sizeof pre), memset(nxt, 0, sizeof nxt);
memset(prob, 0, sizeof prob), memset(vis, 0, sizeof vis);
memset(ban, 0, sizeof ban), rmq.clear();
for (int i = q; i >= st; -- i) if (op[i] == 2) rmq.set(al[i], i);
rmq.init(), tot = 0;
for (int i = 1; i <= n; ++ i) vec[i].clear();
for (int i = 1; i <= cnt; ++ i) {
int fst = rmq.query(bl[i] + 1, br[i] - 1);
if (fst > q) ans = (ans + 1ll * bp[i] * (br[i] - bl[i])) % mod;
else vec[al[fst]].emplace_back(i), insert(rt1[al[fst]], 1, n, bl[i], bp[i]), insert(rt2[al[fst]], 1, n, br[i], bp[i]);
}
for (int i = st; i <= q; ++ i)
if (op[i] == 2) {if (vis[al[i]]) ban[i] = true; else vis[al[i]] = true;}
for (int i = 1, j = 0; i <= n; ++ i) {
rt1[i] = rt2[i] = 0;
if (vis[i]) pre[i] = j, nxt[j] = i, len[j] = i - j, j = i;
}
pre[0] = nxt[0] = 0;
for (int i = q; i >= st; -- i) if (op[i] == 2 && !ban[i]) {
int x = al[i];
if (pre[x]) nxt[pre[x]] = nxt[x];
if (nxt[x]) pre[nxt[x]] = pre[x];
}
for (int i = st; i <= q; ++ i) if (op[i] == 2 && !ban[i]) {
int x = al[i], l = pre[x], r = nxt[x];
if (r) {
split(rt1[r], rt1[x], 1, n, x - 1), swap(rt1[r], rt1[x]);
if (rt1[x]) tr[rt1[x]].sum = 1ll * tr[rt1[x]].sum * p2[1] % mod, ++ tr[rt1[x]].tag;
}
if (l) {
split(rt2[l], rt2[x], 1, n, x);
if (rt2[x]) tr[rt2[x]].sum = 1ll * tr[rt2[x]].sum * p2[1] % mod, ++ tr[rt2[x]].tag;
}
prob[x] = (1ll * prob[l] * p2[1] + tr[rt1[x]].sum) % mod;
prob[l] = (1ll * prob[l] * p2[1] + tr[rt2[x]].sum) % mod;
for (int j : vec[x]) {
insert(rt1[x], 1, n, bl[j], 1ll * bp[j] * p2[1] % mod);
insert(rt2[x], 1, n, br[j], 1ll * bp[j] * p2[1] % mod);
}
}
for (int i = 1; i <= n; ++ i) {
if (prob[i]) ans = (ans + 1ll * len[i] * prob[i]) % mod;
if (rt1[i]) query(rt1[i], 1, n, i);
if (rt2[i]) query(rt2[i], 1, n, i);
}
}
int main() {
scanf("%d%d", &n, &q);
p2[0] = 1, p2[1] = mod + 1 >> 1;
for (int i = 1; i <= q; ++ i) {
scanf("%d%d", op + i, al + i); if (op[i] == 1) scanf("%d", ar + i);
}
for (int i = 2; i <= q; ++ i) p2[i] = 1ll * p2[1] * p2[i - 1] % mod;
for (int i = 1; i <= (q + B - 1) / B; ++ i) {
int st = (i - 1) * B + 1, ed = min(q, i * B), m = 0;
cnt = 0;
for (int j = ed; j >= st; -- j) {
if (op[j] == 2) {
int pos = m + 1;
for (int k = 1; k <= m; ++ k)
if (a[k] > al[j]) {pos = k; break;}
else if (a[k] == al[j]) {pos = 0, tme[k] = j; break;}
if (pos) {
++ m;
for (int k = m; k > pos; -- k) a[k] = a[k - 1], tme[k] = tme[k - 1];
a[pos] = al[j], tme[pos] = j;
}
} else {
int l = m + 1, r = 0;
for (int k = 1; k <= m; ++ k) if (a[k] > al[j]) {l = k; break;}
for (int k = m; k; -- k) if (a[k] < ar[j]) {r = k; break;}
if (l > r) {bl[++ cnt] = al[j], br[cnt] = ar[j], bp[cnt] = 1; continue;}
for (int k = l, top = 0; k <= r; ++ k) {
while (top && tme[stk[top]] > tme[k]) -- top;
stk[++ top] = k, cl[k] = top;
}
for (int k = r, top = 0; k >= l; -- k) {
while (top && tme[stk[top]] > tme[k]) -- top;
stk[++ top] = k, cr[k] = top;
}
for (int k = l; k < r; ++ k)
bl[++ cnt] = a[k], br[cnt] = a[k + 1], bp[cnt] = p2[cl[k] + cr[k + 1]];
bl[++ cnt] = al[j], br[cnt] = a[l], bp[cnt] = p2[cr[l]];
bl[++ cnt] = a[r], br[cnt] = ar[j], bp[cnt] = p2[cl[r]];
}
}
solve(ed + 1);
}
printf("%d", ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 10
Accepted
time: 3ms
memory: 12540kb
input:
500 500 1 119 258 2 134 2 417 2 176 2 61 2 60 2 110 1 335 336 1 96 111 2 202 1 138 344 2 358 2 134 1 29 54 1 73 381 1 179 495 2 490 2 418 2 186 2 183 1 168 340 2 78 1 15 27 2 373 1 245 498 1 372 495 2 244 2 63 1 174 490 2 282 2 417 1 272 408 1 109 134 2 303 2 345 1 238 401 1 36 480 1 21 483 2 10 1 3...
output:
855279801
result:
ok single line: '855279801'
Test #2:
score: 0
Accepted
time: 2ms
memory: 12528kb
input:
495 466 1 35 393 2 236 1 4 335 2 455 1 36 470 1 23 61 2 195 2 109 2 451 1 282 491 2 238 2 117 2 468 1 2 60 1 439 487 2 238 1 209 294 2 321 2 309 1 113 183 2 409 2 87 2 130 2 124 2 176 2 448 2 379 1 181 446 2 146 2 450 1 171 423 2 355 2 332 1 123 387 1 151 269 1 17 417 2 122 1 324 494 1 265 439 2 225...
output:
294468977
result:
ok single line: '294468977'
Test #3:
score: 0
Accepted
time: 2ms
memory: 12144kb
input:
441 467 2 180 1 51 344 2 180 1 16 345 1 39 419 1 64 432 2 176 1 35 372 2 426 1 8 415 1 1 439 1 17 430 2 433 1 89 369 1 83 353 2 292 1 1 421 1 63 430 1 33 345 1 69 421 1 49 373 1 77 343 1 24 393 1 90 375 1 8 425 2 322 2 61 2 112 2 209 1 39 406 1 12 426 1 29 430 1 50 374 1 47 394 1 9 387 2 234 1 19 35...
output:
526117259
result:
ok single line: '526117259'
Test #4:
score: 0
Accepted
time: 0ms
memory: 12492kb
input:
500 500 2 442 1 12 414 1 40 435 2 138 1 79 448 1 16 464 2 163 1 94 492 2 97 2 335 1 7 452 1 25 474 1 78 442 2 286 1 93 430 1 78 438 2 469 2 354 2 270 2 292 2 108 2 301 1 100 480 2 258 1 17 487 2 2 2 409 2 385 2 338 1 83 454 1 41 490 1 95 475 1 43 442 1 66 445 2 406 2 168 1 10 406 2 330 2 20 1 90 491...
output:
810270061
result:
ok single line: '810270061'
Test #5:
score: -10
Wrong Answer
time: 0ms
memory: 12472kb
input:
500 500 1 29 407 1 89 480 1 31 497 1 28 494 1 21 492 1 91 465 1 13 467 1 89 425 1 22 444 1 20 430 1 48 445 1 33 441 1 61 435 1 69 427 1 89 485 1 90 446 1 23 488 1 6 424 1 76 425 1 36 460 1 16 421 1 20 500 1 3 487 1 99 481 1 53 412 1 96 456 1 39 436 1 28 436 1 4 409 1 9 486 1 22 484 1 88 413 1 26 467...
output:
419428993
result:
wrong answer 1st lines differ - expected: '419428992', found: '419428993'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Wrong Answer
Test #13:
score: 40
Accepted
time: 1068ms
memory: 19748kb
input:
50000 50000 1 24367 33007 1 14396 42256 1 6375 22327 1 7892 42501 1 10100 37998 1 6284 48524 1 7357 18164 1 16200 46424 1 18972 34131 1 16849 32591 1 1917 3018 1 19897 30272 1 45044 45753 1 18999 25448 1 5167 31033 1 6182 35335 1 7270 37270 1 12651 39965 1 28896 38022 1 13853 35426 1 35516 48244 1 1...
output:
733099543
result:
ok single line: '733099543'
Test #14:
score: 0
Accepted
time: 842ms
memory: 17800kb
input:
49951 43686 1 21796 23464 1 29304 46959 1 5034 41719 1 7779 35334 1 27566 36486 1 20347 26165 1 12508 30387 1 18363 20335 1 8540 21417 1 5728 49086 1 46038 47603 1 10371 15910 1 27293 43572 1 18915 45279 1 7388 48342 1 6802 43746 1 4361 40049 1 41177 43375 1 23287 48354 1 37097 41733 1 2406 11638 1 ...
output:
792296531
result:
ok single line: '792296531'
Test #15:
score: 0
Accepted
time: 412ms
memory: 15656kb
input:
49914 43874 1 8935 40963 1 4425 44317 1 1769 45855 1 2436 40257 1 1778 47216 1 383 42149 1 5398 40732 1 1079 43346 1 6578 41660 1 9689 45985 1 6131 42681 1 8862 47431 1 3979 46189 1 6456 43485 1 2028 46574 1 3802 47787 1 6990 41659 1 9221 41204 1 2271 43554 1 8018 45280 1 9344 43917 1 6623 41152 1 7...
output:
831211412
result:
ok single line: '831211412'
Test #16:
score: 0
Accepted
time: 597ms
memory: 19276kb
input:
50000 50000 1 1310 49344 1 5755 44255 1 3582 41465 1 6800 42160 1 1651 44584 1 7967 44410 1 3116 48795 1 1855 41120 1 27 42294 1 2455 49629 1 4196 42487 1 7070 44542 1 136 42053 1 5715 44222 1 8794 43115 1 4048 45579 1 635 46703 1 9246 41055 1 3678 41276 1 4871 41715 1 1659 44679 1 1639 46392 1 2479...
output:
316801136
result:
ok single line: '316801136'
Test #17:
score: -40
Wrong Answer
time: 511ms
memory: 18476kb
input:
50000 50000 1 8731 40028 1 6575 43815 1 9558 42476 1 7269 47567 1 6597 45567 1 7753 49129 1 9892 47319 1 9438 45710 1 8688 46209 1 75 43653 1 8918 44467 1 2751 43343 1 4433 45172 1 8062 40732 1 3342 41158 1 615 45475 1 7497 44843 1 9201 48262 1 3063 44796 1 9294 48709 1 382 46129 1 5935 48889 1 1195...
output:
663130071
result:
wrong answer 1st lines differ - expected: '680677335', found: '663130071'
Subtask #4:
score: 0
Skipped
Dependency #1:
0%