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<bits/stdc++.h>
#include<fstream>
using namespace std;
ifstream fin("ANTNEST.INP");
ofstream fout("ANTNEST.OUT");
#define sz(a) (int)a.size()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mpp make_pair
const ld PI = 3.14159265359;
//using u128 = __uint128_t;
//const int x[4] = {1, -1, 0, 0};
//const int y[4] = {0, 0, 1, -1};
const ll mod = 1e9 + 7;
const int mxn = 2e5 + 5, mxq = 2e5 + 5, sq = 350, mxv = 5e5 + 5;
const ll inf = 1e18;
const ld prec = 1e-9;
int n, q;
ll MX;
struct BIT{
ll bit[mxn + 1];
void upd(int p, int v){
while(p <= n){
bit[p] += v; p += p & (-p);
}
}
ll get(int p){
ll ans = 0;
while(p){
ans += bit[p]; p -= p & (-p);
}
return(ans);
}
ll query(int l, int r){
return(get(r) - get(l - 1));
}
};
BIT bit;
pii up[mxn + 1][19];
int tin[mxn + 1], tout[mxn + 1], siz[mxn + 1], dep[mxn + 1], tonode[mxn + 1], tt = 0, eu = 0, eutin[mxn + 1];
int u[mxn + 1], v[mxn + 1];
ll w[mxn + 1];
vt<pll>adj[mxn + 1];
pii getmn(int l, int r){
int lg = __lg(r - l + 1);
return(min(up[l][lg], up[r - (1 << lg) + 1][lg]));
}
int lca(int u, int v){
if(tin[u] > tin[v])swap(u, v);
return(getmn(tin[u], tin[v]).se);
}
ll getdist(int u, int v){
return(bit.get(eutin[u]) + bit.get(eutin[v]) - 2 * bit.get(eutin[lca(u, v)]));
}
void dfs(int s, int pre){
tin[s] = ++tt; tonode[tt] = s; siz[s] = 1;
eutin[s] = ++eu;
for(auto [i, w]: adj[s]){
if(i != pre){
dep[i] = dep[s] + 1;
dfs(i, s); siz[s] += siz[i];
tonode[++tt] = s;
}
}
tout[s] = tt;
}
struct Node{
int u, v; ll d;
Node(){
}
Node(int _u, int _v, ll _d){
u = _u; v = _v; d = _d;
}
friend bool operator <(Node a, Node b){
return(a.d < b.d);
}
friend Node operator +(Node a, Node b){
Node res = Node(-1, -1, -1);
res = max(res, a); res = max(res, b);
res = max(res, Node(a.u, b.u, getdist(a.u, b.u)));
res = max(res, Node(a.u, b.v, getdist(a.u, b.v)));
res = max(res, Node(a.v, b.u, getdist(a.v, b.u)));
res = max(res, Node(a.v, b.v, getdist(a.v, b.v)));
return(res);
}
};
Node st[4 * mxn + 1];
void build(int nd, int l, int r){
if(l == r){
st[nd] = Node(tonode[l], tonode[l], 0);
return;
}
int mid = (l + r) >> 1;
build(nd << 1, l, mid); build(nd << 1 | 1, mid + 1, r);
st[nd] = st[nd << 1] + st[nd << 1 | 1];
}
void upd(int nd, int l, int r, int ql, int qr){
if(ql > r || qr < l)return;
if(ql <= l && qr >= r)return;
int mid = (l + r) >> 1;
upd(nd << 1, l, mid, ql, qr); upd(nd << 1 | 1, mid + 1, r, ql, qr);
st[nd] = st[nd << 1] + st[nd << 1 | 1];
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> q >> MX;
for(int i = 0; i < n - 1; i++){
cin >> u[i] >> v[i] >> w[i];
adj[u[i]].pb(mpp(v[i], w[i])); adj[v[i]].pb(mpp(u[i], w[i]));
}
dfs(1, -1);
for(int i = 0; i < n - 1; i++){
if(dep[u[i]] > dep[v[i]])swap(u[i], v[i]);
bit.upd(eutin[v[i]], w[i]); bit.upd(eutin[v[i]] + siz[v[i]], -w[i]);
}
for(int i = 1; i <= tt; i++)up[i][0] = mpp(dep[tonode[i]], tonode[i]);
for(int i = 1; (1 << i) <= tt; i++){
for(int j = 1; j + (1 << i) - 1 <= tt; j++){
up[j][i] = min(up[j][i - 1], up[j + (1 << (i - 1))][i - 1]);
}
}
build(1, 1, tt);
ll last = 0;
while(q--){
ll d, e; cin >> d >> e; d = (d + last) % (n - 1); e = (e + last) % MX;
bit.upd(eutin[v[d]], e - w[d]); bit.upd(eutin[v[d]] + siz[v[d]], -(e - w[d]));
upd(1, 1, tt, tin[v[d]], tout[v[d]]);
last = st[1].d; w[d] = e;
cout << last << "\n";
}
return(0);
}
Compilation message (stderr)
diameter.cpp: In function 'void dfs(int, int)':
diameter.cpp:68:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
68 | for(auto [i, w]: adj[s]){
| ^
# | 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... |