답안 #153909

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
153909 2019-09-17T12:35:55 Z luciocf Birthday gift (IZhO18_treearray) C++14
100 / 100
2020 ms 83208 KB
#include <bits/stdc++.h>

using namespace std;

const int maxn = 2e5+10;

int n, m, q;
int a[maxn];

set<int> st1[maxn], st2[maxn];

int nivel[maxn];

int tab[maxn][19];

vector<int> grafo[maxn];

void dfs(int u, int p)
{
	for (auto v: grafo[u])
	{
		if (v == p) continue;

		tab[v][0] = u, nivel[v] = nivel[u]+1;
		dfs(v, u);
	}
}

void build(void)
{
	for (int j = 1; j < 19; j++)
		for (int i = 1; i <= n; i++)
			tab[i][j] = tab[tab[i][j-1]][j-1];
}

int lca(int u, int v)
{
	if (nivel[u] < nivel[v]) swap(u, v);

	for (int i = 18; i >= 0; i--)
		if (nivel[u]-(1<<i) >= nivel[v])
			u = tab[u][i];

	if (u == v) return u;

	for (int i = 18; i >= 0; i--)
		if (tab[u][i] && tab[v][i] && tab[u][i] != tab[v][i])
			u = tab[u][i], v = tab[v][i];

	return tab[u][0];
}

int main(void)
{
	scanf("%d %d %d", &n, &m, &q);

	for (int i = 1; i < n; i++)
	{
		int u, v;
		scanf("%d %d", &u, &v);

		grafo[u].push_back(v);
		grafo[v].push_back(u);
	}

	dfs(1, 0);
	build();

	for (int i = 1; i <= m; i++)
	{
		scanf("%d", &a[i]);

		st1[a[i]].insert(i);
		if (i > 1) st2[lca(a[i-1], a[i])].insert(i-1);
	}

	for (int i = 1; i <= q; i++)
	{
		int op;
		scanf("%d", &op);

		if (op == 1)
		{
			int pos, v;
			scanf("%d %d", &pos, &v);

			st1[a[pos]].erase(pos);
			st1[v].insert(pos);

			if (pos > 1)
			{
				st2[lca(a[pos], a[pos-1])].erase(pos-1);
				st2[lca(v, a[pos-1])].insert(pos-1);
			}

			if (pos < m)
			{
				st2[lca(a[pos], a[pos+1])].erase(pos);
				st2[lca(v, a[pos+1])].insert(pos);
			}

			a[pos] = v;
		}
		else
		{
			int l, r, v;
			scanf("%d %d %d", &l, &r, &v);

			auto it = st1[v].lower_bound(l);

			if (it != st1[v].end() && *it <= r) printf("%d %d\n", *it, *it);
			else
			{
				it = st2[v].lower_bound(l);

				if (it != st2[v].end() && *it < r)
					printf("%d %d\n", *it, *it+1);
				else
					printf("-1 -1\n");
			}
		}
	}
}

Compilation message

treearray.cpp: In function 'int main()':
treearray.cpp:55:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d %d", &n, &m, &q);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:60:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &u, &v);
   ~~~~~^~~~~~~~~~~~~~~~~
treearray.cpp:71:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &a[i]);
   ~~~~~^~~~~~~~~~~~~
treearray.cpp:80:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &op);
   ~~~~~^~~~~~~~~~~
treearray.cpp:85:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d %d", &pos, &v);
    ~~~~~^~~~~~~~~~~~~~~~~~~
treearray.cpp:107:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d %d %d", &l, &r, &v);
    ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 24060 KB n=5
