#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define pb push_back
const ll N=100100;
vector<pair<ll,ll>> ma[N];
pair<ll,ll> edg[N];
ll dp[N][2],h[N],sz[N],dist[N],par[N],ans=0,wei[N],ind[N];
multiset<ll> sub[N];
multiset<ll> pos;
void dfs(int x,int p=-1)
{
sub[x].insert(0);
sub[x].insert(0);
for(auto it:ma[x])
{
int y=it.first;
int was=it.second;
if(y==p)continue;
edg[was]={x,y}; // par, child
ll w=wei[was];
h[y]=h[x]+w;
ind[y]=was;
par[y]=x;
dfs(y,x);
sub[x].insert(*rbegin(sub[y])+w);
}
pos.insert(*rbegin(sub[x]) + *(++rbegin(sub[x])));
}
int32_t main()
{
ll n,q,w;
cin>>n>>q>>w;
for(int i=0;i<n-1;i++)
{
ll a,b,w;
cin>>a>>b>>w;
wei[i]=w;
edg[i]={a,b};
ma[a].pb({b,i});
ma[b].pb({a,i});
}
h[1]=1;
dfs(1);
// cout<<ans<<endl;
ans=0;
while(q--)
{
ll d,nw;
cin>>d>>nw;
d=(d+ans)%(n-1);
nw=(nw+ans)%w;
ans=0;
ll x=edg[d].second;
vector<int> tp;
while(x>1)
{
tp.push_back(x);
x=par[x];
}
reverse(begin(tp),end(tp));
// while(x>1)
for(auto x:tp)
{
ll p=par[x];
// cout<<x<<' '<<p<<endl;
// for(auto j:sub[p])
// {
// cout<<j<<' ';
// }
// cout<<endl;
// for(auto j:pos)cout<<j<<' ';
// cout<<endl;
// cout<<(*rbegin(sub[p]) + *(++rbegin(sub[p])))<<endl;
// cout<<(*rbegin(sub[x]) + wei[ind[x]])<<endl;
pos.erase(pos.find(*rbegin(sub[p]) + *(++rbegin(sub[p]))));
// if(())
sub[p].erase(sub[p].find(*rbegin(sub[x]) + wei[ind[x]]));
// for(auto j:sub[p])
// {
// cout<<j<<' ';
// }
// cout<<endl;
x=p;
}
wei[d]=nw;
x=edg[d].second;
while(x>1)
{
ll p=par[x];
// cout<<"insert "<<x<<' '<<p<<endl;
sub[p].insert(*rbegin(sub[x]) + wei[ind[x]]);
pos.insert(*rbegin(sub[p]) + *(++rbegin(sub[p])));
// for(auto pt:sub[p])
// {
// cout<<pt<<' ';
// }
// cout<<endl;
x=p;
}
// cout<<"finish"<<endl;cout<<pos.size()<<endl;
ans=*rbegin(pos);
cout<<ans<<endl;
}
}
| # | 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... |