# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1105184 | tradz | Dynamic Diameter (CEOI19_diameter) | C++14 | 38 ms | 32180 KiB |
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>
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define For(i,a,b) for(int i = a; i <= b; i++)
#define Ford(i,a,b) for(int i = a; i >= b; i--)
#define ll long long
#define ii pair<int,int>
#define fi first
#define se second
#define all(v) v.begin(),v.end()
#define RRH(v) v.resize(unique(all(v)) - v.begin())
using namespace std;
const int N = 1e6+7;
const int M = 1e9+7;
const ll oo = 3e18;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
long long GetRandom(long long l, long long r) {
return uniform_int_distribution<long long> (l, r)(rng);
}
int n, q;
ll w;
pair <int, int> e[N];
vector<pair<int, ll>> g[N];
namespace sub1 {
ll last = 0, c, len[N];
int d;
void dfs(int u, int pre, int su, int sv) {
for (int i = 0; i < g[u].size(); i++) {
int v;
ll w;
tie(v, w) = g[u][i];
if (v == pre) continue;
if ((u == su) && (v == sv)) {
g[u][i].se = c;
}
else if ((u == sv) && (v == su)) {
g[u][i].se = c;
}
w = g[u][i].se;
len[v] = len[u] + w;
dfs(v, u, su, sv);
}
}
void solve() {
for (int i = 1; i <= n; i++) len[i] = 0;
len[0] = 0;
while (q--) {
cin >> d >> c;
d = (1ll * d + last) % (n - 1);
c = (c + last) % w;
dfs(1, 0, e[d].fi, e[d].se);
int mx = 0;
for (int i = 1; i <= n; i++) {
if (len[mx] < len[i]) {
mx = i;
}
}
len[mx] = 0;
dfs(mx, 0, e[d].fi, e[d].se);
mx = 0;
for (int i = 1; i <= n; i++) {
if (len[mx] < len[i]) {
mx = i;
}
}
if (q > 0) cout << len[mx] << '\n';
else cout << len[mx];
last = len[mx];
}
}
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
#define TASK ""
if (fopen (".inp", "r")) {
freopen (".inp", "r", stdin);
freopen (".out", "w", stdout);
}
if(fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
cin >> n >> q >> w;
For (i, 0, n - 2) {
int u, v;
ll c;
cin >> u >> v >> c;
e[i] = {u, v};
g[u].push_back({v, c});
g[v].push_back({u, c});
}
if (max(n, q) <= 5000) {
sub1 :: solve();
return 0;
}
cerr << "Time elapsed: " << TIME << " s.\n";
return 0;
}
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... |