Submission #768821

# Submission time Handle Problem Language Result Execution time Memory
768821 2023-06-28T17:00:17 Z rainboy Birthday gift (IZhO18_treearray) C
100 / 100
558 ms 50932 KB
#include <stdio.h>
#include <stdlib.h>

#define N	200000
#define M	200000
#define Q	200000
#define N_	(M * 2 + Q * 3 + 1)
#define INF	0x3f3f3f3f

unsigned int X = 12345;

int rand_() {
	return (X *= 3) >> 1;
}

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], qu[N], cnt;

int dfs1(int p, int i, int d) {
	int o, s, k_, j_;

	pp[i] = p, dd[i] = d;
	s = 1, k_ = 0, j_ = -1;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p) {
			int k = dfs1(i, j, d + 1);

			s += k;
			if (k_ < k)
				k_ = k, j_ = j;
		}
	}
	qq[i] = j_;
	return s;
}

void dfs2(int p, int i, int q) {
	int o, j_;

	j_ = qq[i], qq[i] = q;
	if (j_ != -1)
		dfs2(i, j_, q);
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p && j != j_)
			dfs2(i, j, j);
	}
}

int lca(int i, int j) {
	while (qq[i] != qq[j])
		if (dd[qq[i]] > dd[qq[j]])
			i = pp[qq[i]];
		else
			j = pp[qq[j]];
	return dd[i] < dd[j] ? i : j;
}

int zz[N_], ll[N_], rr[N_], kk[N_], u_, l_, r_;

int node(int k) {
	static int _ = 1;

	zz[_] = rand_();
	kk[_] = k;
	return _++;
}

void split(int u, int k) {
	if (u == 0) {
		u_ = l_ = r_ = 0;
		return;
	}
	if (kk[u] < k) {
		split(rr[u], k);
		rr[u] = l_, l_ = u;
	} else if (kk[u] > k) {
		split(ll[u], k);
		ll[u] = r_, r_ = u;
	} else {
		u_ = u, l_ = ll[u], r_ = rr[u];
		ll[u] = rr[u] = 0;
	}
}

int merge(int u, int v) {
	if (u == 0)
		return v;
	if (v == 0)
		return u;
	if (zz[u] < zz[v]) {
		rr[u] = merge(rr[u], v);
		return u;
	} else {
		ll[v] = merge(u, ll[v]);
		return v;
	}
}

int tr_add(int u, int k) {
	split(u, k);
	return merge(merge(l_, node(k)), r_);
}

int tr_remove(int u, int k) {
	split(u, k);
	return merge(l_, r_);
}

int tr_ceil(int u, int k) {
	int k_ = INF;

	while (u)
		if (kk[u] >= k)
			k_ = kk[u], u = ll[u];
		else
			u = rr[u];
	return k_;
}

int main() {
	static int ii[M], tt[N];
	int n, m, q, h, i, j, l, r;

	scanf("%d%d%d", &n, &m, &q);
	for (i = 0; i < n; i++)
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
	for (h = 0; h < n - 1; h++) {
		scanf("%d%d", &i, &j), i--, j--;
		append(i, j), append(j, i);
	}
	dfs1(-1, 0, 0);
	dfs2(-1, 0, 0);
	for (h = 0; h < m; h++)
		scanf("%d", &ii[h]), ii[h]--;
	for (h = 0; h < m; h++) {
		i = ii[h];
		tt[i] = merge(tt[i], node(h * 2));
		if (h + 1 < m) {
			i = lca(ii[h], ii[h + 1]);
			tt[i] = merge(tt[i], node(h * 2 + 1));
		}
	}
	while (q--) {
		int t;

		scanf("%d", &t);
		if (t == 1) {
			scanf("%d%d", &h, &j), h--, j--;
			i = ii[h];
			tt[i] = tr_remove(tt[i], h * 2);
			if (h > 0) {
				i = lca(ii[h - 1], ii[h]);
				tt[i] = tr_remove(tt[i], h * 2 - 1);
			}
			if (h + 1 < m) {
				i = lca(ii[h], ii[h + 1]);
				tt[i] = tr_remove(tt[i], h * 2 + 1);
			}
			ii[h] = j;
			i = ii[h];
			tt[i] = tr_add(tt[i], h * 2);
			if (h > 0) {
				i = lca(ii[h - 1], ii[h]);
				tt[i] = tr_add(tt[i], h * 2 - 1);
			}
			if (h + 1 < m) {
				i = lca(ii[h], ii[h + 1]);
				tt[i] = tr_add(tt[i], h * 2 + 1);
			}
		} else {
			scanf("%d%d%d", &l, &r, &i), l--, r--, i--;
			if ((h = tr_ceil(tt[i], l * 2)) <= r * 2)
				printf("%d %d\n", h / 2 + 1, (h + 1) / 2 + 1);
			else
				printf("-1 -1\n");
		}
	}
	return 0;
}

