This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#define endl '\n'
#define pb push_back
#define ll long long
using namespace std;
const int MAXN = 1e5+100;
const ll INF = 1e18+109;
struct Edge{
ll u, c;
};
struct FEdge{
ll a, b, c;
};
struct node{
ll mx, mn, a_2b, c_2b, rs;
};
node seg[1 << 19];
ll lz[MAXN];
int n, q;
int sn = 1;
ll dx, ex;
ll lst;
ll w;
vector<Edge> adj[MAXN];
vector<int> et;
ll d[MAXN];
int st[MAXN], ft[MAXN];
bool used[MAXN];
vector<FEdge> edges;
void dfs(int v){
used[v] = 1;
st[v] = et.size();
ft[v] = et.size();
et.pb(d[v]);
for(Edge e : adj[v]){
if(used[e.u]) continue;
d[e.u] = (d[v] + e.c);
dfs(e.u);
ft[v] = et.size();
et.pb(d[v]);
}
}
ll max(ll a, ll b){
return (a > b)? a : b;
}
ll min(ll a, ll b){
return (a < b)? a : b;
}
node combine(node a, node b){
node res;
res.mx = max(a.mx, b.mx);
res.mn = min(a.mn, b.mn);
res.a_2b = max(a.a_2b, b.a_2b);
res.a_2b = max(res.a_2b, a.mx - 2*b.mn);
res.c_2b = max(a.c_2b, b.c_2b);
res.c_2b = max(res.c_2b, b.mx - 2*a.mn);
res.rs = max(a.rs, b.rs);
res.rs = max(res.rs, max(a.a_2b + b.mx, a.mx + b.c_2b));
return res;
}
void shift(int v){
if(v >= sn) return;
int lc = (v<<1), rc=lc|1;
ll x = lz[v];
seg[lc].mx += x;
seg[lc].mn += x;
seg[lc].a_2b -= x;
seg[lc].c_2b -= x;
seg[rc].mx += x;
seg[rc].mn += x;
seg[rc].a_2b -= x;
seg[rc].c_2b -= x;
lz[lc] += x;
lz[rc] += x;
lz[v] = 0;
}
void print(node x){
cout << x.mn << ' ' << x.mx << ' ' << x.a_2b << ' ' << x.c_2b << ' ' << x.rs << endl;
}
void add(int v, int vl, int vr, int l, int r, ll x){
shift(v);
if(vl > r or vr < l) return;
if(l <= vl and vr <= r){
seg[v].mx += x;
seg[v].mn += x;
seg[v].a_2b -= x;
seg[v].c_2b -= x;
lz[v] += x;
return;
}
int vm = (vl + vr) >> 1;
add((v<<1), vl, vm, l, r, x);
add((v<<1)|1, vm+1, vr, l, r, x);
seg[v] = combine(seg[(v<<1)], seg[(v<<1)|1]);
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);
cin >> n >> q >> w;
for(int i=0; i<n-1; i++){
int a, b, c;
cin >> a >> b >> c;
a--;b--;
adj[a].pb({b, c});
adj[b].pb({a, c});
edges.pb({a, b, c});
}
dfs(0);
while(sn < et.size()) sn <<= 1;
for(int i=0; i<et.size(); i++){
seg[i + sn].mn = et[i];
seg[i + sn].mx = et[i];
seg[i + sn].a_2b = -et[i];
seg[i + sn].c_2b = -et[i];
seg[i + sn].rs = et[i];
}
node emp = {-INF, INF, -INF, -INF, -INF};
for(int i=et.size(); i<sn; i++)
seg[i + sn] = emp;
for(int i=(sn-1); i>=1; i--)
seg[i] = combine(seg[(i<<1)], seg[(i<<1)|1]);
while(q--){
cin >> dx >> ex;
dx = (dx + lst) % (n-1);
ex = (ex + lst) % w;
FEdge e = edges[dx];
if(d[e.a] > d[e.b]) swap(e.a, e.b);
edges[dx].c = ex;
add(1, 1, sn, st[e.b]+1, ft[e.b]+1, ex - e.c);
cout << seg[1].rs << endl;
lst = seg[1].rs;
}
return 0;
}
Compilation message (stderr)
diameter.cpp: In function 'int main()':
diameter.cpp:150:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
150 | while(sn < et.size()) sn <<= 1;
| ~~~^~~~~~~~~~~
diameter.cpp:152:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
152 | for(int i=0; i<et.size(); i++){
| ~^~~~~~~~~~
# | 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... |