#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) {
push(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);
}
pp[n - 1] = -1;
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:150:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
150 | scanf("%d", &q);
| ^~~~~~~~~~~~~~~
lektuvai.c:154:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
154 | scanf("%s", s);
| ^~~~~~~~~~~~~~
lektuvai.c:156:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
156 | scanf("%d", &i), i--;
| ^~~~~~~~~~~~~~~
lektuvai.c:161:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
161 | scanf("%d%lld", &i, &x), i--, x += query(ta[i]);
| ^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8540 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
2 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8540 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
2 ms |
8540 KB |
Output is correct |
12 |
Correct |
2 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8540 KB |
Output is correct |
14 |
Correct |
2 ms |
8540 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
2 ms |
8540 KB |
Output is correct |
17 |
Correct |
2 ms |
8540 KB |
Output is correct |
18 |
Correct |
2 ms |
8540 KB |
Output is correct |
19 |
Correct |
2 ms |
8540 KB |
Output is correct |
20 |
Correct |
2 ms |
8540 KB |
Output is correct |
21 |
Correct |
1 ms |
8540 KB |
Output is correct |
22 |
Correct |
1 ms |
8540 KB |
Output is correct |
23 |
Correct |
2 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8540 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
2 ms |
8540 KB |
Output is correct |
12 |
Correct |
42 ms |
26196 KB |
Output is correct |
13 |
Correct |
123 ms |
36400 KB |
Output is correct |
14 |
Correct |
93 ms |
38440 KB |
Output is correct |
15 |
Correct |
112 ms |
38908 KB |
Output is correct |
16 |
Correct |
110 ms |
39164 KB |
Output is correct |
17 |
Correct |
88 ms |
37320 KB |
Output is correct |
18 |
Correct |
96 ms |
37656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8540 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Correct |
1 ms |
8540 KB |
Output is correct |
12 |
Correct |
2 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8536 KB |
Output is correct |
14 |
Correct |
2 ms |
8540 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
1 ms |
8540 KB |
Output is correct |
17 |
Correct |
1 ms |
8540 KB |
Output is correct |
18 |
Correct |
2 ms |
8540 KB |
Output is correct |
19 |
Correct |
1 ms |
8540 KB |
Output is correct |
20 |
Correct |
2 ms |
8540 KB |
Output is correct |
21 |
Correct |
2 ms |
8540 KB |
Output is correct |
22 |
Correct |
2 ms |
8540 KB |
Output is correct |
23 |
Correct |
2 ms |
8540 KB |
Output is correct |
24 |
Correct |
42 ms |
26196 KB |
Output is correct |
25 |
Correct |
123 ms |
36400 KB |
Output is correct |
26 |
Correct |
93 ms |
38440 KB |
Output is correct |
27 |
Correct |
112 ms |
38908 KB |
Output is correct |
28 |
Correct |
110 ms |
39164 KB |
Output is correct |
29 |
Correct |
88 ms |
37320 KB |
Output is correct |
30 |
Correct |
96 ms |
37656 KB |
Output is correct |
31 |
Correct |
50 ms |
19440 KB |
Output is correct |
32 |
Correct |
113 ms |
21668 KB |
Output is correct |
33 |
Correct |
92 ms |
38460 KB |
Output is correct |
34 |
Correct |
124 ms |
36488 KB |
Output is correct |
35 |
Correct |
108 ms |
31192 KB |
Output is correct |
36 |
Correct |
131 ms |
22968 KB |
Output is correct |
37 |
Correct |
98 ms |
24080 KB |
Output is correct |
38 |
Correct |
73 ms |
20660 KB |
Output is correct |
39 |
Correct |
77 ms |
26672 KB |
Output is correct |
40 |
Correct |
81 ms |
21844 KB |
Output is correct |