#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
struct edge {
int u, v;
long long w;
int other( int x ) {
return u ^ v ^ x;
}
};
const int MAX_N = 1e5;
edge edges[MAX_N];
vector<int> adj[MAX_N + 1];
struct SegTree {
int ll, rr;
vector<long long> segTree, lazy;
void propag( int v, int l, int r ) {
segTree[v] += lazy[v];
if ( l != r ) {
lazy[v * 2 + 1] += lazy[v];
lazy[v * 2 + 2] += lazy[v];
}
lazy[v] = 0;
}
void init( int l, int r ) {
ll = l;
rr = r;
segTree.resize( 4 * (r - l + 1) );
lazy.resize( 4 * (r - l + 1) );
}
void update( int v, int l, int r, int lu, int ru, long long x ) {
propag( v, l, r );
if ( l > ru || r < lu )
return;
if ( lu <= l && r <= ru ) {
lazy[v] = x;
propag( v, l, r );
return;
}
int mid = (l + r) / 2;
update( v * 2 + 1, l, mid, lu, ru, x );
update( v * 2 + 2, mid + 1, r, lu, ru, x );
segTree[v] = max( segTree[v * 2 + 1], segTree[v * 2 + 2] );
}
void update( int l, int r, long long x ) {
update( 0, ll, rr, l, r, x );
}
long long query( int v, int l, int r, int lq, int rq ) {
propag( v, l, r );
if ( l > rq || r < lq )
return 0;
if ( segTree[v] == 0 )
return 0;
if ( lq <= l && r <= rq )
return segTree[v];
int mid = (l + r) / 2;
return max( query( v * 2 + 1, l, mid, lq, rq ), query( v * 2 + 2, mid + 1, r, lq, rq ) );
}
long long queryAll() {
return segTree[0];
}
};
SegTree depths[2 * MAX_N + 1];
bool isCentroid[MAX_N + 1];
int sz[MAX_N + 1];
void calcSizes( int u, int p ) {
sz[u] = 1;
for ( int e: adj[u] ) {
int v = edges[e].other( u );
if ( v == p || isCentroid[v] )
continue;
calcSizes( v, u );
sz[u] += sz[v];
}
}
int totSz;
int findCentroid( int u, int p ) {
int c = 0, maxSz = totSz - sz[u];
for ( int e: adj[u] ) {
int v = edges[e].other( u );
if ( v == p || isCentroid[v] )
continue;
int d = findCentroid( v, u );
if ( d != 0 )
c = d;
maxSz = max( maxSz, sz[v] );
}
if ( maxSz <= totSz / 2 )
c = u;
return c;
}
struct info {
int c, h, l, r;
};
vector<info> centroids[MAX_N];
vector<int> heads[MAX_N + 1];
int crtPos, headId;
void dfs( int u, int p, int c, int h ) {
int children = 0;
for ( int e: adj[u] ) {
int v = edges[e].other( u );
if ( v == p || isCentroid[v] )
continue;
info x;
x.c = c;
x.h = headId;
x.l = crtPos;
dfs( v, u, c, h );
x.r = crtPos - 1;
children++;
centroids[e].push_back( x );
}
if ( children == 0 )
crtPos++;
}
void decomp( int r ) {
calcSizes( r, 0 );
totSz = sz[r];
int c = findCentroid( r, 0 );
for ( int e: adj[c] ) {
int v = edges[e].other( c );
if ( isCentroid[v] )
continue;
heads[c].push_back( headId );
crtPos = 0;
dfs( v, c, c, e );
centroids[e].push_back( { c, headId, 0, crtPos - 1 } );
depths[headId].init( 0, crtPos - 1 );
headId++;
}
isCentroid[c] = true;
for ( int e: adj[c] ) {
int v = edges[e].other( c );
if ( isCentroid[v] )
continue;
decomp( v );
}
}
multiset<long long> diameterByCentroid[MAX_N + 1], allDiameters;
int main() {
int n, q;
long long m;
cin >> n >> q >> m;
for ( int e = 0; e < n - 1; e++ ) {
cin >> edges[e].u >> edges[e].v >> edges[e].w;
adj[edges[e].u].push_back( e );
adj[edges[e].v].push_back( e );
}
decomp( 1 );
for ( int e = 0; e < n - 1; e++ ) {
int w = edges[e].w;
for ( auto x: centroids[e] ) {
int c = x.c, h = x.h, l = x.l, r = x.r;
depths[h].update( l, r, w );
}
}
for ( int c = 1; c <= n; c++ ) {
for ( int h: heads[c] )
diameterByCentroid[c].insert( depths[h].queryAll() );
long long d1 = (diameterByCentroid[c].size() <= 0 ? 0 : *diameterByCentroid[c].rbegin());
long long d2 = (diameterByCentroid[c].size() <= 1 ? 0 : *next( diameterByCentroid[c].rbegin() ) );
allDiameters.insert( d1 + d2 );
}
long long last = 0;
while ( q-- ) {
int e;
long long w;
cin >> e >> w;
e = (e + last) % (n - 1);
w = (w + last) % m;
for ( auto x: centroids[e] ) {
long long d1, d2, maxDepth;
int c = x.c, h = x.h, l = x.l, r = x.r;
d1 = (diameterByCentroid[c].size() <= 0 ? 0 : *diameterByCentroid[c].rbegin());
d2 = (diameterByCentroid[c].size() <= 1 ? 0 : *next( diameterByCentroid[c].rbegin() ) );
allDiameters.erase( allDiameters.lower_bound( d1 + d2 ) );
maxDepth = depths[h].queryAll();
diameterByCentroid[c].erase( diameterByCentroid[c].lower_bound( maxDepth ) );
depths[h].update( l, r, w - edges[e].w );
maxDepth = depths[h].queryAll();
diameterByCentroid[c].insert( maxDepth );
d1 = (diameterByCentroid[c].size() <= 0 ? 0 : *diameterByCentroid[c].rbegin());
d2 = (diameterByCentroid[c].size() <= 1 ? 0 : *next( diameterByCentroid[c].rbegin() ) );
allDiameters.insert( d1 + d2 );
}
edges[e].w = w;
last = *allDiameters.rbegin();
cout << last << "\n";
}
return 0;
}
Compilation message
diameter.cpp: In function 'int main()':
diameter.cpp:193:17: warning: unused variable 'c' [-Wunused-variable]
193 | int c = x.c, h = x.h, l = x.l, r = x.r;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
23384 KB |
Output is correct |
2 |
Correct |
6 ms |
23388 KB |
Output is correct |
3 |
Correct |
6 ms |
23632 KB |
Output is correct |
4 |
Correct |
6 ms |
23644 KB |
Output is correct |
5 |
Correct |
6 ms |
23448 KB |
Output is correct |
6 |
Correct |
6 ms |
23384 KB |
Output is correct |
7 |
Correct |
5 ms |
23644 KB |
Output is correct |
8 |
Correct |
6 ms |
23644 KB |
Output is correct |
9 |
Correct |
6 ms |
23416 KB |
Output is correct |
10 |
Correct |
6 ms |
23644 KB |
Output is correct |
11 |
Correct |
7 ms |
23644 KB |
Output is correct |
12 |
Correct |
5 ms |
23644 KB |
Output is correct |
13 |
Correct |
6 ms |
23644 KB |
Output is correct |
14 |
Correct |
6 ms |
23644 KB |
Output is correct |
15 |
Correct |
6 ms |
23648 KB |
Output is correct |
16 |
Correct |
5 ms |
23648 KB |
Output is correct |
17 |
Correct |
6 ms |
23448 KB |
Output is correct |
18 |
Correct |
6 ms |
23648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
23384 KB |
Output is correct |
2 |
Correct |
6 ms |
23388 KB |
Output is correct |
3 |
Correct |
6 ms |
23632 KB |
Output is correct |
4 |
Correct |
6 ms |
23644 KB |
Output is correct |
5 |
Correct |
6 ms |
23448 KB |
Output is correct |
6 |
Correct |
6 ms |
23384 KB |
Output is correct |
7 |
Correct |
5 ms |
23644 KB |
Output is correct |
8 |
Correct |
6 ms |
23644 KB |
Output is correct |
9 |
Correct |
6 ms |
23416 KB |
Output is correct |
10 |
Correct |
6 ms |
23644 KB |
Output is correct |
11 |
Correct |
7 ms |
23644 KB |
Output is correct |
12 |
Correct |
5 ms |
23644 KB |
Output is correct |
13 |
Correct |
6 ms |
23644 KB |
Output is correct |
14 |
Correct |
6 ms |
23644 KB |
Output is correct |
15 |
Correct |
6 ms |
23648 KB |
Output is correct |
16 |
Correct |
5 ms |
23648 KB |
Output is correct |
17 |
Correct |
6 ms |
23448 KB |
Output is correct |
18 |
Correct |
6 ms |
23648 KB |
Output is correct |
19 |
Correct |
27 ms |
24412 KB |
Output is correct |
20 |
Correct |
23 ms |
24152 KB |
Output is correct |
21 |
Correct |
25 ms |
24156 KB |
Output is correct |
22 |
Correct |
28 ms |
24152 KB |
Output is correct |
23 |
Correct |
36 ms |
26348 KB |
Output is correct |
24 |
Correct |
41 ms |
26728 KB |
Output is correct |
25 |
Correct |
46 ms |
26976 KB |
Output is correct |
26 |
Correct |
39 ms |
26716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
23388 KB |
Output is correct |
2 |
Correct |
5 ms |
23384 KB |
Output is correct |
3 |
Correct |
8 ms |
23640 KB |
Output is correct |
4 |
Correct |
40 ms |
23612 KB |
Output is correct |
5 |
Correct |
174 ms |
24000 KB |
Output is correct |
6 |
Correct |
5 ms |
23388 KB |
Output is correct |
7 |
Correct |
6 ms |
23640 KB |
Output is correct |
8 |
Correct |
6 ms |
23644 KB |
Output is correct |
9 |
Correct |
9 ms |
23556 KB |
Output is correct |
10 |
Correct |
41 ms |
23688 KB |
Output is correct |
11 |
Correct |
185 ms |
24144 KB |
Output is correct |
12 |
Correct |
10 ms |
24924 KB |
Output is correct |
13 |
Correct |
10 ms |
24924 KB |
Output is correct |
14 |
Correct |
14 ms |
24920 KB |
Output is correct |
15 |
Correct |
57 ms |
25308 KB |
Output is correct |
16 |
Correct |
204 ms |
25348 KB |
Output is correct |
17 |
Correct |
116 ms |
52040 KB |
Output is correct |
18 |
Correct |
120 ms |
52040 KB |
Output is correct |
19 |
Correct |
121 ms |
52136 KB |
Output is correct |
20 |
Correct |
169 ms |
52316 KB |
Output is correct |
21 |
Correct |
407 ms |
54108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
24408 KB |
Output is correct |
2 |
Correct |
37 ms |
24156 KB |
Output is correct |
3 |
Correct |
141 ms |
24420 KB |
Output is correct |
4 |
Correct |
290 ms |
25004 KB |
Output is correct |
5 |
Correct |
33 ms |
31640 KB |
Output is correct |
6 |
Correct |
65 ms |
31828 KB |
Output is correct |
7 |
Correct |
203 ms |
32340 KB |
Output is correct |
8 |
Correct |
383 ms |
33360 KB |
Output is correct |
9 |
Correct |
173 ms |
68752 KB |
Output is correct |
10 |
Correct |
236 ms |
69460 KB |
Output is correct |
11 |
Correct |
408 ms |
69716 KB |
Output is correct |
12 |
Correct |
657 ms |
70576 KB |
Output is correct |
13 |
Correct |
365 ms |
117332 KB |
Output is correct |
14 |
Correct |
409 ms |
117692 KB |
Output is correct |
15 |
Correct |
681 ms |
118224 KB |
Output is correct |
16 |
Correct |
946 ms |
119068 KB |
Output is correct |
17 |
Correct |
1829 ms |
118820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1707 ms |
101072 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
23384 KB |
Output is correct |
2 |
Correct |
6 ms |
23388 KB |
Output is correct |
3 |
Correct |
6 ms |
23632 KB |
Output is correct |
4 |
Correct |
6 ms |
23644 KB |
Output is correct |
5 |
Correct |
6 ms |
23448 KB |
Output is correct |
6 |
Correct |
6 ms |
23384 KB |
Output is correct |
7 |
Correct |
5 ms |
23644 KB |
Output is correct |
8 |
Correct |
6 ms |
23644 KB |
Output is correct |
9 |
Correct |
6 ms |
23416 KB |
Output is correct |
10 |
Correct |
6 ms |
23644 KB |
Output is correct |
11 |
Correct |
7 ms |
23644 KB |
Output is correct |
12 |
Correct |
5 ms |
23644 KB |
Output is correct |
13 |
Correct |
6 ms |
23644 KB |
Output is correct |
14 |
Correct |
6 ms |
23644 KB |
Output is correct |
15 |
Correct |
6 ms |
23648 KB |
Output is correct |
16 |
Correct |
5 ms |
23648 KB |
Output is correct |
17 |
Correct |
6 ms |
23448 KB |
Output is correct |
18 |
Correct |
6 ms |
23648 KB |
Output is correct |
19 |
Correct |
27 ms |
24412 KB |
Output is correct |
20 |
Correct |
23 ms |
24152 KB |
Output is correct |
21 |
Correct |
25 ms |
24156 KB |
Output is correct |
22 |
Correct |
28 ms |
24152 KB |
Output is correct |
23 |
Correct |
36 ms |
26348 KB |
Output is correct |
24 |
Correct |
41 ms |
26728 KB |
Output is correct |
25 |
Correct |
46 ms |
26976 KB |
Output is correct |
26 |
Correct |
39 ms |
26716 KB |
Output is correct |
27 |
Correct |
5 ms |
23388 KB |
Output is correct |
28 |
Correct |
5 ms |
23384 KB |
Output is correct |
29 |
Correct |
8 ms |
23640 KB |
Output is correct |
30 |
Correct |
40 ms |
23612 KB |
Output is correct |
31 |
Correct |
174 ms |
24000 KB |
Output is correct |
32 |
Correct |
5 ms |
23388 KB |
Output is correct |
33 |
Correct |
6 ms |
23640 KB |
Output is correct |
34 |
Correct |
6 ms |
23644 KB |
Output is correct |
35 |
Correct |
9 ms |
23556 KB |
Output is correct |
36 |
Correct |
41 ms |
23688 KB |
Output is correct |
37 |
Correct |
185 ms |
24144 KB |
Output is correct |
38 |
Correct |
10 ms |
24924 KB |
Output is correct |
39 |
Correct |
10 ms |
24924 KB |
Output is correct |
40 |
Correct |
14 ms |
24920 KB |
Output is correct |
41 |
Correct |
57 ms |
25308 KB |
Output is correct |
42 |
Correct |
204 ms |
25348 KB |
Output is correct |
43 |
Correct |
116 ms |
52040 KB |
Output is correct |
44 |
Correct |
120 ms |
52040 KB |
Output is correct |
45 |
Correct |
121 ms |
52136 KB |
Output is correct |
46 |
Correct |
169 ms |
52316 KB |
Output is correct |
47 |
Correct |
407 ms |
54108 KB |
Output is correct |
48 |
Correct |
9 ms |
24408 KB |
Output is correct |
49 |
Correct |
37 ms |
24156 KB |
Output is correct |
50 |
Correct |
141 ms |
24420 KB |
Output is correct |
51 |
Correct |
290 ms |
25004 KB |
Output is correct |
52 |
Correct |
33 ms |
31640 KB |
Output is correct |
53 |
Correct |
65 ms |
31828 KB |
Output is correct |
54 |
Correct |
203 ms |
32340 KB |
Output is correct |
55 |
Correct |
383 ms |
33360 KB |
Output is correct |
56 |
Correct |
173 ms |
68752 KB |
Output is correct |
57 |
Correct |
236 ms |
69460 KB |
Output is correct |
58 |
Correct |
408 ms |
69716 KB |
Output is correct |
59 |
Correct |
657 ms |
70576 KB |
Output is correct |
60 |
Correct |
365 ms |
117332 KB |
Output is correct |
61 |
Correct |
409 ms |
117692 KB |
Output is correct |
62 |
Correct |
681 ms |
118224 KB |
Output is correct |
63 |
Correct |
946 ms |
119068 KB |
Output is correct |
64 |
Correct |
1829 ms |
118820 KB |
Output is correct |
65 |
Incorrect |
1707 ms |
101072 KB |
Output isn't correct |
66 |
Halted |
0 ms |
0 KB |
- |