제출 #1262484

#제출 시각아이디문제언어결과실행 시간메모리
1262484EntityPlanttDynamic Diameter (CEOI19_diameter)C++20
31 / 100
1263 ms14044 KiB
#include <bits/stdc++.h>
using namespace std;

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int n, q;
	int64_t w, last = 0;
	cin >> n >> q >> w;
	vector<pair<int, int>> g[n];
	int edga[n - 1], edgb[n - 1];
	int64_t weight[n - 1];
	for (int i = 0; i < n - 1; i++) {
		cin >> edga[i] >> edgb[i] >> weight[i];
		g[--edga[i]].push_back({--edgb[i], i});
		g[edgb[i]].push_back({edga[i], i});
	}
	if (n < 5001) {
		const function<pair<int64_t, int>(int, int)> dfs = [&](int u, int p) {
			pair ans{0LL, u};
			for (auto &[v, e] : g[u]) {
				if (v == p) continue;
				pair p = dfs(v, u);
				ans = max(ans, {p.first + weight[e], p.second});
			}
			return ans;
		};
		while (q--) {
			int d;
			int64_t e;
			cin >> d >> e;
			weight[d = (d + last) % (n - 1)] = e = (e + last) % w;
			cout << (last = dfs(dfs(0, 0).second, -1).first) << '\n';
		}
		return 0;
	}
	multiset<int64_t> lens(weight, weight + n - 1);
	lens.insert(0);
	while (q--) {
		int d;
		int64_t e;
		cin >> d >> e;
		d = (d + last) % (n - 1);
		e = (e + last) % w;
		lens.erase(lens.find(weight[d]));
		lens.insert(weight[d] = e);
		cout << (last = *lens.rbegin() + *next(lens.rbegin())) << '\n';
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...