Submission #472633

#TimeUsernameProblemLanguageResultExecution timeMemory
472633OzyDynamic Diameter (CEOI19_diameter)C++17
0 / 100
469 ms8356 KiB
#include <iostream> #include <bits/stdc++.h> using namespace std; #define lli long long int #define rep(i,a,b) for (int i = (a); i <= (b); i++) #define repa(i,a,b) for (int i = (a); i >= (b); i--) #define debug(a) cout << #a << " = " << a << endl #define debugsl(a) cout << #a << " = " << a << ", " #define MAX 100000 lli n,q,w,a,b,c,last,id,lim; lli peso[MAX+2]; pair<lli,lli> arr[MAX+2]; multiset<lli> raices; multiset<lli>::iterator it; lli prof[(2*MAX)+2],padres[(2*MAX)+2]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> q >> w; rep(i,1,n-1) { cin >> a >> b >> c; arr[i] = {a,b}; peso[i] = c; if (a > b) swap(a,b); padres[b] = c; } repa(i,n/2,1) { a = prof[i*2] + padres[i*2]; if ((i*2)+1 <= n) b = prof[(i*2)+1] + padres[(i*2)+1]; else b = 0; prof[i] = max(a,b); c = a+b; raices.insert(-c); } lim = 1ll << 62; rep(i,1,q) { //correcto cin >> id >> c; id += last; id %= (n-1); id++; c += last; c %= w; a = arr[id].first; b = arr[id].second; //fill if (a > b) swap(a,b); padres[b] = c; lli x,y; while (a > 0) { x = prof[(a*2)] + prof[(a*2)+1] + padres[(a*2)] + padres[(a*2)+1]; it = raices.lower_bound(-x); raices.erase(it); x = prof[a*2] + padres[a*2]; y = prof[(a*2)+1] + padres[(a*2)+1]; prof[a] = max(x,y); x += y; raices.insert(-x); b = a; a /= 2; } a = *raices.lower_bound(-lim); a *= -1; cout << a << "\n"; last = a; } }
#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...