이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int INF=1e18;
const int L=25;
struct segtree{ //lazy maximum segtree with add updates
vector<int> tree;
vector<int> lazy;
segtree(int n){
tree.assign(n*4, 0);
lazy.assign(n*4, 0);
}
int update_node(int v, int add){
tree[v] += add;
lazy[v] += add;
return tree[v];
}
void propagate(int l, int r, int v){
if(lazy[v]==0) return;
update_node(v*2+1, lazy[v]);
update_node(v*2+2, lazy[v]);
lazy[v]=0;
}
int update(int l, int r, int v, int ql, int qr, int add){
if(qr<=l || r<=ql) return tree[v];
if(ql<=l && r<=qr){
return update_node(v, add);
}
propagate(l, r, v);
int m=l+(r-l)/2;
return tree[v] = max(update(l, m, v*2+1, ql, qr, add), update(m, r, v*2+2, ql, qr, add));
}
int query(int l, int r, int v, int ql, int qr){
if(qr<=l || r<=ql) return 0;
if(ql<=l && r<=qr){
return tree[v];
}
int m=l+(r-l)/2;
return max(query(l, m, v*2+1, ql, qr), query(m, r, v*2+2, ql, qr));
}
};
struct segtree2{
vector<int> tree;
void init(int n){
tree.assign(n*4, 0);
}
int update(int l, int r, int v, int x, int val){
if(x<l || r<=x) return tree[v];
if(l+1==r){
return tree[v]=val;
}
int m=l+(r-l)/2;
return tree[v] = max(update(l, m, v*2+1, x, val), update(m, r, v*2+2, x, val));
}
int query(){
return tree[0];
}
};
struct tree{
int n;
vector<pair<int,int> > edge;
vector<vector<pair<int,int> > >adi;
segtree2 segans;
vector<int> level;
vector<int> pc; //parent centroid
vector<int> subtreesize;
vector<int> curind;
vector<set<pair<int, int> > >children;
vector<set<pair<int, int> > >paths;
vector<vector<int> > preind;
vector<vector<int> > ssize;
vector<vector<int> > paredge;
vector<segtree> seg;
tree(int ninput){
n = ninput;
edge.assign(n-1, {-1, -1});
adi.assign(n, {});
segans.init(n);
level.assign(n, INF);
pc.assign(n, -1);
subtreesize.assign(n, 0);
curind.assign(L, 0);
children.assign(n, {});
paths.assign(n, {});
preind.assign(L, vector<int>(n, 0));
ssize.assign(L, vector<int> (n, 0));
paredge.assign(L, vector<int> (n, 0));
seg.assign(L, segtree(n));
}
//centroids
int dfs(int v, int p, int lev){ //precomputes the subtreesizes
subtreesize[v] = 1;
for(auto [u, d]: adi[v]){
if(u==p || level[u]<lev) continue;
subtreesize[v] += dfs(u, v, lev);
}
return subtreesize[v];
}
int centroiddfs(int v, int lev, int size){ //finds a centroid in a connected graph
//cout << "centroid dfs " << v << " " << lev << " " << size << " " << "\n";
for(auto [u, d]: adi[v]){
if(level[u] < lev) continue;
if(subtreesize[u] > size/2){
subtreesize[v] = size - subtreesize[u];
return centroiddfs(u, lev, size);
}
}
return v;
}
void find_centroids(int v, int lev, int centroidp){ //finds all centroids recursively
int size = dfs(v, -1, lev);
int centroid = centroiddfs(v, lev, size);
level[centroid] = lev;
pc[centroid]= centroidp;
//compute all values for this component
distancedfs(centroid, -1, 0, lev);
for(auto [u, d]: adi[centroid]){
if(level[u] < lev) continue;
children[centroid].insert({preind[lev][u], u});
int p = seg[lev].query(0, n, 0, preind[lev][u], preind[lev][u] + ssize[lev][u]);
paths[centroid].insert({p, u});
}
int diameter = 0;
if(paths[centroid].size()>0){
diameter += (*prev(paths[centroid].end())).first;
}
if(paths[centroid].size()>1){
diameter += (*prev(prev(paths[centroid].end()))).first;
}
segans.update(0, n, 0, centroid, diameter);
//cout << "centroid: " << centroid << " " << lev << " " << diameter << "\n";
//find next centroids
for(auto [u, d]: adi[centroid]){
if(level[u] < lev) continue;
find_centroids(u, lev+1, centroid);
}
}
int distancedfs(int v, int p, int dist, int lev){
preind[lev][v] = curind[lev];
curind[lev] ++;
seg[lev].update(0, n, 0, preind[lev][v], preind[lev][v]+1, dist);
int s=1;
for(auto [u, d]: adi[v]){
if(u==p || level[u] < lev) continue;
paredge[lev][u] = d;
s += distancedfs(u, v, dist+d, lev);
}
ssize[lev][v] = s;
return s;
}
int update_edge(int a, int b, int val){
int c; int lev;
if(level[a] > level[b]){
c=a;
lev = level[a];
}
else{
c=b;
lev = level[b];
}
c = pc[c];
lev--;
for(; lev>=0; lev--){
//cout << "update centroid " << c << " " << lev << "\n";
if(paths[c].size()==0){ //single node doesn't need update
c = pc[c];
continue;
}
if(preind[lev][b]<preind[lev][a]){ //=> a is an ancestor of b
swap(a, b);
}
/*cout << preind[lev][b] << "\n";
for(auto [i, j]: children[c]){
cout << "(" << i << ", " << j << ") ";
}
cout << "\n";*/
// update c
int x = (*prev(children[c].upper_bound({preind[lev][b], b}))).second;
int oldp = seg[lev].query(0, n, 0, preind[lev][x], preind[lev][x]+ssize[lev][x]);
paths[c].erase({oldp, x});
int add = val- paredge[lev][b];
paredge[lev][b] = val;
seg[lev].update(0, n, 0, preind[lev][b], preind[lev][b]+ssize[lev][b], add);
int newp = seg[lev].query(0, n, 0, preind[lev][x], preind[lev][x] + ssize[lev][x]);
paths[c].insert({newp, x});
//cout << x << " " << newp << "\n";
int diameterc = (*prev(paths[c].end())).first;
if(paths.size()>=2){
diameterc += (*prev(prev(paths[c].end()))).first;
}
segans.update(0, n, 0, c, diameterc);
//cout << "=> " << diameterc << "\n";
c = pc[c];
}
return segans.query();
}
};
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, q, maxw;
cin >> n >> q >> maxw;
tree g(n);
for(int i=0; i<n-1; i++){
int a, b, c;
cin >> a >> b >> c;
a--; b--;
g.edge[i]={a, b};
g.adi[a].push_back({b, c});
g.adi[b].push_back({a, c});
}
g.find_centroids(0, 0, -1);
int last=0;
for(int i=0; i<q; i++){
//cout << "update " << i << "\n" << flush;
int di, wi;
cin >> di >> wi;
int d = (di + last) % (n-1);
int w = (wi + last) % maxw;
auto [a, b] = g.edge[d];
//cout << d << " " << w << ": " << a << " " << b << "\n" << flush;
int ans=g.update_edge(a, b, w);
cout << ans << "\n";
last = ans;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |