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>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
#define long unsigned long
#define pb push_back
#define mp make_pair
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define lb lower_bound
#define ub upper_bound
#define sz(v) int((v).size())
#define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
const int N = 1e5+7;
set <pair <int, ll>> graph[N];
vector <pair <pii, ll>> edges;
ll df, vf;
int n, q;
ll w;
void find_furthest(int v, int p, ll dist = 0) {
if (dist > df) {
df = dist;
vf = v;
}
for (auto to : graph[v]) {
if (to.first == p) continue;
find_furthest(to.first, v, dist+to.second);
}
}
void subtask3() {
for (auto to : edges) {
if (to.first.first != 1) {
return;
}
}
set <pair <ll, int>, greater <pair <ll, int>>> st;
for (auto to : edges) {
st.insert(mp(to.second, to.first.second));
}
ll last = 0;
while (q--) {
ll d, e;
cin >> d >> e;
d = (d + last) % (n - 1);
e = (e + last) % w;
auto &c = edges[d].second;
auto b = edges[d].first.second;
st.erase(st.find(mp(c, b)));
c = e;
st.insert(mp(c, b));
ll ans = 0;
ll cnt = 0;
for (auto to : st) {
ans += to.first;
if (++cnt > 1) break;
}
cout << ans << endl;
last = ans;
}
exit(0);
}
void subtask4() {
vector <ll> ans_for(n+1);
vector <pll> mx_child(n+1), dist_child(n+1);
set <pair <ll, int>, greater <pair <ll, int>>> st;
for (auto to : edges) {
auto a = to.first.first;
auto b = to.first.second;
if (!(a*2 != b || a*2+1 != b)) {
return;
}
if (a*2 == b) dist_child[a].first = to.second;
else dist_child[a].second = to.second;
}
for (int i = 1; i <= n; i++) {
if (sz(graph[i]) == 1) {
int x = i;
while (x) {
if (x*2 <= n) mx_child[x].first = max(mx_child[x*2].first, mx_child[x*2].second)+dist_child[x].first;
if (x*2+1 <= n) mx_child[x].second = max(mx_child[x*2+1].first, mx_child[x*2+1].second)+dist_child[x].second;
x >>= 1;
}
}
}
for (int i = 1; i <= n; i++) {
st.insert(mp(mx_child[i].first+mx_child[i].second, i));
}
ll last = 0;
while (q--) {
ll d, e;
cin >> d >> e;
d = (d + last) % (n - 1);
e = (e + last) % w;
auto a = edges[d].first.first, b = edges[d].first.second;
int x = (b >> 1);
if (a*2 == b) dist_child[a].first = e;
else dist_child[a].second = e;
while (x) {
st.erase(st.find(mp(mx_child[x].first+mx_child[x].second, x)));
if (x*2 <= n) mx_child[x].first = max(mx_child[x*2].first, mx_child[x*2].second)+dist_child[x].first;
if (x*2+1 <= n) mx_child[x].second = max(mx_child[x*2+1].first, mx_child[x*2+1].second)+dist_child[x].second;
st.insert(mp(mx_child[x].first+mx_child[x].second, x));
x >>= 1;
}
cout << (*st.begin()).first << endl;
last = (*st.begin()).first;
}
exit(0);
}
void solve() {
cin >> n >> q >> w;
for (int i = 0; i < n-1; i++) {
int a, b;
ll c;
cin >> a >> b >> c;
if (a > b) swap(a, b);
edges.pb(mp(mp(a, b), c));
graph[a].insert(mp(b, c));
graph[b].insert(mp(a, c));
}
subtask3();
subtask4();
if (n <= 5000) {
ll last = 0;
while (q--) {
ll d, e;
cin >> d >> e;
d = (d + last) % (n - 1);
e = (e + last) % w;
auto a = edges[d].first.first, b = edges[d].first.second;
auto &c = edges[d].second;
graph[a].erase(graph[a].find(mp(b, c)));
graph[b].erase(graph[b].find(mp(a, c)));
c = e;
graph[a].insert(mp(b, c));
graph[b].insert(mp(a, c));
df = vf = 0;
find_furthest(1, 1);
df = 0;
find_furthest(vf, vf);
cout << df << endl;
last = df;
}
}
}
int main() {
do_not_disturb
int t = 1;
//~ cin >> t;
while (t--) {
solve();
}
return 0;
}
/*
*/
# | 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... |