#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];
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;
}
int leafs[MAX_N + 1];
vector<int> centroids[MAX_N], heads[MAX_N + 1];
vector<pair<int, int>> head[MAX_N + 1], leftPos[MAX_N + 1], rightPos[MAX_N + 1];
int crtPos;
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;
centroids[e].push_back( c );
head[c].push_back( { e, h } );
leftPos[c].push_back( { e, crtPos } );
dfs( v, u, c, h );
rightPos[c].push_back( { e, crtPos - 1 } );
children++;
}
if ( children == 0 )
crtPos++;
}
void decomp( int r ) {
calcSizes( r, 0 );
totSz = sz[r];
int c = findCentroid( r, 0 );
c = 1;
crtPos = 0;
for ( int e: adj[c] ) {
int v = edges[e].other( c );
if ( isCentroid[v] )
continue;
centroids[e].push_back( c );
heads[c].push_back( e );
head[c].push_back( { e, e } );
leftPos[c].push_back( { e, crtPos } );
dfs( v, c, c, e );
rightPos[c].push_back( { e, crtPos - 1 } );
}
sort( head[c].begin(), head[c].end() );
sort( leftPos[c].begin(), leftPos[c].end() );
sort( rightPos[c].begin(), rightPos[c].end() );
leafs[c] = crtPos;
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 findElem( vector<pair<int, int>> &v, int x ) {
int st = 0, dr = v.size();
while ( dr - st > 1 ) {
int mij = (st + dr) / 2;
if ( v[mij].first > x )
dr = mij;
else
st = mij;
}
return v[st].second;
}
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 query( int l, int r ) {
return query( 0, ll, rr, l, r );
}
};
SegTree depths[MAX_N + 1];
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 c = 1; c <= n; c++ ) {
depths[c].init( 0, leafs[c] - 1 );
for ( auto p: head[c] ) {
int e = p.first;
depths[c].update( findElem( leftPos[c], e ), findElem( rightPos[c], e ), edges[e].w );
}
for ( int h: heads[c] ) {
long long maxDepth = depths[c].query( findElem( leftPos[c], h ), findElem( rightPos[c], h ) );
diameterByCentroid[c].insert( maxDepth );
}
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 ( int c: centroids[e] ) {
long long d1, d2, maxDepth;
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 ) );
int h = findElem( head[c], e );
maxDepth = depths[c].query( findElem( leftPos[c], h ), findElem( rightPos[c], h ) );
diameterByCentroid[c].erase( diameterByCentroid[c].lower_bound( maxDepth ) );
depths[c].update( findElem( leftPos[c], e ), findElem( rightPos[c], e ), w - edges[e].w );
maxDepth = depths[c].query( findElem( leftPos[c], h ), findElem( rightPos[c], h ) );
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
25436 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
25436 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
24668 KB |
Output is correct |
2 |
Correct |
13 ms |
24668 KB |
Output is correct |
3 |
Correct |
16 ms |
24664 KB |
Output is correct |
4 |
Correct |
50 ms |
24828 KB |
Output is correct |
5 |
Correct |
206 ms |
25684 KB |
Output is correct |
6 |
Correct |
11 ms |
24668 KB |
Output is correct |
7 |
Correct |
14 ms |
24800 KB |
Output is correct |
8 |
Correct |
12 ms |
24668 KB |
Output is correct |
9 |
Correct |
21 ms |
24668 KB |
Output is correct |
10 |
Correct |
64 ms |
24928 KB |
Output is correct |
11 |
Correct |
252 ms |
25688 KB |
Output is correct |
12 |
Correct |
17 ms |
26200 KB |
Output is correct |
13 |
Correct |
15 ms |
26972 KB |
Output is correct |
14 |
Correct |
27 ms |
26916 KB |
Output is correct |
15 |
Correct |
79 ms |
26400 KB |
Output is correct |
16 |
Correct |
314 ms |
27284 KB |
Output is correct |
17 |
Correct |
206 ms |
52548 KB |
Output is correct |
18 |
Correct |
188 ms |
52632 KB |
Output is correct |
19 |
Correct |
212 ms |
52964 KB |
Output is correct |
20 |
Correct |
297 ms |
52932 KB |
Output is correct |
21 |
Correct |
758 ms |
53952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
11 ms |
25692 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
586 ms |
47052 KB |
Output is correct |
2 |
Correct |
626 ms |
47024 KB |
Output is correct |
3 |
Correct |
642 ms |
47192 KB |
Output is correct |
4 |
Correct |
746 ms |
47540 KB |
Output is correct |
5 |
Correct |
649 ms |
47848 KB |
Output is correct |
6 |
Correct |
633 ms |
51436 KB |
Output is correct |
7 |
Correct |
558 ms |
50296 KB |
Output is correct |
8 |
Correct |
624 ms |
52268 KB |
Output is correct |
9 |
Correct |
679 ms |
52188 KB |
Output is correct |
10 |
Correct |
637 ms |
52216 KB |
Output is correct |
11 |
Correct |
670 ms |
52884 KB |
Output is correct |
12 |
Correct |
649 ms |
55968 KB |
Output is correct |
13 |
Correct |
492 ms |
56248 KB |
Output is correct |
14 |
Correct |
710 ms |
56448 KB |
Output is correct |
15 |
Correct |
546 ms |
56516 KB |
Output is correct |
16 |
Correct |
575 ms |
56388 KB |
Output is correct |
17 |
Correct |
580 ms |
56760 KB |
Output is correct |
18 |
Correct |
580 ms |
56996 KB |
Output is correct |
19 |
Correct |
586 ms |
57024 KB |
Output is correct |
20 |
Correct |
506 ms |
56980 KB |
Output is correct |
21 |
Correct |
661 ms |
57076 KB |
Output is correct |
22 |
Correct |
588 ms |
56800 KB |
Output is correct |
23 |
Correct |
563 ms |
57056 KB |
Output is correct |
24 |
Correct |
599 ms |
57532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
25436 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |