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>
using namespace std;
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#ifdef LOCAL
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
#define ar array
#define ll long long
#define ld long double
#define sze(x) ((ll)x.size())
#define all(a) (a).begin(), (a).end()
#define sui cout.tie(NULL); cin.tie(NULL); ios_base::sync_with_stdio(false)
#define mset(a, x) memset(a, x, sizeof(a))
#define lid id << 1
#define rid id << 1 | 1
#define mid ((r + l) >> 1)
typedef priority_queue <ll, vector<ll>, greater<ll> > max_heap;
typedef priority_queue <ll> min_heap;
const ll MAX_N = 1e5 + 5;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const ld EPS = 1e-9;
const ll LOG = 30;
pair<ll, ll> seg[MAX_N << 2];
ll ops[MAX_N << 2];
void build(ll l, ll r, ll id)
{
ops[id] = 0;
if (l == r - 1)
{
seg[id] = make_pair(0, l);
return;
}
build(l, mid, lid);
build(mid, r, rid);
seg[id] = max(seg[lid], seg[rid]);
}
void move(ll l, ll r, ll id)
{
if (l == r - 1) return;
seg[lid].first += ops[id];
seg[rid].first += ops[id];
ops[lid] += ops[id];
ops[rid] += ops[id];
ops[id] = 0;
}
void upd(ll s, ll t, ll x, ll l, ll r, ll id)
{
move(l, r, id);
if (s <= l && t >= r)
{
seg[id].first += x;
ops[id] += x;
return;
}
if (s < mid) upd(s, t, x, l, mid, lid);
if (t > mid) upd(s, t, x, mid, r, rid);
seg[id] = max(seg[lid], seg[rid]);
}
pair<ll, ll> get(ll s, ll t, ll l, ll r, ll id)
{
if (s >= t) return make_pair(-INF, -1);
move(l, r, id);
if (s <= l && t >= r) return seg[id];
if (s >= r || t <= l) return make_pair(-INF, -1);
return max(get(s, t, l, mid, lid), get(s, t, mid, r, rid));
}
ll h[MAX_N];
ll st[MAX_N];
ll fi[MAX_N];
vector<pair<ll, ll>> adj[MAX_N];
ll par[MAX_N];
ll dp[MAX_N];
ll maa[MAX_N];
vector<ll> as[MAX_N];
ll timer = 0;
ll n, q, w;
void dfs(ll u, ll p)
{
par[u] = p;
st[u] = timer;
timer++;
maa[u] = h[u];
for (auto v : adj[u]) if (v.first != p) h[v.first] = h[u] + v.second, dfs(v.first, u), maa[u] = max(maa[u], maa[v.first]), as[u].push_back(v.first);
if (as[u].size() >= 2) dp[u] = maa[as[u][0]] + maa[as[u][1]] - 2 * h[u];
else if (as[u].size() == 1) dp[u] = maa[as[u][0]] - h[u];
else dp[u] = 0;
upd(st[u], st[u] + 1, h[u], 0, n, 1);
fi[u] = timer;
}
void solve() {
cin >> n >> q >> w;
vector<pair<ll, pair<ll, ll>>> edges;
for (ll i = 1; i < n; i++)
{
ll u, v, c;
cin >> u >> v >> c;
edges.push_back(make_pair(c, make_pair(u, v)));
adj[u].push_back(make_pair(v, c));
adj[v].push_back(make_pair(u, c));
}
build(0, n, 1);
dfs(1, 0);
multiset<ll> dps;
for (ll i = 1; i <= n; i++) dps.insert(dp[i]);
ll last = 0;
while (q--)
{
ll d, e;
cin >> d >> e;
d = (d + last) % (n - 1);
e = (e + last) % w;
if (h[edges[d].second.first] < h[edges[d].second.second]) swap(edges[d].second.first, edges[d].second.second);
ll dd = e - edges[d].first;
edges[d].first = e;
upd(st[edges[d].second.first], fi[edges[d].second.first], dd, 0, n, 1);
ll u = edges[d].second.second;
while (u)
{
dps.erase(dps.find(dp[u]));
if (as[u].size() >= 2) dp[u] = get(st[as[u][0]], fi[as[u][0]], 0, n, 1).first + get(st[as[u][1]], fi[as[u][1]], 0, n, 1).first - 2 * h[u];
else if (as[u].size() == 1) dp[u] = get(st[as[u][0]], fi[as[u][0]], 0, n, 1).first - h[u];
else dp[u] = 0;
dps.insert(dp[u]);
u = par[u];
}
cout << *dps.rbegin() << "\n";
last = *dps.rbegin();
}
}
int main() {
sui;
ll tc = 1;
//cin >> tc;
for (ll t = 1; t <= tc; t++) {
solve();
}
}
# | 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... |