| # | Time | Username | Problem | Language | Result | Execution time | Memory | 
|---|---|---|---|---|---|---|---|
| 1156682 | SmuggingSpun | Dynamic Diameter (CEOI19_diameter) | C++20 | 1387 ms | 4552 KiB | 
#include<bits/stdc++.h>
#define taskname "C"
using namespace std;
typedef long long ll;
const int lim = 1e5 + 5;
int n, q, u[lim], v[lim], w[lim];
ll ans = 0, BOUND_W;
vector<int>g[lim];
namespace sub12{
	const int lim = 5e3 + 5;
	int vertex;
	ll f[lim];
	void dfs(int s, int p){
		if(f[s] > f[vertex]){
			vertex = s;
		}
		for(int& i : g[s]){
			int d = u[i] ^ v[i] ^ s;
			if(d != p){
				f[d] = f[s] + w[i];
				dfs(d, s);
			}
		}
	}
	void solve(){
		for(int _ = 0; _ < q; _++){
			int d;
			ll e;
			cin >> d >> e;
			w[(ans + ll(d)) % (n - 1) + 1] = (ans + ll(e)) % BOUND_W;
			dfs(vertex = 1, f[1] = 0);
			dfs(vertex, f[vertex] = 0);
			cout << (ans = f[vertex]) << "\n";		
		}
	}
}
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	if(fopen(taskname".inp", "r")){
		freopen(taskname".inp", "r", stdin);
	}
	cin >> n >> q >> BOUND_W;
	for(int i = 1; i < n; i++){
		cin >> u[i] >> v[i] >> w[i];
		g[u[i]].emplace_back(i);
		g[v[i]].emplace_back(i);
	}
	if(max(n, q) <= 5000){
		sub12::solve();
	}
}
Compilation message (stderr)
| # | 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... | ||||