Compilation message

treearray.c: In function 'append':
treearray.c:21:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   21 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
treearray.c: In function 'main':
treearray.c:137:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  137 |  scanf("%d%d%d", &n, &m, &q);
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
treearray.c:141:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  141 |   scanf("%d%d", &i, &j), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~
treearray.c:147:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  147 |   scanf("%d", &ii[h]), ii[h]--;
      |   ^~~~~~~~~~~~~~~~~~~
treearray.c:159:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  159 |   scanf("%d", &t);
      |   ^~~~~~~~~~~~~~~
treearray.c:161:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  161 |    scanf("%d%d", &h, &j), h--, j--;
      |    ^~~~~~~~~~~~~~~~~~~~~
treearray.c:184:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  184 |    scanf("%d%d%d", &l, &r, &i), l--, r--, i--;
      |    ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB n=5
2 Correct 1 ms 292 KB n=100
3 Correct 1 ms 340 KB n=100
4 Correct 0 ms 340 KB n=100
5 Correct 1 ms 340 KB n=100
6 Correct 0 ms 340 KB n=100
7 Correct 1 ms 340 KB n=100
8 Correct 0 ms 288 KB n=100
9 Correct 0 ms 340 KB n=100
10 Correct 0 ms 340 KB n=100
11 Correct 1 ms 340 KB n=100
12 Correct 1 ms 380 KB n=100
13 Correct 1 ms 340 KB n=100
14 Correct 0 ms 340 KB n=100
15 Correct 0 ms 340 KB n=100
16 Correct 1 ms 340 KB n=100
17 Correct 0 ms 340 KB n=100
18 Correct 0 ms 340 KB n=100
19 Correct 1 ms 288 KB n=100
20 Correct 0 ms 340 KB n=100
21 Correct 1 ms 288 KB n=100
22 Correct 1 ms 340 KB n=100
23 Correct 0 ms 340 KB n=100
24 Correct 0 ms 340 KB n=100
25 Correct 1 ms 340 KB n=100
26 Correct 0 ms 340 KB n=12
27 Correct 0 ms 340 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB n=5
2 Correct 1 ms 292 KB n=100
3 Correct 1 ms 340 KB n=100
4 Correct 0 ms 340 KB n=100
5 Correct 1 ms 340 KB n=100
6 Correct 0 ms 340 KB n=100
7 Correct 1 ms 340 KB n=100
8 Correct 0 ms 288 KB n=100
9 Correct 0 ms 340 KB n=100
10 Correct 0 ms 340 KB n=100
11 Correct 1 ms 340 KB n=100
12 Correct 1 ms 380 KB n=100
13 Correct 1 ms 340 KB n=100
14 Correct 0 ms 340 KB n=100
15 Correct 0 ms 340 KB n=100
16 Correct 1 ms 340 KB n=100
17 Correct 0 ms 340 KB n=100
18 Correct 0 ms 340 KB n=100
19 Correct 1 ms 288 KB n=100
20 Correct 0 ms 340 KB n=100
21 Correct 1 ms 288 KB n=100
22 Correct 1 ms 340 KB n=100
23 Correct 0 ms 340 KB n=100
24 Correct 0 ms 340 KB n=100
25 Correct 1 ms 340 KB n=100
26 Correct 0 ms 340 KB n=12
27 Correct 0 ms 340 KB n=100
28 Correct 1 ms 428 KB n=500
29 Correct 1 ms 468 KB n=500
30 Correct 1 ms 432 KB n=500
31 Correct 1 ms 340 KB n=500
32 Correct 1 ms 340 KB n=500
33 Correct 1 ms 436 KB n=500
34 Correct 1 ms 340 KB n=500
35 Correct 1 ms 468 KB n=500
36 Correct 1 ms 340 KB n=500
37 Correct 1 ms 340 KB n=500
38 Correct 1 ms 340 KB n=500
39 Correct 1 ms 300 KB n=500
40 Correct 1 ms 340 KB n=500
41 Correct 1 ms 340 KB n=500
42 Correct 1 ms 340 KB n=500
43 Correct 1 ms 424 KB n=500
44 Correct 1 ms 340 KB n=500
45 Correct 1 ms 340 KB n=500
46 Correct 1 ms 468 KB n=500
47 Correct 1 ms 468 KB n=500
48 Correct 1 ms 428 KB n=500
49 Correct 1 ms 352 KB n=500
50 Correct 1 ms 340 KB n=500
51 Correct 1 ms 432 KB n=500
52 Correct 1 ms 468 KB n=500
53 Correct 1 ms 340 KB n=500
54 Correct 1 ms 468 KB n=500
55 Correct 1 ms 340 KB n=278
56 Correct 1 ms 468 KB n=500
57 Correct 1 ms 468 KB n=500
58 Correct 1 ms 304 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB n=5
2 Correct 1 ms 292 KB n=100
3 Correct 1 ms 340 KB n=100
4 Correct 0 ms 340 KB n=100
5 Correct 1 ms 340 KB n=100
6 Correct 0 ms 340 KB n=100
7 Correct 1 ms 340 KB n=100
8 Correct 0 ms 288 KB n=100
9 Correct 0 ms 340 KB n=100
10 Correct 0 ms 340 KB n=100
11 Correct 1 ms 340 KB n=100
12 Correct 1 ms 380 KB n=100
13 Correct 1 ms 340 KB n=100
14 Correct 0 ms 340 KB n=100
15 Correct 0 ms 340 KB n=100
16 Correct 1 ms 340 KB n=100
17 Correct 0 ms 340 KB n=100
18 Correct 0 ms 340 KB n=100
19 Correct 1 ms 288 KB n=100
20 Correct 0 ms 340 KB n=100
21 Correct 1 ms 288 KB n=100
22 Correct 1 ms 340 KB n=100
23 Correct 0 ms 340 KB n=100
24 Correct 0 ms 340 KB n=100
25 Correct 1 ms 340 KB n=100
26 Correct 0 ms 340 KB n=12
27 Correct 0 ms 340 KB n=100
28 Correct 1 ms 428 KB n=500
29 Correct 1 ms 468 KB n=500
30 Correct 1 ms 432 KB n=500
31 Correct 1 ms 340 KB n=500
32 Correct 1 ms 340 KB n=500
33 Correct 1 ms 436 KB n=500
34 Correct 1 ms 340 KB n=500
35 Correct 1 ms 468 KB n=500
36 Correct 1 ms 340 KB n=500
37 Correct 1 ms 340 KB n=500
38 Correct 1 ms 340 KB n=500
39 Correct 1 ms 300 KB n=500
40 Correct 1 ms 340 KB n=500
41 Correct 1 ms 340 KB n=500
42 Correct 1 ms 340 KB n=500
43 Correct 1 ms 424 KB n=500
44 Correct 1 ms 340 KB n=500
45 Correct 1 ms 340 KB n=500
46 Correct 1 ms 468 KB n=500
47 Correct 1 ms 468 KB n=500
48 Correct 1 ms 428 KB n=500
49 Correct 1 ms 352 KB n=500
50 Correct 1 ms 340 KB n=500
51 Correct 1 ms 432 KB n=500
52 Correct 1 ms 468 KB n=500
53 Correct 1 ms 340 KB n=500
54 Correct 1 ms 468 KB n=500
55 Correct 1 ms 340 KB n=278
56 Correct 1 ms 468 KB n=500
57 Correct 1 ms 468 KB n=500
58 Correct 1 ms 304 KB n=500
59 Correct 4 ms 596 KB n=2000
60 Correct 2 ms 828 KB n=2000
61 Correct 2 ms 724 KB n=2000
62 Correct 2 ms 688 KB n=2000
63 Correct 3 ms 596 KB n=2000
64 Correct 2 ms 724 KB n=2000
65 Correct 3 ms 556 KB n=2000
66 Correct 2 ms 724 KB n=2000
67 Correct 3 ms 560 KB n=2000
68 Correct 3 ms 692 KB n=2000
69 Correct 2 ms 596 KB n=2000
70 Correct 2 ms 560 KB n=2000
71 Correct 2 ms 552 KB n=2000
72 Correct 2 ms 596 KB n=2000
73 Correct 2 ms 564 KB n=2000
74 Correct 3 ms 560 KB n=1844
75 Correct 2 ms 568 KB n=2000
76 Correct 3 ms 564 KB n=2000
77 Correct 3 ms 596 KB n=2000
78 Correct 4 ms 596 KB n=2000
79 Correct 3 ms 596 KB n=2000
80 Correct 2 ms 724 KB n=2000
81 Correct 2 ms 724 KB n=2000
82 Correct 3 ms 596 KB n=2000
83 Correct 2 ms 724 KB n=2000
84 Correct 3 ms 560 KB n=2000
85 Correct 2 ms 596 KB n=2000
86 Correct 2 ms 684 KB n=2000
87 Correct 4 ms 636 KB n=2000
88 Correct 2 ms 724 KB n=2000
89 Correct 2 ms 724 KB n=2000
90 Correct 2 ms 724 KB n=2000
91 Correct 2 ms 596 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB n=5
2 Correct 1 ms 292 KB n=100
3 Correct 1 ms 340 KB n=100
4 Correct 0 ms 340 KB n=100
5 Correct 1 ms 340 KB n=100
6 Correct 0 ms 340 KB n=100
7 Correct 1 ms 340 KB n=100
8 Correct 0 ms 288 KB n=100
9 Correct 0 ms 340 KB n=100
10 Correct 0 ms 340 KB n=100
11 Correct 1 ms 340 KB n=100
12 Correct 1 ms 380 KB n=100
13 Correct 1 ms 340 KB n=100
14 Correct 0 ms 340 KB n=100
15 Correct 0 ms 340 KB n=100
16 Correct 1 ms 340 KB n=100
17 Correct 0 ms 340 KB n=100
18 Correct 0 ms 340 KB n=100
19 Correct 1 ms 288 KB n=100
20 Correct 0 ms 340 KB n=100
21 Correct 1 ms 288 KB n=100
22 Correct 1 ms 340 KB n=100
23 Correct 0 ms 340 KB n=100
24 Correct 0 ms 340 KB n=100
25 Correct 1 ms 340 KB n=100
26 Correct 0 ms 340 KB n=12
27 Correct 0 ms 340 KB n=100
28 Correct 1 ms 428 KB n=500
29 Correct 1 ms 468 KB n=500
30 Correct 1 ms 432 KB n=500
31 Correct 1 ms 340 KB n=500
32 Correct 1 ms 340 KB n=500
33 Correct 1 ms 436 KB n=500
34 Correct 1 ms 340 KB n=500
35 Correct 1 ms 468 KB n=500
36 Correct 1 ms 340 KB n=500
37 Correct 1 ms 340 KB n=500
38 Correct 1 ms 340 KB n=500
39 Correct 1 ms 300 KB n=500
40 Correct 1 ms 340 KB n=500
41 Correct 1 ms 340 KB n=500
42 Correct 1 ms 340 KB n=500
43 Correct 1 ms 424 KB n=500
44 Correct 1 ms 340 KB n=500
45 Correct 1 ms 340 KB n=500
46 Correct 1 ms 468 KB n=500
47 Correct 1 ms 468 KB n=500
48 Correct 1 ms 428 KB n=500
49 Correct 1 ms 352 KB n=500
50 Correct 1 ms 340 KB n=500
51 Correct 1 ms 432 KB n=500
52 Correct 1 ms 468 KB n=500
53 Correct 1 ms 340 KB n=500
54 Correct 1 ms 468 KB n=500
55 Correct 1 ms 340 KB n=278
56 Correct 1 ms 468 KB n=500
57 Correct 1 ms 468 KB n=500
58 Correct 1 ms 304 KB n=500
59 Correct 4 ms 596 KB n=2000
60 Correct 2 ms 828 KB n=2000
61 Correct 2 ms 724 KB n=2000
62 Correct 2 ms 688 KB n=2000
63 Correct 3 ms 596 KB n=2000
64 Correct 2 ms 724 KB n=2000
65 Correct 3 ms 556 KB n=2000
66 Correct 2 ms 724 KB n=2000
67 Correct 3 ms 560 KB n=2000
68 Correct 3 ms 692 KB n=2000
69 Correct 2 ms 596 KB n=2000
70 Correct 2 ms 560 KB n=2000
71 Correct 2 ms 552 KB n=2000
72 Correct 2 ms 596 KB n=2000
73 Correct 2 ms 564 KB n=2000
74 Correct 3 ms 560 KB n=1844
75 Correct 2 ms 568 KB n=2000
76 Correct 3 ms 564 KB n=2000
77 Correct 3 ms 596 KB n=2000
78 Correct 4 ms 596 KB n=2000
79 Correct 3 ms 596 KB n=2000
80 Correct 2 ms 724 KB n=2000
81 Correct 2 ms 724 KB n=2000
82 Correct 3 ms 596 KB n=2000
83 Correct 2 ms 724 KB n=2000
84 Correct 3 ms 560 KB n=2000
85 Correct 2 ms 596 KB n=2000
86 Correct 2 ms 684 KB n=2000
87 Correct 4 ms 636 KB n=2000
88 Correct 2 ms 724 KB n=2000
89 Correct 2 ms 724 KB n=2000
90 Correct 2 ms 724 KB n=2000
91 Correct 2 ms 596 KB n=2000
92 Correct 425 ms 32364 KB n=200000
93 Correct 239 ms 41804 KB n=200000
94 Correct 205 ms 48460 KB n=200000
95 Correct 459 ms 32076 KB n=200000
96 Correct 412 ms 32140 KB n=200000
97 Correct 267 ms 39992 KB n=200000
98 Correct 442 ms 32156 KB n=200000
99 Correct 558 ms 32396 KB n=200000
100 Correct 423 ms 32292 KB n=200000
101 Correct 204 ms 50932 KB n=200000
102 Correct 182 ms 28856 KB n=200000
103 Correct 188 ms 28900 KB n=200000
104 Correct 184 ms 28860 KB n=200000
105 Correct 191 ms 29256 KB n=200000
106 Correct 197 ms 29220 KB n=200000
107 Correct 191 ms 29260 KB n=200000
108 Correct 509 ms 32336 KB n=200000
109 Correct 471 ms 32456 KB n=200000
110 Correct 469 ms 32332 KB n=200000
111 Correct 495 ms 31620 KB n=200000
112 Correct 234 ms 48952 KB n=200000
113 Correct 254 ms 39984 KB n=200000
114 Correct 520 ms 31740 KB n=200000
115 Correct 420 ms 35768 KB n=200000
116 Correct 414 ms 32260 KB n=200000
117 Correct 216 ms 49900 KB n=200000
118 Correct 284 ms 37836 KB n=200000
119 Correct 402 ms 32336 KB n=200000
120 Correct 192 ms 50164 KB n=200000
121 Correct 210 ms 50096 KB n=200000
122 Correct 221 ms 50364 KB n=200000
123 Correct 178 ms 28980 KB n=200000
124 Correct 62 ms 10680 KB n=25264