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;
vector<vector<pair<int, ll>>> adj;
vector<pair<int, int>> pa;
vector<pair<int, ll>> dist;
void init(int n, int q, ll l, vector<int> u, vector<int> v, vector<ll> w) {
pa.resize(n-1);
adj.resize(n);
for(int i = 0; i < n-1; i++) {
u[i]--; v[i]--;
pa[i] = {u[i], v[i]};
adj[u[i]].push_back({v[i], w[i]});
adj[v[i]].push_back({u[i], w[i]});
}
for(int i = 0; i < n; i++) sort(adj[i].begin(), adj[i].end());
dist.resize(n);
}
void dfs(int n, ll dis = 0, int par = -1) {
dist[n] = {dis, n};
for(auto ch : adj[n]) {
if(ch.first == par) continue;
dfs(ch.first, dis + ch.second, n);
}
}
ll get_diameter(int d, ll e) {
int x, y;
tie(x, y) = pa[d];
int l = 0, r = adj[x].size();
while(l != r-1) {
int mid = (l+r)/2;
if(adj[x][mid].first <= y) l = mid;
else r = mid;
}
adj[x][l].second = e;
l = 0; r = adj[y].size();
while(l != r-1) {
int mid = (l+r)/2;
if(adj[y][mid].first <= x) l = mid;
else r = mid;
}
adj[y][l].second = e;
dfs(0);
sort(dist.rbegin(), dist.rend());
dfs(dist[0].second);
sort(dist.rbegin(), dist.rend());
return dist[0].first;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
ll l;
cin >> n >> q >> l;
vector<int> u(n-1), v(n-1);
vector<ll> w(n-1);
for(int i = 0; i < n-1; i++) {
cin >> u[i] >> v[i] >> w[i];
}
init(n, q, l, u, v, w);
ll last = 0;
for(int i = 0; i < q; i++) {
ll d, e;
cin >> d >> e;
d += last;
d %= n-1;
e += last;
e %= l;
last = get_diameter(d, e);
cout << last << '\n';
}
}
# | 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... |