QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#424892 | #943. Dynamic Diameter | james1BadCreeper | 0 | 4ms | 12044kb | C++17 | 3.1kb | 2024-05-29 19:38:03 | 2024-05-29 19:38:03 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long i64;
const int N = 1e5 + 5;
int n, q, sz[N]; i64 WW;
int mi[17][N], num, dfn[N], idx[N];
vector<pair<int, i64>> G[N];
struct Edge {
int v; i64 w;
} E[N];
i64 C[N];
inline void add(int x, i64 k) { for (; x <= n; x += x & -x) C[x] += k; }
inline void add(int l, int r, i64 k) { add(l, k); add(r + 1, -k); }
inline i64 sum(int x) { i64 r = 0; for (; x; x -= x & -x) r += C[x]; return r; }
inline int get(int x, int y) { return dfn[x] < dfn[y] ? x : y; }
void dfs(int x, int ff) {
mi[0][dfn[x] = ++num] = ff; sz[x] = 1; idx[num] = x;
for (auto [y, w] : G[x]) if (y != ff) {
E[w].v = y; dfs(y, x);
add(dfn[y], dfn[y] + sz[y] - 1, E[w].w);
sz[x] += sz[y];
}
cout << x << " " << sz[x] << "\n";
}
inline int lca(int u, int v) {
if (u == v) return u;
if ((u = dfn[u]) > (v = dfn[v])) swap(u, v);
int k = __lg(v - u++);
return get(mi[k][u], mi[k][v - (1 << k) + 1]);
}
inline i64 dist(int u, int v) {
return sum(dfn[u]) + sum(dfn[v]) - 2 * sum(dfn[lca(u, v)]);
}
struct Node {
int a, b; i64 ans;
friend Node operator+ (const Node &a, const Node &b) {
Node c = (a.ans > b.ans ? a : b); i64 l;
if ((l = dist(a.a, b.a)) > c.ans) c.ans = l, c.a = a.a, c.b = b.a;
if ((l = dist(a.b, b.a)) > c.ans) c.ans = l, c.a = a.b, c.b = b.a;
if ((l = dist(a.a, b.b)) > c.ans) c.ans = l, c.a = a.a, c.b = b.b;
if ((l = dist(a.b, b.b)) > c.ans) c.ans = l, c.a = a.b, c.b = b.b;
return c;
}
} T[N * 4];
void build(int o, int l, int r) {
if (l == r) return T[o] = Node{idx[l], idx[l], 0}, void();
int mid = l + r >> 1;
build(o << 1, l, mid); build(o << 1 | 1, mid + 1, r);
T[o] = T[o << 1] + T[o << 1 | 1];
}
void update(int o, int l, int r, int x, int y) {
if (x <= l && r <= y) return;
int mid = l + r >> 1;
if (x <= mid) update(o << 1, l, mid, x, y);
if (mid < y) update(o << 1 | 1, mid + 1, r, x, y);
T[o] = T[o << 1] + T[o << 1 | 1];
}
int main(void) {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> q >> WW;
for (int i = 1; i < n; ++i) {
int u, v; i64 w; cin >> u >> v >> w; E[i].w = w;
G[u].emplace_back(v, i); G[v].emplace_back(u, i);
}
dfs(1, 0);
for (int i = 1; i <= 16; ++i)
for (int j = 1; j + (1 << i) - 1 <= n; ++j)
mi[i][j] = get(mi[i - 1][j], mi[i - 1][j + (1 << i - 1)]);
build(1, 1, n);
// cout << T[1].ans << "\n";
for (int lst = 0; q--; ) {
int d; i64 e; cin >> d >> e;
d = (d + lst) % (n - 1) + 1; e = (e + lst) % WW;
// cout << d << " " << e << " UPD\n";
int y = E[d].v;
// cout << y << " ETDF\n";
// cout << dfn[y] << " " << dfn[y] + sz[y] - 1 << "\n";
add(dfn[y], dfn[y] + sz[y] - 1, e - E[d].w); E[d].w = e;
update(1, 1, n, dfn[y], dfn[y] + sz[y] - 1);
cout << (lst = T[1].ans) << "\n";
// cout << T[1].a << " " << T[1].b << "\n";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 9768kb
input:
10 100 10000 3 4 115 4 7 2757 1 5 5809 8 5 1111 6 2 7043 6 5 4932 6 4 4171 9 5 8499 10 5 2395 1 3517 8 7196 1 8064 6 7437 6 2421 8 67 7 6695 3 1217 1 920 1 1786 4 2541 5 266 4 6167 0 7590 6 195 8 4087 2 8073 6 8065 5 2882 2 3292 4 3435 6 8447 8 3419 0 9545 1 7922 0 4088 8 2546 4 7071 1 722 6 9178 0 ...
output:
8 1 2 1 3 1 7 1 4 3 6 5 9 1 10 1 5 9 1 10 21119 21746 23057 18619 18309 18309 19479 18309 19533 18309 18186 18262 19939 21768 20410 22382 20410 21356 17833 17119 13244 7187 4529 6469 8465 8524 9139 10958 11513 9377 9567 8338 8034 5923 5027 2900 2285 2068 1882 1945 2775 4745 4710 5813 7516 5813 4710 ...
result:
wrong answer 1st lines differ - expected: '21119', found: '8 1'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Wrong Answer
Test #27:
score: 0
Wrong Answer
time: 2ms
memory: 11856kb
input:
20 20 10000 1 2 835 1 3 57 1 4 1883 1 5 1349 1 6 1543 1 7 688 1 8 272 1 9 744 1 10 569 1 11 1019 1 12 201 1 13 1228 1 14 1194 1 15 1123 1 16 48 1 17 164 1 18 893 1 19 963 1 20 818 6 1 0 7412 10 6575 15 6696 0 7412 4 8318 18 7563 15 7502 1 7668 13 7859 14 8045 2 7969 12 8159 11 8040 2 8168 12 8192 0 ...
output:
2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 1 15 1 16 1 17 1 18 1 19 1 20 1 1 20 3426 3426 3426 2892 2577 2577 2543 2472 2142 2142 2086 2018 1961 1961 1958 1653 1562 1432 1432 1064
result:
wrong answer 1st lines differ - expected: '3426', found: '2 1'
Subtask #4:
score: 0
Wrong Answer
Test #48:
score: 0
Wrong Answer
time: 4ms
memory: 12044kb
input:
1000 1000 10000 1 2 503 1 3 889 2 4 6 2 5 1468 3 6 102 3 7 464 4 8 658 4 9 1642 5 10 1956 5 11 420 6 12 1287 6 13 1051 7 14 48 7 15 678 8 16 1662 8 17 906 9 18 432 9 19 124 10 20 71 10 21 1624 11 22 268 11 23 1776 12 24 404 12 25 967 13 26 658 13 27 815 14 28 1196 14 29 1920 15 30 865 15 31 1227 16 ...
output:
512 1 513 1 256 3 514 1 515 1 257 3 128 7 516 1 517 1 258 3 518 1 519 1 259 3 129 7 64 15 520 1 521 1 260 3 522 1 523 1 261 3 130 7 524 1 525 1 262 3 526 1 527 1 263 3 131 7 65 15 32 31 528 1 529 1 264 3 530 1 531 1 265 3 132 7 532 1 533 1 266 3 534 1 535 1 267 3 133 7 66 15 536 1 537 1 268 3 538 1 ...
result:
wrong answer 1st lines differ - expected: '24077', found: '512 1'
Subtask #5:
score: 0
Time Limit Exceeded
Test #65:
score: 0
Time Limit Exceeded
input:
100000 100000 20000000000000 36784 60773 140153248 61611 56014 4384507 39699 81699 3960320 64081 33880 136970044 38189 43550 67958946 16177 77482 151567728 90206 77109 108497900 42376 92541 75503161 10148 21587 195080992 11109 80580 11975495 8506 81405 144971319 85501 69547 59675956 72008 46890 3423...
output:
12452 1 56941 1 82676 1 47439 1 61326 1 73000 1 20871 1 47907 4 83675 1 82382 1 32460 1 68540 1 77672 1 7898 1 2443 2 52957 3 6892 4 22011 5 77903 1 46368 1 67990 1 39819 1 88243 1 69300 2 43719 1 26924 1 35218 1 54917 1 5818 1 31158 3 30160 1 54382 2 4766 3 12609 1 50368 1 7176 2 62265 1 4376 1 255...
result:
Subtask #6:
score: 0
Skipped
Dependency #1:
0%