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 startt ios_base::sync_with_stdio(false);cin.tie(0);
typedef long long ll;
using namespace std;
#define vint vector<int>
#define all(v) v.begin(), v.end()
#define MOD 1000000007
#define MOD2 998244353
#define MX 1000000000
#define MXL 1000000000000000000
#define PI (ld)2*acos(0.0)
#define pb push_back
#define sc second
#define fr first
#define int long long
#define endl '\n'
#define ld long double
#define NO cout << "NO" << endl
#define YES cout << "YES" << endl
int ceildiv(int one, int two) {if (one % two == 0) {return one / two;}else {return one / two + 1;}} int power(int n, int pow, int m) {if (pow == 0) return 1;if (pow % 2 == 0) {ll x = power(n, pow / 2, m);return (x * x) % m;}else return (power(n, pow - 1, m) * n) % m;} int gcd(int a, int b) { if (!b)return a; return gcd(b, a % b);} int factorial(int n, int mod) {if (n > 1)return (n * factorial(n - 1, mod)) % mod; else return 1;} int lcm(int a, int b) {return (a * b) / gcd(a, b);} vector<int> read(int n) {vector<int> a; for (int i = 0; i < n; i++) { int x; cin >> x; a.pb(x);} return a;}struct prefix_sum{vint pref;void build(vint a){pref.pb(0);for(int i = 0; i < a.size(); i++){pref.pb(pref.back()+a[i]);}}int get(int l, int r){return pref[r]-pref[l-1];}};//mesanu
int n, q, w;
vector<vector<pair<int, int>>> adj;
vector<int> tin, tout;
vector<int> tour;
void dfs(int u, int p, int dist)
{
tin[u] = tour.size();
tour.push_back(dist);
for(auto v : adj[u])
{
if(v.first != p)
{
dfs(v.first, u, dist+v.second);
tour.push_back(dist);
}
}
tout[u] = tour.size();
tour.push_back(dist);
}
struct node
{
int d_max, d_min, d_left, d_right, ans;
};
vector<node> seg;
vector<int> lazy;
node merge(node a, node b)
{
node temp;
temp.ans = max({a.d_right + b.d_max, b.d_left + a.d_max, a.ans, b.ans});
temp.d_right = max({a.d_right, b.d_right, a.d_max-2*b.d_min});
temp.d_left = max({a.d_left, b.d_left, b.d_max-2*a.d_min});
temp.d_max = max(a.d_max, b.d_max);
temp.d_min = min(a.d_min, b.d_min);
return temp;
}
void build(int l, int r, int v)
{
lazy[v] = 0;
if(l == r)
{
node temp;
temp.d_max = tour[l];
temp.d_min = tour[l];
temp.ans = 0;
temp.d_right = -tour[l];
temp.d_left = -tour[l];
seg[v] = temp;
return;
}
build(l, (l+r)/2, 2*v+1);
build((l+r)/2+1, r, 2*v+2);
seg[v] = merge(seg[2*v+1], seg[2*v+2]);
}
void push(int v, int l, int r)
{
if(lazy[v] == 0)
{
return;
}
seg[v].d_max+=lazy[v];
seg[v].d_min+=lazy[v];
seg[v].d_left-=lazy[v];
seg[v].d_right-=lazy[v];
if(l != r){
lazy[2*v+1]+=lazy[v];
lazy[2*v+2]+=lazy[v];
}
lazy[v] = 0;
}
void modif(int v, int l, int r, int tl, int tr, int val){
push(v, l, r);
if(l > tr || r < tl)return;
if(l >= tl && r <= tr){
lazy[v]+=val;
push(v, l , r);
return;
}
int mid = (l + r) >> 1;
modif(2 * v + 1, l, mid, tl, tr, val);
modif(2 * v + 2, mid + 1, r, tl, tr, val);
seg[v] = merge(seg[2*v+1], seg[2*v+2]);
}
int32_t main(){
startt
cin >> n >> q >> w;
adj.resize(n+1);
tin.resize(n+1);
tout.resize(n+1);
map<pair<int, int>, int> mp;
vector<pair<pair<int, int>, int>> ww;
for(int i = 0; i < n-1; i++)
{
int u, v, w;
cin >> u >> v >> w;
adj[u].pb({v, w});
adj[v].pb({u, w});
ww.push_back({{u, v}, w});
}
dfs(1, 0, 0);
seg.resize(4*tour.size()+1000);
lazy.resize(4*tour.size()+1000);
build(0, tour.size(), 0);
//cout << seg[0].ans << endl;
int last = 0;
for(int i = 0; i < q; i++)
{
int dd, ee;
cin >> dd >> ee;
int d = (dd+last)%(n-1), e = (ee+last)%w;
if(n == 2)
{
cout << e << endl;
last = e;
continue;
}
if(tin[ww[d].fr.fr] > tin[ww[d].fr.sc])
{
swap(ww[d].fr.fr, ww[d].fr.sc);
}
modif(0, 0, tour.size(), tin[ww[d].fr.sc], tout[ww[d].fr.sc], e-ww[d].sc);
ww[d].sc = e;
last = max(seg[0].ans, seg[0].d_max);
cout << last << endl;
}
}
Compilation message (stderr)
diameter.cpp: In member function 'void prefix_sum::build(std::vector<long long int>)':
diameter.cpp:20:667: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | int ceildiv(int one, int two) {if (one % two == 0) {return one / two;}else {return one / two + 1;}} int power(int n, int pow, int m) {if (pow == 0) return 1;if (pow % 2 == 0) {ll x = power(n, pow / 2, m);return (x * x) % m;}else return (power(n, pow - 1, m) * n) % m;} int gcd(int a, int b) { if (!b)return a; return gcd(b, a % b);} int factorial(int n, int mod) {if (n > 1)return (n * factorial(n - 1, mod)) % mod; else return 1;} int lcm(int a, int b) {return (a * b) / gcd(a, b);} vector<int> read(int n) {vector<int> a; for (int i = 0; i < n; i++) { int x; cin >> x; a.pb(x);} return a;}struct prefix_sum{vint pref;void build(vint a){pref.pb(0);for(int i = 0; i < a.size(); i++){pref.pb(pref.back()+a[i]);}}int get(int l, int r){return pref[r]-pref[l-1];}};//mesanu
| ~~^~~~~~~~~~
# | 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... |