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 <stdio.h>
#include <stdlib.h>
#define N 100000
#define N_ (1 << 18) /* N_ = pow2(ceil(log2((N - 1) * 2))) */
long long max(long long a, long long b) { return a > b ? a : b; }
int *eh[N], eo[N];
void append(int i, int h) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
eh[i] = (int *) realloc(eh[i], o * 2 * sizeof *eh[i]);
eh[i][o] = h;
}
int ii[N - 1], jj[N - 1], ta[N - 1], tb[N - 1]; long long ww[N - 1], aa[(N - 1) * 2];
void dfs(int p, int i) {
static int time;
int o;
for (o = eo[i]; o--; ) {
int h = eh[i][o], j = i ^ ii[h] ^ jj[h];
if (j != p) {
ii[h] = i, jj[h] = j;
ta[h] = time++;
dfs(i, j);
tb[h] = time++;
aa[ta[h]] = ww[h], aa[tb[h]] = -ww[h];
}
}
}
long long st1[N_ * 2], st2[N_ * 2], st01[N_ * 2], st12[N_ * 2], st23[N_ * 2], st02[N_ * 2], st13[N_ * 2], st03[N_ * 2]; int n_;
void pul(int i) {
int l = i << 1 | 0, r = l | 1;
st1[i] = st1[l] + st1[r];
st2[i] = st2[l] + st2[r];
st01[i] = max(st01[r], st01[l] + st1[r]);
st12[i] = max(st1[l] + st12[r], st12[l] + st2[r]);
st23[i] = max(st2[l] + st23[r], st23[l]);
st02[i] = max(max(st02[r], st01[l] + st12[r]), st02[l] + st2[r]);
st13[i] = max(max(st1[l] + st13[r], st12[l] + st23[r]), st13[l]);
st03[i] = max(max(max(st03[r], st01[l] + st13[r]), st02[l] + st23[r]), st03[l]);
}
void build(long long *aa, int n) {
int i;
n_ = 1;
while (n_ < n)
n_ <<= 1;
for (i = 0; i < n; i++) {
st1[n_ + i] = -aa[i];
st2[n_ + i] = aa[i];
st01[n_ + i] = max(0, -aa[i]);
st23[n_ + i] = max(aa[i], 0);
st12[n_ + i] = st02[n_ + i] = st13[n_ + i] = st03[n_ + i] = max(-aa[i], aa[i]);
}
for (i = n_ - 1; i > 0; i--)
pul(i);
}
void update(int i, long long w) {
i += n_;
st1[i] = -w;
st2[i] = w;
st01[i] = max(0, -w);
st23[i] = max(w, 0);
st12[i] = st02[i] = st13[i] = st03[i] = max(-w, w);
while (i > 1)
pul(i >>= 1);
}
int main() {
int n, q, h, i;
long long w_, last;
scanf("%d%d%lld", &n, &q, &w_);
for (i = 0; i < n; i++)
eh[i] = (int *) malloc(2 * sizeof *eh[i]);
for (h = 0; h < n - 1; h++) {
scanf("%d%d%lld", &ii[h], &jj[h], &ww[h]), ii[h]--, jj[h]--;
append(ii[h], h), append(jj[h], h);
}
dfs(-1, 0);
build(aa, (n - 1) * 2);
last = 0;
while (q--) {
int h;
long long w;
scanf("%d%lld", &h, &w), h = (h + last) % (n - 1), w = (w + last) % w_;
ww[h] = w;
update(ta[h], ww[h]), update(tb[h], -ww[h]);
printf("%lld\n", last = st03[1]);
}
return 0;
}
Compilation message (stderr)
diameter.c: In function 'append':
diameter.c:14:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
14 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
diameter.c: In function 'main':
diameter.c:85:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
85 | scanf("%d%d%lld", &n, &q, &w_);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
diameter.c:89:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | scanf("%d%d%lld", &ii[h], &jj[h], &ww[h]), ii[h]--, jj[h]--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
diameter.c:99:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
99 | scanf("%d%lld", &h, &w), h = (h + last) % (n - 1), w = (w + last) % w_;
| ^~~~~~~~~~~~~~~~~~~~~~~
# | 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... |