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>
#define ll long long
#define ull unsigned long long
#define all(x) x.begin(), x.end()
#define Neco "Dynamic Diameter"
#define resp(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define getbit(x,i) ((x >> i)&1)
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define cntbit(x) __builtin_popcountll(x)
#define fi(i, a, b) for(int i = a; i <= b; i++)
#define fid(i, a, b) for(int i = a; i >= b; i--)
#define maxn (int) 2e5 + 7
using namespace std;
const ll mod = 1e9 + 7; //972663749
const ll base = 911382323;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r) {
return uniform_int_distribution<ll> (l, r)(rng);
}
int n, q, Time; ll W;
int rn[maxn], In[maxn], Out[maxn]; ll w[maxn];
struct dl { int u, v; ll w; } E[maxn];
vector<int> edges[maxn];
void dfs_tour(int u, int par) {
rn[++Time] = u, In[u] = Time;
for(int v : edges[u]) if(v != par) {
dfs_tour(v, u); rn[++Time] = u;
}
Out[u] = Time;
}
struct IT {
ll store[4 * maxn];
struct Node {
ll Diameter, MaxH, MinH, MaxPref, MaxSuff;
void Add(ll w) {
MaxH += w, MinH += w;
MaxPref -= w, MaxSuff -= w;
}
} st[4 * maxn];
Node cb(const Node x, const Node y) {
Node ans;
ans.MaxH = max(x.MaxH, y.MaxH), ans.MinH = min(x.MinH, y.MinH);
ans.MaxPref = max({x.MaxPref, y.MaxPref, x.MaxH - 2 * y.MinH});
ans.MaxSuff = max({x.MaxSuff, y.MaxSuff, y.MaxH - 2 * x.MinH});
ans.Diameter = max(x.Diameter, y.Diameter);
ans.Diameter = max(ans.Diameter, x.MaxPref + y.MaxH);
ans.Diameter = max(ans.Diameter, y.MaxSuff + x.MaxH);
return ans;
}
void down(int id) {
ll val = store[id];
fi(i, 0, 1) {
st[id * 2 + i].Add(val), store[id * 2 + i] += val;
}
store[id] = 0;
}
void update(int u, int v, ll val, int id = 1, int l = 1, int r = Time) {
if(l > v || r < u) return;
if(u <= l && r <= v) {
st[id].Add(val), store[id] += val;
return;
}
down(id);
int mid = (l + r) >> 1;
update(u, v, val, _left), update(u, v, val, _right);
st[id] = cb(st[id * 2], st[id * 2 + 1]);
}
ll get() {
return st[1].Diameter;
}
} St;
void solve()
{
cin >> n >> q >> W;
fi(i, 1, n - 1) {
int x, y; ll w; cin >> x >> y >> w;
E[i] = {x, y, w};
edges[x].push_back(y), edges[y].push_back(x);
}
dfs_tour(1, 0);
fi(i, 1, n - 1) {
if(In[E[i].u] > In[E[i].v]) swap(E[i].u, E[i].v);
w[E[i].v] = E[i].w;
St.update(In[E[i].v], Out[E[i].v], E[i].w);
}
ll ans = 0;
fi(i, 1, q) {
ll id, val; cin >> id >> val;
id = (id + ans) % (n - 1) + 1;
val = (val + ans) % W;
int v = E[id].v;
val = val - w[v], w[v] += val;
St.update(In[v], Out[v], val);
ans = St.get(); cout << ans << '\n';
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen(Neco".inp", "r")) {
freopen(Neco".inp", "r", stdin);
freopen(Neco".out", "w", stdout);
}
int nTest = 1;
// cin >> nTest;
while(nTest--)
{
solve();
}
return 0;
}
Compilation message (stderr)
diameter.cpp: In function 'int main()':
diameter.cpp:132:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
132 | freopen(Neco".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
diameter.cpp:133:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
133 | freopen(Neco".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |