답안 #166771

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
166771 2019-12-04T04:18:43 Z abil Birthday gift (IZhO18_treearray) C++14
100 / 100
2518 ms 84984 KB
#include <bits/stdc++.h>

#define fr first
#define sc second
#define pb push_back
#define mk make_pair
#define all(s) s.begin(),s.end()
//#define int long long

using namespace std;

const int N = (2e5 + 12);
const int mod = (1e9 + 7);
const int INF = (0x3f3f3f3f);

vector<int > g[N];
int up[20][N], tin[N], tout[N], timer, a[N];

void dfs(int v,int pr){
	tin[v] = ++timer;
	up[0][v] = pr;
	for(int i = 1;i <= 19; i++){
		up[i][v] = up[i - 1][up[i - 1][v]];
	}
	for(auto to : g[v]){
		if(to != pr){
			dfs(to, v);
		}
	}
	tout[v] = ++timer;
}

bool upper(int a,int b){
	return tin[a] <= tin[b] && tout[a] >= tout[b];
}

int lca(int a,int b){
	if(upper(a, b)){
		return a;
	}
	if(upper(b, a)){
		return b;
	}
	for(int i = 19;i >= 0; i--){
		if(!upper(up[i][a], b)){
			a = up[i][a];
		}
	}
	a = up[0][a];
	return a;
}

set<int > a_pos[N], lca_pos[N];
main()
{
	int n, m, q, v, u;
	cin >> n >> m >> q;
	for(int i = 1;i <= n - 1; i++){
		scanf("%d%d", &v, &u);
		g[u].pb(v);
		g[v].pb(u);
	}
	dfs(1, 1);
	for(int i = 1;i <= m; i++){
		scanf("%d", &a[i]);
		a_pos[a[i]].insert(i);
		if(i > 1){
			lca_pos[lca(a[i - 1], a[i])].insert(i - 1);
		}
	}
	int type, l, r, x;
	while(q--){
		scanf("%d", &type);
		if(type == 1){
			scanf("%d%d", &x, &v);
			a_pos[a[x]].erase(x);
			a_pos[v].insert(x);
			if(x > 1){
				lca_pos[lca(a[x - 1], a[x])].erase(x - 1);
				lca_pos[lca(a[x - 1], v)].insert(x - 1);
			}
			if(x < m){
				lca_pos[lca(a[x], a[x + 1])].erase(x);
				lca_pos[lca(a[x + 1], v)].insert(x);
			}
			a[x] = v;
		}
		else{
			scanf("%d%d%d",&l, &r, &v);
			auto it = a_pos[v].lower_bound(l);
			if(it != a_pos[v].end() && *it <= r){
				printf("%d %d\n", *it, *it);
				continue;
			}
			it = lca_pos[v].lower_bound(l);
			if(r > l && it != lca_pos[v].end() && *it < r){
				printf("%d %d\n", *it, *it + 1);
				continue;
			}
			printf("%d %d\n", -1, -1);
		}
	}
}

Compilation message

treearray.cpp:54:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main()
      ^
treearray.cpp: In function 'int main()':
treearray.cpp:59:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &v, &u);
   ~~~~~^~~~~~~~~~~~~~~~
treearray.cpp:65:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &a[i]);
   ~~~~~^~~~~~~~~~~~~
treearray.cpp:73:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &type);
   ~~~~~^~~~~~~~~~~~~
treearray.cpp:75:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d%d", &x, &v);
    ~~~~~^~~~~~~~~~~~~~~~
