#include <stdio.h>
#include <stdlib.h>
#define N 200000
#define N_ (1 << 18) /* N_ = pow2(ceil(log2(N + 1))) */
long long max(long long a, long long b) { return a > b ? a : b; }
int *ej[N], eo[N];
void append(int i, int j) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
ej[i][o] = j;
}
int dd[N], pp[N], qq[N], ta[N], tb[N];
int dfs1(int i, int d) {
int o, s, k_, j_;
dd[i] = d;
s = 1, k_ = 0, j_ = -1;
for (o = eo[i]; o--; ) {
int j = ej[i][o], k = dfs1(j, d + 1);
s += k;
if (k_ < k)
k_ = k, j_ = j;
}
qq[i] = j_;
return s;
}
void dfs2(int i, int q) {
static int time;
int o, j_;
ta[i] = time++;
j_ = qq[i], qq[i] = q;
if (j_ != -1)
dfs2(j_, q);
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != j_)
dfs2(j, j);
}
tb[i] = time;
}
long long st[N_ * 2], lz[N_]; int h_, n_;
void put(int i, long long x) {
st[i] = x;
if (i < n_)
lz[i] = x;
}
void pus(int i) {
if (lz[i])
put(i << 1 | 0, lz[i]), put(i << 1 | 1, lz[i]), lz[i] = 0;
}
void pul(int i) {
if (!lz[i])
st[i] = max(st[i << 1 | 0], st[i << 1 | 1]);
}
void push(int i) {
int h;
for (h = h_; h > 0; h--)
pus(i >> h);
}
void pull(int i) {
while (i > 1)
pul(i >>= 1);
}
void build(int *aa, int n) {
int i;
h_ = 0;
while (1 << h_ <= n)
h_++;
n_ = 1 << h_;
for (i = 0; i < n; i++)
st[n_ + ta[i]] = aa[i];
for (i = n_ - 1; i > 0; i--)
pul(i);
}
void update(int l, int r, long long x) {
int l_ = l += n_, r_ = r += n_;
push(l_), push(r_);
for ( ; l <= r; l >>= 1, r >>= 1) {
if ((l & 1) == 1)
put(l++, x);
if ((r & 1) == 0)
put(r--, x);
}
pull(l_), pull(r_);
}
int prev(int r, long long x) {
int l = 0;
push(l += n_), push(r += n_);
for ( ; l <= r; l >>= 1, r >>= 1)
if ((r & 1) == 0) {
if (st[r] >= x) {
while (r < n_) {
pus(r);
r = st[r << 1 | 1] >= x ? r << 1 | 1 : r << 1 | 0;
}
return r - n_;
}
r--;
}
return -1;
}
long long query(int i) {
pus(i += n_);
return st[i];
}
int main() {
static int aa[N];
int n, q, i, a;
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d", &aa[i]);
for (i = 0; i < n; i++)
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
for (i = 0; i + 1 < n; i++) {
scanf("%d", &pp[i]), pp[i]--;
append(pp[i], i);
}
dfs1(n - 1, 0);
dfs2(n - 1, n - 1);
build(aa, n);
scanf("%d", &q);
while (q--) {
static char s[2];
scanf("%s", s);
if (s[0] == 'I') {
scanf("%d", &i), i--;
printf("%lld\n", query(ta[i]));
} else {
long long x;
scanf("%d%lld", &i, &x), i--, x += query(ta[i]);
while (i >= 0) {
if ((a = prev(ta[i], x)) >= ta[qq[i]]) {
if (a < ta[i])
update(a + 1, ta[i], x);
break;
}
update(ta[qq[i]], ta[i], x);
i = pp[qq[i]];
}
}
}
return 0;
}
Compilation message
lektuvai.c: In function 'append':
lektuvai.c:14:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
14 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
lektuvai.c: In function 'main':
lektuvai.c:137:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
137 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
lektuvai.c:139:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
139 | scanf("%d", &aa[i]);
| ^~~~~~~~~~~~~~~~~~~
lektuvai.c:143:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
143 | scanf("%d", &pp[i]), pp[i]--;
| ^~~~~~~~~~~~~~~~~~~
lektuvai.c:149:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
149 | scanf("%d", &q);
| ^~~~~~~~~~~~~~~
lektuvai.c:153:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
153 | scanf("%s", s);
| ^~~~~~~~~~~~~~
lektuvai.c:155:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
155 | scanf("%d", &i), i--;
| ^~~~~~~~~~~~~~~
lektuvai.c:160:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
160 | scanf("%d%lld", &i, &x), i--, x += query(ta[i]);
| ^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
8540 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
8540 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
8540 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
8540 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |