This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define wiwihorz
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma loop-opt(on)
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define rrep(i, a, b) for(int i = b; i >= a; i--)
#define ceil(a, b) ((a + b - 1) / (b))
#define all(x) x.begin(), x.end()
#define INF 1000000000000000000
#define MOD 1000000007
#define eps (1e-9)
using namespace std;
#define int long long int
#define lld long double
#define pii pair<int, int>
#define random mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count())
#ifdef wiwihorz
#define print(a...) kout("[" + string(#a) + "] = ", a)
void vprint(auto L, auto R) {while(L < R) cerr << *L << " \n"[next(L) == R], ++L;}
void kout() { cerr << endl; }
template<class T1, class ... T2> void kout(T1 a, T2 ... e) { cerr << a << " ", kout(e...); }
#else
#define print(...) 0
#define vprint(...) 0
#endif
namespace solver {
int n, ans;
vector<int> es, cost, dp;
vector<vector<int>> mp;
void init_(int _n) {
n = _n;
mp.assign(n + 1, vector<int>());
es.clear();
cost.clear();
dp.assign(n + 1, 0);
}
void add_edge(int a, int b, int c) {
mp[a].push_back(es.size());
mp[b].push_back(es.size());
es.push_back(a ^ b);
cost.push_back(c);
}
void dfs(int x, int par) {
dp[x] = 0;
int v1 = 0, v2 = 0;
for(auto i : mp[x]) if(i != par) {
int to = es[i] ^ x;
dfs(to, i);
int vv = dp[to] + cost[i];
dp[x] = max(dp[x], vv);
if(vv > v1) swap(vv, v1);
if(vv > v2) swap(vv, v2);
}
ans = max(ans, v1 + v2);
}
int query(int id, int c) {
ans = 0, cost[id] = c;
dfs(1, -1);
return ans;
}
};
using namespace solver;
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
int n, q, w, last = 0;
cin >> n >> q >> w, init_(n);
rep(i, 1, n - 1) {
int a, b, c;
cin >> a >> b >> c;
add_edge(a, b, c);
}
while(q --) {
int x, y; cin >> x >> y;
x = (x + last) % (n - 1);
y = (y + last) % w;
int ans = query(x, y);
last = ans;
cout << ans << "\n";
}
return 0;
}
Compilation message (stderr)
diameter.cpp:4: warning: ignoring '#pragma loop ' [-Wunknown-pragmas]
4 | #pragma loop-opt(on)
|
diameter.cpp:23:13: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
23 | void vprint(auto L, auto R) {while(L < R) cerr << *L << " \n"[next(L) == R], ++L;}
| ^~~~
diameter.cpp:23:21: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
23 | void vprint(auto L, auto R) {while(L < R) cerr << *L << " \n"[next(L) == R], ++L;}
| ^~~~
# | 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... |