treearray.cpp:89: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 24 ms 23928 KB n=5
2 Correct 43 ms 23928 KB n=100
3 Correct 25 ms 23928 KB n=100
4 Correct 27 ms 23928 KB n=100
5 Correct 24 ms 23928 KB n=100
6 Correct 24 ms 23928 KB n=100
7 Correct 26 ms 23992 KB n=100
8 Correct 21 ms 24056 KB n=100
9 Correct 23 ms 23928 KB n=100
10 Correct 24 ms 23928 KB n=100
11 Correct 26 ms 23956 KB n=100
12 Correct 28 ms 23928 KB n=100
13 Correct 24 ms 23900 KB n=100
14 Correct 24 ms 23928 KB n=100
15 Correct 24 ms 23928 KB n=100
16 Correct 27 ms 23952 KB n=100
17 Correct 56 ms 23932 KB n=100
18 Correct 26 ms 23928 KB n=100
19 Correct 26 ms 23928 KB n=100
20 Correct 26 ms 23928 KB n=100
21 Correct 27 ms 23928 KB n=100
22 Correct 24 ms 23928 KB n=100
23 Correct 28 ms 23928 KB n=100
24 Correct 26 ms 23928 KB n=100
25 Correct 25 ms 24056 KB n=100
26 Correct 25 ms 23928 KB n=12
27 Correct 25 ms 23928 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 23928 KB n=5
2 Correct 43 ms 23928 KB n=100
3 Correct 25 ms 23928 KB n=100
4 Correct 27 ms 23928 KB n=100
5 Correct 24 ms 23928 KB n=100
6 Correct 24 ms 23928 KB n=100
7 Correct 26 ms 23992 KB n=100
8 Correct 21 ms 24056 KB n=100
9 Correct 23 ms 23928 KB n=100
10 Correct 24 ms 23928 KB n=100
11 Correct 26 ms 23956 KB n=100
12 Correct 28 ms 23928 KB n=100
13 Correct 24 ms 23900 KB n=100
14 Correct 24 ms 23928 KB n=100
15 Correct 24 ms 23928 KB n=100
16 Correct 27 ms 23952 KB n=100
17 Correct 56 ms 23932 KB n=100
18 Correct 26 ms 23928 KB n=100
19 Correct 26 ms 23928 KB n=100
20 Correct 26 ms 23928 KB n=100
21 Correct 27 ms 23928 KB n=100
22 Correct 24 ms 23928 KB n=100
23 Correct 28 ms 23928 KB n=100
24 Correct 26 ms 23928 KB n=100
25 Correct 25 ms 24056 KB n=100
26 Correct 25 ms 23928 KB n=12
27 Correct 25 ms 23928 KB n=100
28 Correct 28 ms 24196 KB n=500
29 Correct 28 ms 24056 KB n=500
30 Correct 27 ms 24132 KB n=500
31 Correct 26 ms 24076 KB n=500
32 Correct 27 ms 24056 KB n=500
33 Correct 28 ms 24056 KB n=500
34 Correct 28 ms 24188 KB n=500
35 Correct 29 ms 24056 KB n=500
36 Correct 27 ms 24056 KB n=500
37 Correct 25 ms 24060 KB n=500
38 Correct 26 ms 24184 KB n=500
39 Correct 24 ms 24056 KB n=500
40 Correct 24 ms 24056 KB n=500
41 Correct 26 ms 24056 KB n=500
42 Correct 25 ms 24184 KB n=500
43 Correct 28 ms 24056 KB n=500
44 Correct 25 ms 24056 KB n=500
45 Correct 27 ms 24056 KB n=500
46 Correct 27 ms 24056 KB n=500
47 Correct 27 ms 24084 KB n=500
48 Correct 27 ms 24056 KB n=500
49 Correct 27 ms 24056 KB n=500
50 Correct 26 ms 24028 KB n=500
51 Correct 28 ms 24104 KB n=500
52 Correct 27 ms 24096 KB n=500
53 Correct 28 ms 24060 KB n=500
54 Correct 24 ms 24056 KB n=500
55 Correct 25 ms 23976 KB n=278
56 Correct 24 ms 24056 KB n=500
57 Correct 24 ms 24104 KB n=500
58 Correct 25 ms 24056 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 23928 KB n=5
2 Correct 43 ms 23928 KB n=100
3 Correct 25 ms 23928 KB n=100
4 Correct 27 ms 23928 KB n=100
5 Correct 24 ms 23928 KB n=100
6 Correct 24 ms 23928 KB n=100
7 Correct 26 ms 23992 KB n=100
8 Correct 21 ms 24056 KB n=100
9 Correct 23 ms 23928 KB n=100
10 Correct 24 ms 23928 KB n=100
11 Correct 26 ms 23956 KB n=100
12 Correct 28 ms 23928 KB n=100
13 Correct 24 ms 23900 KB n=100
14 Correct 24 ms 23928 KB n=100
15 Correct 24 ms 23928 KB n=100
16 Correct 27 ms 23952 KB n=100
17 Correct 56 ms 23932 KB n=100
18 Correct 26 ms 23928 KB n=100
19 Correct 26 ms 23928 KB n=100
20 Correct 26 ms 23928 KB n=100
21 Correct 27 ms 23928 KB n=100
22 Correct 24 ms 23928 KB n=100
23 Correct 28 ms 23928 KB n=100
24 Correct 26 ms 23928 KB n=100
25 Correct 25 ms 24056 KB n=100
26 Correct 25 ms 23928 KB n=12
27 Correct 25 ms 23928 KB n=100
28 Correct 28 ms 24196 KB n=500
29 Correct 28 ms 24056 KB n=500
30 Correct 27 ms 24132 KB n=500
31 Correct 26 ms 24076 KB n=500
32 Correct 27 ms 24056 KB n=500
33 Correct 28 ms 24056 KB n=500
34 Correct 28 ms 24188 KB n=500
35 Correct 29 ms 24056 KB n=500
36 Correct 27 ms 24056 KB n=500
37 Correct 25 ms 24060 KB n=500
38 Correct 26 ms 24184 KB n=500
39 Correct 24 ms 24056 KB n=500
40 Correct 24 ms 24056 KB n=500
41 Correct 26 ms 24056 KB n=500
42 Correct 25 ms 24184 KB n=500
43 Correct 28 ms 24056 KB n=500
44 Correct 25 ms 24056 KB n=500
45 Correct 27 ms 24056 KB n=500
46 Correct 27 ms 24056 KB n=500
47 Correct 27 ms 24084 KB n=500
48 Correct 27 ms 24056 KB n=500
49 Correct 27 ms 24056 KB n=500
50 Correct 26 ms 24028 KB n=500
51 Correct 28 ms 24104 KB n=500
52 Correct 27 ms 24096 KB n=500
53 Correct 28 ms 24060 KB n=500
54 Correct 24 ms 24056 KB n=500
55 Correct 25 ms 23976 KB n=278
56 Correct 24 ms 24056 KB n=500
57 Correct 24 ms 24104 KB n=500
58 Correct 25 ms 24056 KB n=500
59 Correct 29 ms 24364 KB n=2000
60 Correct 28 ms 24568 KB n=2000
61 Correct 28 ms 24440 KB n=2000
62 Correct 29 ms 24440 KB n=2000
63 Correct 30 ms 24416 KB n=2000
64 Correct 42 ms 24568 KB n=2000
65 Correct 30 ms 24312 KB n=2000
66 Correct 28 ms 24440 KB n=2000
67 Correct 33 ms 24296 KB n=2000
68 Correct 28 ms 24440 KB n=2000
69 Correct 28 ms 24412 KB n=2000
70 Correct 27 ms 24312 KB n=2000
71 Correct 27 ms 24340 KB n=2000
72 Correct 27 ms 24312 KB n=2000
73 Correct 28 ms 24312 KB n=2000
74 Correct 29 ms 24312 KB n=1844
75 Correct 27 ms 24312 KB n=2000
76 Correct 29 ms 24312 KB n=2000
77 Correct 29 ms 24312 KB n=2000
78 Correct 29 ms 24312 KB n=2000
79 Correct 32 ms 24376 KB n=2000
80 Correct 28 ms 24440 KB n=2000
81 Correct 31 ms 24456 KB n=2000
82 Correct 30 ms 24312 KB n=2000
83 Correct 28 ms 24440 KB n=2000
84 Correct 30 ms 24312 KB n=2000
85 Correct 31 ms 24340 KB n=2000
86 Correct 32 ms 24440 KB n=2000
87 Correct 30 ms 24312 KB n=2000
88 Correct 29 ms 24444 KB n=2000
89 Correct 27 ms 24440 KB n=2000
90 Correct 27 ms 24440 KB n=2000
91 Correct 31 ms 24460 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 23928 KB n=5
2 Correct 43 ms 23928 KB n=100
3 Correct 25 ms 23928 KB n=100
4 Correct 27 ms 23928 KB n=100
5 Correct 24 ms 23928 KB n=100
6 Correct 24 ms 23928 KB n=100
7 Correct 26 ms 23992 KB n=100
8 Correct 21 ms 24056 KB n=100
9 Correct 23 ms 23928 KB n=100
10 Correct 24 ms 23928 KB n=100
11 Correct 26 ms 23956 KB n=100
12 Correct 28 ms 23928 KB n=100
13 Correct 24 ms 23900 KB n=100
14 Correct 24 ms 23928 KB n=100
15 Correct 24 ms 23928 KB n=100
16 Correct 27 ms 23952 KB n=100
17 Correct 56 ms 23932 KB n=100
18 Correct 26 ms 23928 KB n=100
19 Correct 26 ms 23928 KB n=100
20 Correct 26 ms 23928 KB n=100
21 Correct 27 ms 23928 KB n=100
22 Correct 24 ms 23928 KB n=100
23 Correct 28 ms 23928 KB n=100
24 Correct 26 ms 23928 KB n=100
25 Correct 25 ms 24056 KB n=100
26 Correct 25 ms 23928 KB n=12
27 Correct 25 ms 23928 KB n=100
28 Correct 28 ms 24196 KB n=500
29 Correct 28 ms 24056 KB n=500
30 Correct 27 ms 24132 KB n=500
31 Correct 26 ms 24076 KB n=500
32 Correct 27 ms 24056 KB n=500
33 Correct 28 ms 24056 KB n=500
34 Correct 28 ms 24188 KB n=500
35 Correct 29 ms 24056 KB n=500
36 Correct 27 ms 24056 KB n=500
37 Correct 25 ms 24060 KB n=500
38 Correct 26 ms 24184 KB n=500
39 Correct 24 ms 24056 KB n=500
40 Correct 24 ms 24056 KB n=500
41 Correct 26 ms 24056 KB n=500
42 Correct 25 ms 24184 KB n=500
43 Correct 28 ms 24056 KB n=500
44 Correct 25 ms 24056 KB n=500
45 Correct 27 ms 24056 KB n=500
46 Correct 27 ms 24056 KB n=500
47 Correct 27 ms 24084 KB n=500
48 Correct 27 ms 24056 KB n=500
49 Correct 27 ms 24056 KB n=500
50 Correct 26 ms 24028 KB n=500
51 Correct 28 ms 24104 KB n=500
52 Correct 27 ms 24096 KB n=500
53 Correct 28 ms 24060 KB n=500
54 Correct 24 ms 24056 KB n=500
55 Correct 25 ms 23976 KB n=278
56 Correct 24 ms 24056 KB n=500
57 Correct 24 ms 24104 KB n=500
58 Correct 25 ms 24056 KB n=500
59 Correct 29 ms 24364 KB n=2000
60 Correct 28 ms 24568 KB n=2000
61 Correct 28 ms 24440 KB n=2000
62 Correct 29 ms 24440 KB n=2000
63 Correct 30 ms 24416 KB n=2000
64 Correct 42 ms 24568 KB n=2000
65 Correct 30 ms 24312 KB n=2000
66 Correct 28 ms 24440 KB n=2000
67 Correct 33 ms 24296 KB n=2000
68 Correct 28 ms 24440 KB n=2000
69 Correct 28 ms 24412 KB n=2000
70 Correct 27 ms 24312 KB n=2000
71 Correct 27 ms 24340 KB n=2000
72 Correct 27 ms 24312 KB n=2000
73 Correct 28 ms 24312 KB n=2000
74 Correct 29 ms 24312 KB n=1844
75 Correct 27 ms 24312 KB n=2000
76 Correct 29 ms 24312 KB n=2000
77 Correct 29 ms 24312 KB n=2000
78 Correct 29 ms 24312 KB n=2000
79 Correct 32 ms 24376 KB n=2000
80 Correct 28 ms 24440 KB n=2000
81 Correct 31 ms 24456 KB n=2000
82 Correct 30 ms 24312 KB n=2000
83 Correct 28 ms 24440 KB n=2000
84 Correct 30 ms 24312 KB n=2000
85 Correct 31 ms 24340 KB n=2000
86 Correct 32 ms 24440 KB n=2000
87 Correct 30 ms 24312 KB n=2000
88 Correct 29 ms 24444 KB n=2000
89 Correct 27 ms 24440 KB n=2000
90 Correct 27 ms 24440 KB n=2000
91 Correct 31 ms 24460 KB n=2000
92 Correct 2288 ms 69120 KB n=200000
93 Correct 1575 ms 80360 KB n=200000
94 Correct 939 ms 83672 KB n=200000
95 Correct 2429 ms 75372 KB n=200000
96 Correct 2256 ms 75332 KB n=200000
97 Correct 1927 ms 79608 KB n=200000
98 Correct 2518 ms 75280 KB n=200000
99 Correct 2285 ms 75528 KB n=200000
100 Correct 2327 ms 75420 KB n=200000
101 Correct 756 ms 84984 KB n=200000
102 Correct 963 ms 76556 KB n=200000
103 Correct 1033 ms 76540 KB n=200000
104 Correct 942 ms 76432 KB n=200000
105 Correct 1043 ms 76992 KB n=200000
106 Correct 1031 ms 76920 KB n=200000
107 Correct 1075 ms 76920 KB n=200000
108 Correct 2264 ms 75560 KB n=200000
109 Correct 1919 ms 75484 KB n=200000
110 Correct 1928 ms 75504 KB n=200000
111 Correct 2137 ms 74812 KB n=200000
112 Correct 932 ms 83960 KB n=200000
113 Correct 1836 ms 79512 KB n=200000
114 Correct 2316 ms 74860 KB n=200000
115 Correct 2108 ms 77368 KB n=200000
116 Correct 2512 ms 75580 KB n=200000
117 Correct 900 ms 84344 KB n=200000
118 Correct 2162 ms 78332 KB n=200000
119 Correct 2149 ms 75444 KB n=200000
120 Correct 721 ms 83892 KB n=200000
121 Correct 777 ms 83992 KB n=200000
122 Correct 744 ms 84348 KB n=200000
123 Correct 1230 ms 76664 KB n=200000
124 Correct 296 ms 38876 KB n=25264