QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#121572 | #4551. 数据交互 | NOI_AK_ME | 100 ✓ | 81ms | 46928kb | C++98 | 10.7kb | 2023-07-08 14:44:44 | 2023-07-08 14:44:46 |
Judging History
answer
#include<cstdio>
#include<algorithm>
#define L 1<<23
#define C (c=*A++)
#define ENS
#define NES
#define N 100007
using namespace std;
struct Athy {
long long x;
int y;
NES bool operator>(Athy &b) {
return x > b.x;
}
} G1[N << 2], G2[N << 1];
const long long NE = -1ll << 57;
int n, test, v1, v2, cnt, p[N], F[N], G[N], z[N], mx[N], s[N], fa[N], d[N], e[N], cd[N << 1],
nt[N << 1], et = 1, wt, ss[19];
char fl[L], *A = fl, fll[L], *B = fll;
long long a[N], b[N];
NES void read(int &x) {
char c;
for (x = 0; '0' > C || c > '9';)
;
while ('0' <= c && c <= '9')
x = (x << 3) + (x << 1) + c - 48, C;
}
NES void print(long long x) {
if (!x)
*B++ = (48);
else {
for (wt = 0; x; ss[++wt] = x % 10, x /= 10)
;
for (; wt; *B++ = (ss[wt--] + 48))
;
}
}
NES long long max(long long a, long long b, long long c) {
return a > b ? (a > c ? a : c) : (b > c ? b : c);
}
NES long long max(long long a, long long b) {
return a > b ? a : b;
}
struct node {
int n;
Athy *f;
NES void build(int x) {
int y;
for (f = &G1[v1 + 1], y = e[x]; y; y = nt[y])
if (!z[cd[y]])
f[F[cd[y]] = ++n].y = cd[y];
v1 += n + 3;
} NES void alter_F(int tmp, long long dt) {
int tp, LIA, x;
if (f[x = F[tmp]].x = dt, x > 1 && f[x] > f[x >> 1])
for (swap(F[f[x].y], F[f[x >> 1].y]), swap(f[x], f[x >> 1]), x >>= 1; x > 1 &&
f[x] > f[x >> 1]; swap(f[x], f[x >> 1]), swap(F[f[x].y], F[f[x >> 1].y]), x >>= 1)
;
else
while ((x << 1) <= n) {
if (f[tp = x << 1] > f[LIA = x])
LIA = tp;
if ((tp |= 1) <= n && f[tp] > f[LIA])
LIA = tp;
if (LIA != x)
swap(f[LIA], f[x]), swap(F[f[LIA].y], F[f[x].y]), x = LIA;
else
break;
}
}
} g[N];
struct val {
long long lm, ra, sa, sb, ia;
NES void fix(val &l, val &r) {
lm = max(l.lm, l.sa + r.lm), ra = max(r.sb + l.ra + r.sa, r.ra), ia = max(r.sb + l.ia, r.ia,
r.sb + l.ra + r.lm), sa = l.sa + r.sa, sb = l.sb + r.sb;
} NES void initial(int x) {
long long mx = g[x].f[1].x, sc = max(g[x].f[2].x, g[x].f[3].x);
ra = (lm = a[x] + mx) + b[x], ia = mx + sc + a[x] + b[x], sa = a[x], sb = b[x];
}
} T[N << 2];
struct tree {
int M, n, r;
val *t;
Athy *g;
NES void build(int x) {
int tmp = fa[z[x]], y;
for (n = p[x], M = 1; M <= n; M <<= 1)
;
for (t = &T[cnt + 1], g = &G2[v2 + 1], cnt += M << 1; x != tmp; x = fa[x])
for (y = e[x]; y; y = nt[y])
if (!z[cd[y]])
g[G[cd[y]] = ++r].y = cd[y];
for (v2 += r + 1, x = M + n + 1; x < (M << 1); t[x] = (val) {
NE, NE, 0, 0, NE
}, x++)
;
} NES void alter_G(int tmp, long long dt) {
int tp, LIA, x;
if (g[x = G[tmp]].x = dt, x > 1 && g[x] > g[x >> 1])
for (swap(G[g[x].y], G[g[x >> 1].y]), swap(g[x], g[x >> 1]), x >>= 1; x > 1 &&
g[x] > g[x >> 1]; swap(g[x], g[x >> 1]), swap(G[g[x].y], G[g[x >> 1].y]), x >>= 1)
;
else
while ((x << 1) <= r) {
if (g[tp = x << 1] > g[LIA = x])
LIA = tp;
if ((tp |= 1) <= r && g[tp] > g[LIA])
LIA = tp;
if (LIA != x)
swap(g[LIA], g[x]), swap(G[g[LIA].y], G[g[x].y]), x = LIA;
else
break;
}
} NES void alter_a(int x, int dt) {
for (t[x = p[x] + M].sb += dt, t[x].ra += dt, t[x].ia += dt, x >>= 1; x;
t[x].fix(t[x << 1], t[x << 1 | 1]), x >>= 1)
;
} NES void alter_c(int x) {
for (t[p[x] + M].initial(x), x = (p[x] + M) >> 1; x; t[x].fix(t[x << 1], t[x << 1 | 1]), x >>= 1)
;
}
} t[N];
struct ques {
int qx, qy, tmp, LIA;
NES void initial() {
int x, y, nt;
for (read(qx), read(qy), read(tmp), x = qx, y = qy; z[x] != z[y];
d[z[x]] > d[z[y]] ? x = fa[z[x]] : y = fa[z[y]])
;
LIA = d[x] > d[y] ? y : x;
if (x = qx, y = qy, b[x] += tmp, b[y] += tmp, a[LIA] += tmp, b[LIA] -= (long long)tmp << 1,
z[x] != z[LIA]) {
for (t[z[x]].alter_a(x, tmp); (nt = z[fa[x = z[x]]]) != z[LIA]; x = nt) {
long long tp;
if ((tp = max(t[x].t[1].lm, g[x].f[1].x)) != g[fa[x]].f[F[x]].x)
g[fa[x]].alter_F(x, tp);
if ((tp = max(t[x].t[1].ia, t[x].g[1].x)) != t[nt].g[G[x]].x)
t[nt].alter_G(x, tp);
b[fa[x]] += tmp, t[nt].alter_c(fa[x]);
}
long long tp;
if ((tp = max(t[x].t[1].lm, g[x].f[1].x)) != g[fa[x]].f[F[x]].x)
g[fa[x]].alter_F(x, tp);
if ((tp = max(t[x].t[1].ia, t[x].g[1].x)) != t[nt].g[G[x]].x)
t[nt].alter_G(x, tp);
if (b[x = fa[x]] += tmp, x != LIA)
t[nt].alter_c(x);
} else if (x != LIA)
t[z[x]].alter_a(x, tmp);
if (z[y] != z[LIA]) {
for (t[z[y]].alter_a(y, tmp); (nt = z[fa[y = z[y]]]) != z[LIA]; y = nt) {
long long tp;
if ((tp = max(t[y].t[1].lm, g[y].f[1].x)) != g[fa[y]].f[F[y]].x)
g[fa[y]].alter_F(y, tp);
if ((tp = max(t[y].t[1].ia, t[y].g[1].x)) != t[nt].g[G[y]].x)
t[nt].alter_G(y, tp);
b[fa[y]] += tmp, t[nt].alter_c(fa[y]);
}
long long tp;
if ((tp = max(t[y].t[1].lm, g[y].f[1].x)) != g[fa[y]].f[F[y]].x)
g[fa[y]].alter_F(y, tp);
if ((tp = max(t[y].t[1].ia, t[y].g[1].x)) != t[nt].g[G[y]].x)
t[nt].alter_G(y, tp);
if (b[y = fa[y]] += tmp, y != LIA)
t[nt].alter_c(y);
} else if (y != LIA)
t[z[y]].alter_a(y, tmp);
for (t[x = z[LIA]].alter_c(LIA); nt = z[fa[x]]; x = nt) {
long long tp, l1 = g[fa[x]].f[1].x, l2 = t[nt].g[1].x, l3 = max(g[fa[x]].f[2].x, g[fa[x]].f[3].x);
if ((tp = max(t[x].t[1].lm, g[x].f[1].x)) != g[fa[x]].f[F[x]].x)
g[fa[x]].alter_F(x, tp);
if ((tp = max(t[x].t[1].ia, t[x].g[1].x)) != t[nt].g[G[x]].x)
t[nt].alter_G(x, tp);
if (g[fa[x]].f[1].x != l1 || (max(g[fa[x]].f[2].x, g[fa[x]].f[3].x)) != l3)
t[nt].alter_c(fa[x]);
else if (t[nt].g[1].x == l2)
break;
}
} NES void del() {
int x, y, nt;
for (x = qx, y = qy, tmp = -tmp; z[x] != z[y]; d[z[x]] > d[z[y]] ? x = fa[z[x]] : y = fa[z[y]])
;
LIA = d[x] > d[y] ? y : x;
if (x = qx, y = qy, b[x] += tmp, b[y] += tmp, a[LIA] += tmp, b[LIA] -= (long long)tmp << 1,
z[x] != z[LIA]) {
for (t[z[x]].alter_a(x, tmp); (nt = z[fa[x = z[x]]]) != z[LIA]; x = nt) {
long long tp;
if ((tp = max(t[x].t[1].lm, g[x].f[1].x)) != g[fa[x]].f[F[x]].x)
g[fa[x]].alter_F(x, tp);
if ((tp = max(t[x].t[1].ia, t[x].g[1].x)) != t[nt].g[G[x]].x)
t[nt].alter_G(x, tp);
b[fa[x]] += tmp, t[nt].alter_c(fa[x]);
}
long long tp;
if ((tp = max(t[x].t[1].lm, g[x].f[1].x)) != g[fa[x]].f[F[x]].x)
g[fa[x]].alter_F(x, tp);
if ((tp = max(t[x].t[1].ia, t[x].g[1].x)) != t[nt].g[G[x]].x)
t[nt].alter_G(x, tp);
if (b[x = fa[x]] += tmp, x != LIA)
t[nt].alter_c(x);
} else if (x != LIA)
t[z[x]].alter_a(x, tmp);
if (z[y] != z[LIA]) {
for (t[z[y]].alter_a(y, tmp); (nt = z[fa[y = z[y]]]) != z[LIA]; y = nt) {
long long tp;
if ((tp = max(t[y].t[1].lm, g[y].f[1].x)) != g[fa[y]].f[F[y]].x)
g[fa[y]].alter_F(y, tp);
if ((tp = max(t[y].t[1].ia, t[y].g[1].x)) != t[nt].g[G[y]].x)
t[nt].alter_G(y, tp);
b[fa[y]] += tmp, t[nt].alter_c(fa[y]);
}
long long tp;
if ((tp = max(t[y].t[1].lm, g[y].f[1].x)) != g[fa[y]].f[F[y]].x)
g[fa[y]].alter_F(y, tp);
if ((tp = max(t[y].t[1].ia, t[y].g[1].x)) != t[nt].g[G[y]].x)
t[nt].alter_G(y, tp);
if (b[y = fa[y]] += tmp, y != LIA)
t[nt].alter_c(y);
} else if (y != LIA)
t[z[y]].alter_a(y, tmp);
for (t[x = z[LIA]].alter_c(LIA); nt = z[fa[x]]; x = nt) {
long long tp, l1 = g[fa[x]].f[1].x, l2 = t[nt].g[1].x, l3 = max(g[fa[x]].f[2].x, g[fa[x]].f[3].x);
if ((tp = max(t[x].t[1].lm, g[x].f[1].x)) != g[fa[x]].f[F[x]].x)
g[fa[x]].alter_F(x, tp);
if ((tp = max(t[x].t[1].ia, t[x].g[1].x)) != t[nt].g[G[x]].x)
t[nt].alter_G(x, tp);
if (g[fa[x]].f[1].x != l1 || (max(g[fa[x]].f[2].x, g[fa[x]].f[3].x)) != l3)
t[nt].alter_c(fa[x]);
else if (t[nt].g[1].x == l2)
break;
}
}
} q[N];
NES void dfs(int x, int fe) {
s[x] = 1;
for (int y = e[x]; y; y = nt[y])
if (y != fe &&
(fa[cd[y]] = x, d[cd[y]] = d[x] + 1, dfs(cd[y], y ^ 1), s[x] += s[cd[y]], s[cd[y]] > s[mx[x]]))
mx[x] = cd[y];
}
NES void vfs(int x, int chain) {
if (z[x] = chain, mx[x])
p[mx[x]] = p[x] + 1, vfs(mx[x], chain);
else
t[chain].build(x);
g[x].build(x);
for (int y = e[x]; y; y = nt[y])
if (!z[cd[y]])
vfs(cd[y], cd[y]);
}
ENS main() {
int i, x, y, ti;
char c;
for (fl[fread(fl, 1, L, stdin)] = EOF, read(n), read(test), i = 1; i < n;
read(x), read(y), cd[++et] = y, nt[et] = e[x], e[x] = et, cd[++et] = x, nt[et] = e[y], e[y] = et,
i++)
;
for (dfs(1, 0), vfs(1, 1), ti = 1; ti <= test;
ti++, print(max(t[1].g[1].x, t[1].t[1].ia)), *B++ = '\n') {
while (C != '+' && c != '-')
;
if (c == '+')
q[ti].initial();
else
read(x), q[x].del();
}
fwrite(fll, 1, B - fll, stdout);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 10
Accepted
time: 1ms
memory: 15672kb
input:
23 45 2 1 2 17 1 4 12 1 22 7 3 7 6 2 14 9 5 1 21 23 18 1 2 3 13 3 20 6 4 11 1 15 4 19 17 21 7 9 8 3 4 16 8 10 + 16 14 42 + 2 18 73 + 9 19 100 + 17 6 8 + 20 22 78 + 10 23 67 + 15 16 7 + 14 10 83 - 2 + 8 22 77 + 4 13 87 + 2 4 50 + 23 18 92 + 3 23 92 + 5 10 35 + 5 1 27 - 13 - 3 + 14 14 15 + 18 3 34 + 3...
output:
42 115 215 223 301 368 375 458 385 462 549 599 691 783 818 845 753 653 668 702 762 786 694 695 765 839 864 914 1010 1038 1079 1158 1240 1315 1341 1291 1346 1398 1494 1499 1458 1513 1564 1581 1662
result:
ok 45 lines
Test #2:
score: 10
Accepted
time: 1ms
memory: 19720kb
input:
78 63 10 4 29 38 9 19 65 55 13 31 42 23 7 15 13 1 28 34 6 4 2 47 77 54 25 1 19 55 57 48 2 35 51 7 24 1 11 23 75 32 25 67 37 69 19 21 62 74 21 27 8 56 3 1 22 58 33 14 36 40 1 5 4 22 22 32 78 76 66 65 70 64 26 1 1 29 62 64 43 15 36 49 19 46 5 73 41 53 65 71 61 46 68 39 1 17 14 7 33 62 60 63 7 9 18 28 ...
output:
904 1354 1888 2639 1735 2139 2372 3315 3842 4313 5294 6123 5142 6033 6997 7422 8061 8676 8272 8458 8492 9381 8931 9207 8736 8900 9564 10073 10469 10581 11570 11589 12046 12899 13128 13820 13881 14778 14545 15307 15195 16095 17081 17910 18298 18886 18886 18886 19808 20687 21035 21581 22518 23305 2404...
result:
ok 63 lines
Test #3:
score: 10
Accepted
time: 1ms
memory: 19676kb
input:
89 97 12 21 35 12 27 51 66 72 9 5 51 56 65 52 60 75 34 53 11 30 54 49 89 84 33 43 66 60 15 10 44 47 78 72 82 42 32 13 29 81 6 2 22 18 17 6 40 21 15 23 8 7 11 16 38 2 14 1 15 29 79 84 1 4 25 9 12 39 49 12 15 37 59 24 65 71 46 73 17 20 62 11 87 40 33 28 12 4 5 3 69 36 48 36 42 9 24 45 18 14 27 1 52 48...
output:
1089540208 1759220800 2546484532 4230712681 6371901183 7774014584 5632826082 5925957371 7952282056 8074251425 9519004213 8849323621 10415748675 10690169447 11813499691 11906187227 13991001411 11964676726 12578914249 12578914249 14515987793 15416467423 13850042369 11765228185 12726918299 13983101093 ...
result:
ok 97 lines
Test #4:
score: 10
Accepted
time: 62ms
memory: 43888kb
input:
100000 100000 52336 52334 53869 53867 16732 19792 65544 65549 27367 23422 67961 67956 49048 33887 60632 60637 93826 93822 25325 8799 21288 10911 43533 46232 20651 19064 56161 56163 46617 5394 12318 49023 71818 71819 47059 3840 10278 12051 13859 13915 24747 3591 23064 48399 370 1304 11451 28543 11004...
output:
83 633 708 783 1548 2026 2691 3419 4280 4374 5121 6031 6947 7582 8072 8180 8391 8462 8615 9456 9723 10613 11355 12003 12716 12756 13385 13779 13802 14780 14829 15437 15872 16691 17438 18428 19351 19698 19799 20385 20472 20960 21252 21396 21413 21511 21858 21872 22026 22184 22624 23300 24193 25193 25...
result:
ok 100000 lines
Test #5:
score: 10
Accepted
time: 81ms
memory: 45840kb
input:
95677 98173 3340 3973 94173 94170 35297 5013 58731 58728 67614 67618 86454 86451 25595 15771 77275 77277 25332 20520 46497 46498 25941 35331 42294 42292 50457 50452 89704 89701 51426 51424 67050 67045 58437 58436 73785 73782 78312 78317 21909 3074 58903 58899 14297 25726 50060 50064 63798 63794 8628...
output:
1900644205 1946927684 2759299849 3390928670 3627190657 4340157873 6384135762 7966963280 8878768263 10810461477 12698241005 13683799636 15556286164 17160972456 18338672233 19668925940 20133417800 21047167002 22424995453 23350540833 25291671140 25496566536 27386151399 28769001460 30038231490 302037243...
result:
ok 98173 lines
Test #6:
score: 10
Accepted
time: 60ms
memory: 43180kb
input:
99998 99997 363 14746 354 134 58807 58803 99668 99667 87741 87745 1025 1590 2096 1793 65402 65403 56473 56475 73129 73127 94399 94404 71937 71933 89969 89971 1543 7005 1776 13484 96923 96922 47845 47843 90728 90726 8450 6598 81818 81815 36987 36985 51102 51107 97018 97017 7093 8058 59277 59281 36428...
output:
928 1079 1301 1410 1672 2055 2336 3261 3747 4195 4676 5315 5683 6472 6804 7490 7594 8217 8568 9270 9925 9972 10947 11360 11888 12740 12768 12983 13293 13915 14258 14501 14566 14742 15617 16197 17048 17763 18395 19286 19497 19749 20227 21040 21647 22067 22337 22550 22993 23220 23546 24161 24472 24824...
result:
ok 99997 lines
Test #7:
score: 10
Accepted
time: 76ms
memory: 45716kb
input:
98134 97241 69878 69877 18311 9613 90751 90753 35760 17785 69470 69475 3792 10137 23917 26686 94684 94680 95049 95047 88474 88477 28924 48715 79475 79478 97977 97981 12335 7320 80471 80468 69488 69492 94740 94742 96936 96932 29480 37380 28265 45159 22779 20652 64959 64957 69176 69175 80119 80114 185...
output:
2057533475 2293411568 3897759032 3909264883 2304917419 2943495532 3948680786 4236363587 6195907936 4236363587 2178830112 1540251999 1807983129 2108758108 2097252257 2718708571 2812846322 2980211652 2812846322 2576968229 4031194380 4360342160 3355156906 4715945863 5745187483 5847919006 6589762671 794...
result:
ok 97241 lines
Test #8:
score: 10
Accepted
time: 52ms
memory: 46208kb
input:
100000 100000 23954 12295 47529 47524 91373 91378 15638 6925 15454 32962 9038 5615 16793 6606 90092 90088 77082 77080 18944 8974 62796 62797 22321 17972 52896 52898 51414 51417 48800 48804 80453 80458 45838 45833 1027 1838 93017 93021 54313 54310 1139 272 16249 15479 81196 81192 97845 97844 48839 48...
output:
389 807 1120 1996 2269 3071 3303 4293 4749 4784 5439 6325 6645 6872 6640 7108 7224 6756 6982 7572 8519 9480 10359 9970 9514 10033 9147 9987 9674 10600 10957 10438 10735 10860 11520 12031 12940 13164 13290 14091 14285 15149 15646 15818 15646 16390 17252 18049 18903 19211 20081 20908 21366 21331 21760...
result:
ok 100000 lines
Test #9:
score: 10
Accepted
time: 66ms
memory: 46928kb
input:
99938 99173 29210 29209 24805 24806 18902 18901 84977 84976 21151 21150 33914 33911 26605 26606 15309 14605 9229 7297 12797 10600 70506 70511 19355 19360 39615 39611 5058 15762 33588 33587 50269 50264 29467 29470 25154 25151 34950 34947 77168 77166 51154 51153 93763 93761 81266 81270 29625 29629 789...
output:
1986671667 3785768400 5121718492 6046130937 4247034204 6214576849 8118400899 6150858254 8227376394 6150858254 8051397217 8887528821 9337071717 10106783275 10995952205 12130975145 13255736624 12331324179 13139730970 13592218933 12457195993 11568027063 12019696759 12500941036 12913181587 15036478591 1...
result:
ok 99173 lines
Test #10:
score: 10
Accepted
time: 79ms
memory: 43928kb
input:
100000 100000 49975 13565 10650 36102 12276 5900 88308 88304 7795 34194 58024 58027 22072 12288 54859 54863 93061 93057 24812 8732 731 1001 7782 9774 63659 63655 87209 87205 69971 69972 54531 54534 9102 46324 14825 32543 3555 32704 18786 11252 11218 6948 54503 54498 4133 14042 54680 54677 52955 5295...
output:
7 66 59 494 1260 1759 1700 1201 2069 2899 3249 3886 4068 3886 3536 4419 5187 6067 6146 6399 6763 7131 7574 7787 7419 7206 6338 6970 7843 8258 7492 7958 8016 8750 9540 9482 10125 9242 9406 9854 10276 10276 10276 10648 10412 10568 11491 12411 12628 12828 11955 12700 13080 13853 14320 14443 15012 15263...
result:
ok 100000 lines
Extra Test:
score: 0
Extra Test Passed