이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define REPD(i, a) for (int i = (a) - 1; i >= 0; --i)
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
/*
Phu Trong from Nguyen Tat Thanh High School for gifted student
*/
template <class X, class Y>
bool minimize(X &x, const Y &y){
X eps = 1e-9;
if (x > y + eps) {x = y; return 1;}
return 0;
}
template <class X, class Y>
bool maximize(X &x, const Y &y){
X eps = 1e-9;
if (x + eps < y) {x = y; return 1;}
return 0;
}
#define MIN_HIGH(u, v) (depth[u] < depth[v] ? (u) : (v))
#define MAX 100005
#define LOG 17
int numNode, numQuery, base;
struct Edge{
int u, v, w;
Edge(){}
Edge(int _u, int _v, int _w): u(_u), v(_v), w(_w){}
int other(int x){
return (x ^ u ^ v);
}
} edge[MAX];
vector<int> G[MAX];
int tin[MAX], tout[MAX], st[MAX << 1][LOG], pos[MAX], rev[MAX], depth[MAX];
int timeDfs = 0, cnt = 0;
void pre_dfs(int u, int p = -1){
tin[u] = ++timeDfs;
st[++cnt][0] = u;
pos[u] = cnt; rev[timeDfs] = u;
for (int&i : G[u]) {
int v = edge[i].other(u);
if(v ^ p){
depth[v] = depth[u] + 1;
pre_dfs(v, u);
st[++cnt][0] = u;
}
}
tout[u] = timeDfs;
}
struct FenwickTree{
vector<int> F;
int n;
void init(int _n = 0){
this -> n = _n;
F.resize(n + 5);
}
int low_bit(int p){
return p & -p;
}
void upd(int p, int v){
for (; p <= n; p += low_bit(p)) F[p] += v;
}
int ask(int p){
int res = 0;
for (; p > 0; p -= low_bit(p)) res += F[p];
return res;
}
void upd(int l, int r, int v){
upd(l, v); upd(r + 1, -v);
}
}ft;
void build(void){
for (int k = 1; MASK(k) <= cnt; ++k){
for (int i = 1; i + MASK(k) - 1 <= cnt; ++i){
st[i][k] = MIN_HIGH(st[i][k - 1], st[i + MASK(k - 1)][k - 1]);
}
}
}
int lca(int u, int v){
int l = pos[u], r = pos[v];
if (l > r) swap(l, r);
int k = 31 - __builtin_clz(r - l + 1);
return MIN_HIGH(st[l][k], st[r - MASK(k) + 1][k]);
}
int dist(int u, int v){
return ft.ask(tin[u]) + ft.ask(tin[v]) - 2 * ft.ask(tin[lca(u, v)]);
}
#define lc (nd << 1)
#define rc (nd << 1 | 1)
struct Node{
int u, v, d;
Node(){}
Node(int _u, int _v, int _d): u(_u), v(_v), d(_d){}
bool operator < (const Node &o) const {
return d < o.d;
}
friend Node operator + (const Node& a, const Node& b){
Node res = max(a, b);
res = max(res, Node(a.u, b.u, dist(a.u, b.u)));
res = max(res, Node(a.u, b.v, dist(a.u, b.v)));
res = max(res, Node(a.v, b.u, dist(a.v, b.u)));
res = max(res, Node(a.v, b.v, dist(a.v, b.v)));
return res;
}
};
Node seg[MAX << 2];
void build(int nd, int l, int r){
if(l == r){
seg[nd] = Node(rev[l], rev[l], 0);
return;
}
int m = (l + r) >> 1;
build(lc, l, m);
build(rc, m + 1, r);
seg[nd] = seg[lc] + seg[rc];
}
void upd(int nd, int l, int r, int u, int v){
if (u > r || v < l) return;
if (u <= l and v >= r) return;
int m = (l + r) >> 1;
upd(lc, l, m, u, v);
upd(rc, m + 1, r, u, v);
seg[nd] = seg[lc] + seg[rc];
}
void process(void){
cin >> numNode >> numQuery >> base;
for (int i = 1; i < numNode; ++i){
cin >> edge[i].u >> edge[i].v >> edge[i].w;
G[edge[i].u].emplace_back(i);
G[edge[i].v].emplace_back(i);
}
pre_dfs(1);
build();
ft.init(numNode);
for (int i = 1; i < numNode; ++i){
if(depth[edge[i].u] < depth[edge[i].v]) swap(edge[i].u, edge[i].v);
ft.upd(tin[edge[i].u], tout[edge[i].u], edge[i].w);
}
build(1, 1, numNode);
int last = 0;
for (int i = 1; i <= numQuery; ++i){
int id, new_w; cin >> id >> new_w;
id = (id + last) % (numNode - 1); ++id;
new_w = (new_w + last) % base;
ft.upd(tin[edge[id].u], tout[edge[id].u], new_w - edge[id].w);
edge[id].w = new_w;
upd(1, 1, numNode, tin[edge[id].u], tout[edge[id].u]);
cout << (last = seg[1].d) << '\n';
}
}
signed main(){
#define name "Whisper"
cin.tie(nullptr) -> sync_with_stdio(false);
//freopen(name".inp", "r", stdin);
//freopen(name".out", "w", stdout);
process();
return (0 ^ 0);
}
# | 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... |