2 Correct 24 ms 23800 KB n=100
3 Correct 24 ms 23800 KB n=100
4 Correct 24 ms 23876 KB n=100
5 Correct 25 ms 23816 KB n=100
6 Correct 25 ms 23800 KB n=100
7 Correct 25 ms 23800 KB n=100
8 Correct 24 ms 23800 KB n=100
9 Correct 24 ms 23800 KB n=100
10 Correct 27 ms 23892 KB n=100
11 Correct 25 ms 23800 KB n=100
12 Correct 27 ms 23800 KB n=100
13 Correct 24 ms 23800 KB n=100
14 Correct 25 ms 23928 KB n=100
15 Correct 25 ms 23800 KB n=100
16 Correct 25 ms 23800 KB n=100
17 Correct 25 ms 23924 KB n=100
18 Correct 25 ms 23852 KB n=100
19 Correct 24 ms 23852 KB n=100
20 Correct 25 ms 23928 KB n=100
21 Correct 24 ms 23800 KB n=100
22 Correct 25 ms 23800 KB n=100
23 Correct 24 ms 23800 KB n=100
24 Correct 25 ms 23800 KB n=100
25 Correct 25 ms 23928 KB n=100
26 Correct 29 ms 23800 KB n=12
27 Correct 25 ms 23800 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 24060 KB n=5
2 Correct 24 ms 23800 KB n=100
3 Correct 24 ms 23800 KB n=100
4 Correct 24 ms 23876 KB n=100
5 Correct 25 ms 23816 KB n=100
6 Correct 25 ms 23800 KB n=100
7 Correct 25 ms 23800 KB n=100
8 Correct 24 ms 23800 KB n=100
9 Correct 24 ms 23800 KB n=100
10 Correct 27 ms 23892 KB n=100
11 Correct 25 ms 23800 KB n=100
12 Correct 27 ms 23800 KB n=100
13 Correct 24 ms 23800 KB n=100
14 Correct 25 ms 23928 KB n=100
15 Correct 25 ms 23800 KB n=100
16 Correct 25 ms 23800 KB n=100
17 Correct 25 ms 23924 KB n=100
18 Correct 25 ms 23852 KB n=100
19 Correct 24 ms 23852 KB n=100
20 Correct 25 ms 23928 KB n=100
21 Correct 24 ms 23800 KB n=100
22 Correct 25 ms 23800 KB n=100
23 Correct 24 ms 23800 KB n=100
24 Correct 25 ms 23800 KB n=100
25 Correct 25 ms 23928 KB n=100
26 Correct 29 ms 23800 KB n=12
27 Correct 25 ms 23800 KB n=100
28 Correct 26 ms 23928 KB n=500
29 Correct 26 ms 23924 KB n=500
30 Correct 26 ms 23928 KB n=500
31 Correct 26 ms 23928 KB n=500
32 Correct 30 ms 23984 KB n=500
33 Correct 26 ms 23928 KB n=500
34 Correct 26 ms 23928 KB n=500
35 Correct 26 ms 23928 KB n=500
36 Correct 25 ms 23928 KB n=500
37 Correct 25 ms 23928 KB n=500
38 Correct 25 ms 23924 KB n=500
39 Correct 30 ms 23932 KB n=500
40 Correct 25 ms 23900 KB n=500
41 Correct 26 ms 23928 KB n=500
42 Correct 26 ms 23928 KB n=500
43 Correct 27 ms 24028 KB n=500
44 Correct 25 ms 23928 KB n=500
45 Correct 25 ms 24056 KB n=500
46 Correct 25 ms 23928 KB n=500
47 Correct 25 ms 23928 KB n=500
48 Correct 25 ms 23860 KB n=500
49 Correct 26 ms 24048 KB n=500
50 Correct 26 ms 23800 KB n=500
51 Correct 25 ms 23928 KB n=500
52 Correct 25 ms 24056 KB n=500
53 Correct 26 ms 23928 KB n=500
54 Correct 26 ms 23928 KB n=500
55 Correct 25 ms 23928 KB n=278
56 Correct 25 ms 23928 KB n=500
57 Correct 26 ms 23928 KB n=500
58 Correct 25 ms 23928 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 24060 KB n=5
2 Correct 24 ms 23800 KB n=100
3 Correct 24 ms 23800 KB n=100
4 Correct 24 ms 23876 KB n=100
5 Correct 25 ms 23816 KB n=100
6 Correct 25 ms 23800 KB n=100
7 Correct 25 ms 23800 KB n=100
8 Correct 24 ms 23800 KB n=100
9 Correct 24 ms 23800 KB n=100
10 Correct 27 ms 23892 KB n=100
11 Correct 25 ms 23800 KB n=100
12 Correct 27 ms 23800 KB n=100
13 Correct 24 ms 23800 KB n=100
14 Correct 25 ms 23928 KB n=100
15 Correct 25 ms 23800 KB n=100
16 Correct 25 ms 23800 KB n=100
17 Correct 25 ms 23924 KB n=100
18 Correct 25 ms 23852 KB n=100
19 Correct 24 ms 23852 KB n=100
20 Correct 25 ms 23928 KB n=100
21 Correct 24 ms 23800 KB n=100
22 Correct 25 ms 23800 KB n=100
23 Correct 24 ms 23800 KB n=100
24 Correct 25 ms 23800 KB n=100
25 Correct 25 ms 23928 KB n=100
26 Correct 29 ms 23800 KB n=12
27 Correct 25 ms 23800 KB n=100
28 Correct 26 ms 23928 KB n=500
29 Correct 26 ms 23924 KB n=500
30 Correct 26 ms 23928 KB n=500
31 Correct 26 ms 23928 KB n=500
32 Correct 30 ms 23984 KB n=500
33 Correct 26 ms 23928 KB n=500
34 Correct 26 ms 23928 KB n=500
35 Correct 26 ms 23928 KB n=500
36 Correct 25 ms 23928 KB n=500
37 Correct 25 ms 23928 KB n=500
38 Correct 25 ms 23924 KB n=500
39 Correct 30 ms 23932 KB n=500
40 Correct 25 ms 23900 KB n=500
41 Correct 26 ms 23928 KB n=500
42 Correct 26 ms 23928 KB n=500
43 Correct 27 ms 24028 KB n=500
44 Correct 25 ms 23928 KB n=500
45 Correct 25 ms 24056 KB n=500
46 Correct 25 ms 23928 KB n=500
47 Correct 25 ms 23928 KB n=500
48 Correct 25 ms 23860 KB n=500
49 Correct 26 ms 24048 KB n=500
50 Correct 26 ms 23800 KB n=500
51 Correct 25 ms 23928 KB n=500
52 Correct 25 ms 24056 KB n=500
53 Correct 26 ms 23928 KB n=500
54 Correct 26 ms 23928 KB n=500
55 Correct 25 ms 23928 KB n=278
56 Correct 25 ms 23928 KB n=500
57 Correct 26 ms 23928 KB n=500
58 Correct 25 ms 23928 KB n=500
59 Correct 34 ms 24312 KB n=2000
60 Correct 29 ms 24312 KB n=2000
61 Correct 35 ms 24312 KB n=2000
62 Correct 29 ms 24312 KB n=2000
63 Correct 29 ms 24440 KB n=2000
64 Correct 37 ms 24440 KB n=2000
65 Correct 30 ms 24376 KB n=2000
66 Correct 30 ms 24312 KB n=2000
67 Correct 29 ms 24312 KB n=2000
68 Correct 29 ms 24312 KB n=2000
69 Correct 29 ms 24312 KB n=2000
70 Correct 28 ms 24312 KB n=2000
71 Correct 30 ms 24308 KB n=2000
72 Correct 28 ms 24284 KB n=2000
73 Correct 28 ms 24312 KB n=2000
74 Correct 28 ms 24312 KB n=1844
75 Correct 28 ms 24312 KB n=2000
76 Correct 30 ms 24436 KB n=2000
77 Correct 30 ms 24312 KB n=2000
78 Correct 37 ms 24380 KB n=2000
79 Correct 30 ms 24312 KB n=2000
80 Correct 45 ms 24440 KB n=2000
81 Correct 30 ms 24316 KB n=2000
82 Correct 29 ms 24260 KB n=2000
83 Correct 29 ms 24312 KB n=2000
84 Correct 29 ms 24440 KB n=2000
85 Correct 30 ms 24284 KB n=2000
86 Correct 29 ms 24312 KB n=2000
87 Correct 29 ms 24312 KB n=2000
88 Correct 29 ms 24440 KB n=2000
89 Correct 30 ms 24440 KB n=2000
90 Correct 30 ms 24440 KB n=2000
91 Correct 29 ms 24312 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 24060 KB n=5
2 Correct 24 ms 23800 KB n=100
3 Correct 24 ms 23800 KB n=100
4 Correct 24 ms 23876 KB n=100
5 Correct 25 ms 23816 KB n=100
6 Correct 25 ms 23800 KB n=100
7 Correct 25 ms 23800 KB n=100
8 Correct 24 ms 23800 KB n=100
9 Correct 24 ms 23800 KB n=100
10 Correct 27 ms 23892 KB n=100
11 Correct 25 ms 23800 KB n=100
12 Correct 27 ms 23800 KB n=100
13 Correct 24 ms 23800 KB n=100
14 Correct 25 ms 23928 KB n=100
15 Correct 25 ms 23800 KB n=100
16 Correct 25 ms 23800 KB n=100
17 Correct 25 ms 23924 KB n=100
18 Correct 25 ms 23852 KB n=100
19 Correct 24 ms 23852 KB n=100
20 Correct 25 ms 23928 KB n=100
21 Correct 24 ms 23800 KB n=100
22 Correct 25 ms 23800 KB n=100
23 Correct 24 ms 23800 KB n=100
24 Correct 25 ms 23800 KB n=100
25 Correct 25 ms 23928 KB n=100
26 Correct 29 ms 23800 KB n=12
27 Correct 25 ms 23800 KB n=100
28 Correct 26 ms 23928 KB n=500
29 Correct 26 ms 23924 KB n=500
30 Correct 26 ms 23928 KB n=500
31 Correct 26 ms 23928 KB n=500
32 Correct 30 ms 23984 KB n=500
33 Correct 26 ms 23928 KB n=500
34 Correct 26 ms 23928 KB n=500
35 Correct 26 ms 23928 KB n=500
36 Correct 25 ms 23928 KB n=500
37 Correct 25 ms 23928 KB n=500
38 Correct 25 ms 23924 KB n=500
39 Correct 30 ms 23932 KB n=500
40 Correct 25 ms 23900 KB n=500
41 Correct 26 ms 23928 KB n=500
42 Correct 26 ms 23928 KB n=500
43 Correct 27 ms 24028 KB n=500
44 Correct 25 ms 23928 KB n=500
45 Correct 25 ms 24056 KB n=500
46 Correct 25 ms 23928 KB n=500
47 Correct 25 ms 23928 KB n=500
48 Correct 25 ms 23860 KB n=500
49 Correct 26 ms 24048 KB n=500
50 Correct 26 ms 23800 KB n=500
51 Correct 25 ms 23928 KB n=500
52 Correct 25 ms 24056 KB n=500
53 Correct 26 ms 23928 KB n=500
54 Correct 26 ms 23928 KB n=500
55 Correct 25 ms 23928 KB n=278
56 Correct 25 ms 23928 KB n=500
57 Correct 26 ms 23928 KB n=500
58 Correct 25 ms 23928 KB n=500
59 Correct 34 ms 24312 KB n=2000
60 Correct 29 ms 24312 KB n=2000
61 Correct 35 ms 24312 KB n=2000
62 Correct 29 ms 24312 KB n=2000
63 Correct 29 ms 24440 KB n=2000
64 Correct 37 ms 24440 KB n=2000
65 Correct 30 ms 24376 KB n=2000
66 Correct 30 ms 24312 KB n=2000
67 Correct 29 ms 24312 KB n=2000
68 Correct 29 ms 24312 KB n=2000
69 Correct 29 ms 24312 KB n=2000
70 Correct 28 ms 24312 KB n=2000
71 Correct 30 ms 24308 KB n=2000
72 Correct 28 ms 24284 KB n=2000
73 Correct 28 ms 24312 KB n=2000
74 Correct 28 ms 24312 KB n=1844
75 Correct 28 ms 24312 KB n=2000
76 Correct 30 ms 24436 KB n=2000
77 Correct 30 ms 24312 KB n=2000
78 Correct 37 ms 24380 KB n=2000
79 Correct 30 ms 24312 KB n=2000
80 Correct 45 ms 24440 KB n=2000
81 Correct 30 ms 24316 KB n=2000
82 Correct 29 ms 24260 KB n=2000
83 Correct 29 ms 24312 KB n=2000
84 Correct 29 ms 24440 KB n=2000
85 Correct 30 ms 24284 KB n=2000
86 Correct 29 ms 24312 KB n=2000
87 Correct 29 ms 24312 KB n=2000
88 Correct 29 ms 24440 KB n=2000
89 Correct 30 ms 24440 KB n=2000
90 Correct 30 ms 24440 KB n=2000
91 Correct 29 ms 24312 KB n=2000
92 Correct 1027 ms 73836 KB n=200000
93 Correct 1522 ms 78756 KB n=200000
94 Correct 1348 ms 82008 KB n=200000
95 Correct 943 ms 73564 KB n=200000
96 Correct 955 ms 73728 KB n=200000
97 Correct 1837 ms 77720 KB n=200000
98 Correct 971 ms 73664 KB n=200000
99 Correct 1236 ms 73792 KB n=200000
100 Correct 933 ms 73788 KB n=200000
101 Correct 1373 ms 83208 KB n=200000
102 Correct 662 ms 74824 KB n=200000
103 Correct 676 ms 74944 KB n=200000
104 Correct 616 ms 74764 KB n=200000
105 Correct 638 ms 75248 KB n=200000
106 Correct 649 ms 75324 KB n=200000
107 Correct 638 ms 75220 KB n=200000
108 Correct 1135 ms 73788 KB n=200000
109 Correct 1075 ms 73792 KB n=200000
110 Correct 1155 ms 73764 KB n=200000
111 Correct 1018 ms 73100 KB n=200000
112 Correct 1382 ms 82484 KB n=200000
113 Correct 1500 ms 77908 KB n=200000
114 Correct 1008 ms 73204 KB n=200000
115 Correct 2020 ms 75808 KB n=200000
116 Correct 945 ms 73760 KB n=200000
117 Correct 1389 ms 82728 KB n=200000
118 Correct 1472 ms 76580 KB n=200000
119 Correct 941 ms 73764 KB n=200000
120 Correct 1603 ms 82440 KB n=200000
121 Correct 1323 ms 82324 KB n=200000
122 Correct 1307 ms 82528 KB n=200000
123 Correct 628 ms 75152 KB n=200000
124 Correct 377 ms 38876 KB n=25264