Submission #174697

# Submission time Handle Problem Language Result Execution time Memory
174697 2020-01-06T18:25:39 Z Lightning Birthday gift (IZhO18_treearray) C++14
100 / 100
1237 ms 84988 KB
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <iomanip>
#include <stack>
#include <queue>
#include <deque>

using namespace std;

typedef long long ll;
typedef pair <int, int> pii;

#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define pb push_back
#define ppb pop_back
#define mkp make_pair
#define F first
#define S second
#define show(a) cerr << #a <<" -> "<< a <<"\n"
#define fo(a, b, c, d) for(int (a) = (b); (a) <= (c); (a) += (d))
#define foo(a, b, c ,d) for(int (a) = (b); (a) >= (c); (a) -= (d))
//#define int ll

const int N = 2e5 + 5;
const int LG = 18;

int n, m, q, a[N];
vector <int> g[N];
int up[N][20], tin[N], tout[N], timer;
set <int> st[N], hv[N];

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

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

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

int main () {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> m >> q;
	for(int i = 1; i < n; ++i) {
		int p, q;
		cin >> p >> q;
		g[p].pb(q);
		g[q].pb(p);
	}
	dfs(1, 1);
	for(int i = 1; i <= m; ++i) {
		cin >> a[i];
	}
	for(int i = 1; i < m; ++i) {
		st[lca(a[i], a[i + 1])].insert(i);
	}
	for(int i = 1; i <= m; ++i) {
		hv[a[i]].insert(i);
	}
	for(int it = 1; it <= q; ++it) {
		int type;
		cin >> type;
		if(type == 1) {
			int pos, v;
			cin >> pos >> v;
			if(1 <= pos - 1) {
				st[lca(a[pos - 1], a[pos])].erase(pos - 1);
				st[lca(a[pos - 1], v)].insert(pos - 1);
			} if(pos + 1 <= m) {
				st[lca(a[pos], a[pos + 1])].erase(pos);
				st[lca(v, a[pos + 1])].insert(pos);
			}
			hv[a[pos]].erase(pos);
			hv[v].insert(pos);
			a[pos] = v;
		} else {
			int l, r, v;
			cin >> l >> r >> v;
			auto it = st[v].lower_bound(l);
			if(it != st[v].end() && *it < r) {
				cout << *it <<' '<< *it + 1 <<'\n';
				continue;
			}
			auto it2 = hv[v].lower_bound(l); 
			if(it2 != hv[v].end() && *it2 <= r) {
				cout << *it2 <<' '<< *it2 <<'\n'; 
				continue;
			} 
			cout << -1 <<' '<< -1 <<'\n';	
		} 
	}
	return 0;
}
/*
5 4 4
1 2
3 1
3 4
5 3
4 5 2 3
2 1 3 1
1 3 5
2 3 4 5 
2 1 3 1
*/
# Verdict Execution time Memory Grader output
1 Correct 24 ms 23800 KB n=5
2 Correct 24 ms 23928 KB n=100
3 Correct 24 ms 23800 KB n=100
4 Correct 24 ms 23880 KB n=100
5 Correct 24 ms 23928 KB n=100
6 Correct 23 ms 23800 KB n=100
7 Correct 23 ms 23928 KB n=100
8 Correct 24 ms 23844 KB n=100
9 Correct 24 ms 23928 KB n=100
10 Correct 24 ms 23928 KB n=100
11 Correct 24 ms 23928 KB n=100
12 Correct 24 ms 23800 KB n=100
13 Correct 25 ms 23928 KB n=100
14 Correct 23 ms 23928 KB n=100
15 Correct 24 ms 23800 KB n=100
16 Correct 25 ms 23976 KB n=100
17 Correct 24 ms 23924 KB n=100
18 Correct 25 ms 23928 KB n=100
19 Correct 24 ms 23928 KB n=100
20 Correct 24 ms 23932 KB n=100
21 Correct 24 ms 23928 KB n=100
22 Correct 24 ms 23928 KB n=100
23 Correct 23 ms 23928 KB n=100
24 Correct 23 ms 23928 KB n=100
25 Correct 24 ms 23928 KB n=100
26 Correct 24 ms 23928 KB n=12
27 Correct 24 ms 23800 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 24 ms 23800 KB n=5
2 Correct 24 ms 23928 KB n=100
3 Correct 24 ms 23800 KB n=100
4 Correct 24 ms 23880 KB n=100
5 Correct 24 ms 23928 KB n=100
6 Correct 23 ms 23800 KB n=100
7 Correct 23 ms 23928 KB n=100
8 Correct 24 ms 23844 KB n=100
9 Correct 24 ms 23928 KB n=100
10 Correct 24 ms 23928 KB n=100
11 Correct 24 ms 23928 KB n=100
12 Correct 24 ms 23800 KB n=100
13 Correct 25 ms 23928 KB n=100
14 Correct 23 ms 23928 KB n=100
15 Correct 24 ms 23800 KB n=100
16 Correct 25 ms 23976 KB n=100
17 Correct 24 ms 23924 KB n=100
18 Correct 25 ms 23928 KB n=100
19 Correct 24 ms 23928 KB n=100
20 Correct 24 ms 23932 KB n=100
21 Correct 24 ms 23928 KB n=100
22 Correct 24 ms 23928 KB n=100
23 Correct 23 ms 23928 KB n=100
24 Correct 23 ms 23928 KB n=100
25 Correct 24 ms 23928 KB n=100
26 Correct 24 ms 23928 KB n=12
27 Correct 24 ms 23800 KB n=100
28 Correct 25 ms 23900 KB n=500
29 Correct 24 ms 23928 KB n=500
30 Correct 24 ms 23956 KB n=500
31 Correct 24 ms 23928 KB n=500
32 Correct 24 ms 23928 KB n=500
33 Correct 25 ms 23928 KB n=500
34 Correct 29 ms 23928 KB n=500
35 Correct 24 ms 24056 KB n=500
36 Correct 24 ms 23928 KB n=500
37 Correct 24 ms 23928 KB n=500
38 Correct 24 ms 23932 KB n=500
39 Correct 24 ms 23928 KB n=500
40 Correct 29 ms 24056 KB n=500
41 Correct 25 ms 23928 KB n=500
42 Correct 25 ms 23928 KB n=500
43 Correct 25 ms 23928 KB n=500
44 Correct 25 ms 23928 KB n=500
45 Correct 25 ms 23928 KB n=500
46 Correct 24 ms 24056 KB n=500
47 Correct 25 ms 24056 KB n=500
48 Correct 26 ms 23976 KB n=500
49 Correct 24 ms 24056 KB n=500
50 Correct 25 ms 23928 KB n=500
51 Correct 25 ms 23932 KB n=500
52 Correct 24 ms 24056 KB n=500
53 Correct 24 ms 23928 KB n=500
54 Correct 24 ms 24060 KB n=500
55 Correct 24 ms 23928 KB n=278
56 Correct 25 ms 24056 KB n=500
57 Correct 25 ms 24056 KB n=500
58 Correct 24 ms 23932 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 24 ms 23800 KB n=5
2 Correct 24 ms 23928 KB n=100
3 Correct 24 ms 23800 KB n=100
4 Correct 24 ms 23880 KB n=100
5 Correct 24 ms 23928 KB n=100
6 Correct 23 ms 23800 KB n=100
7 Correct 23 ms 23928 KB n=100
8 Correct 24 ms 23844 KB n=100
9 Correct 24 ms 23928 KB n=100
10 Correct 24 ms 23928 KB n=100
11 Correct 24 ms 23928 KB n=100
12 Correct 24 ms 23800 KB n=100
13 Correct 25 ms 23928 KB n=100
14 Correct 23 ms 23928 KB n=100
15 Correct 24 ms 23800 KB n=100
16 Correct 25 ms 23976 KB n=100
17 Correct 24 ms 23924 KB n=100
18 Correct 25 ms 23928 KB n=100
19 Correct 24 ms 23928 KB n=100
20 Correct 24 ms 23932 KB n=100
21 Correct 24 ms 23928 KB n=100
22 Correct 24 ms 23928 KB n=100
23 Correct 23 ms 23928 KB n=100
24 Correct 23 ms 23928 KB n=100
25 Correct 24 ms 23928 KB n=100
26 Correct 24 ms 23928 KB n=12
27 Correct 24 ms 23800 KB n=100
28 Correct 25 ms 23900 KB n=500
29 Correct 24 ms 23928 KB n=500
30 Correct 24 ms 23956 KB n=500
31 Correct 24 ms 23928 KB n=500
32 Correct 24 ms 23928 KB n=500
33 Correct 25 ms 23928 KB n=500
34 Correct 29 ms 23928 KB n=500
35 Correct 24 ms 24056 KB n=500
36 Correct 24 ms 23928 KB n=500
37 Correct 24 ms 23928 KB n=500
38 Correct 24 ms 23932 KB n=500
39 Correct 24 ms 23928 KB n=500
40 Correct 29 ms 24056 KB n=500
41 Correct 25 ms 23928 KB n=500
42 Correct 25 ms 23928 KB n=500
43 Correct 25 ms 23928 KB n=500
44 Correct 25 ms 23928 KB n=500
45 Correct 25 ms 23928 KB n=500
46 Correct 24 ms 24056 KB n=500
47 Correct 25 ms 24056 KB n=500
48 Correct 26 ms 23976 KB n=500
49 Correct 24 ms 24056 KB n=500
50 Correct 25 ms 23928 KB n=500
51 Correct 25 ms 23932 KB n=500
52 Correct 24 ms 24056 KB n=500
53 Correct 24 ms 23928 KB n=500
54 Correct 24 ms 24060 KB n=500
55 Correct 24 ms 23928 KB n=278
56 Correct 25 ms 24056 KB n=500
57 Correct 25 ms 24056 KB n=500
58 Correct 24 ms 23932 KB n=500
59 Correct 33 ms 24440 KB n=2000
60 Correct 31 ms 24440 KB n=2000
61 Correct 28 ms 24440 KB n=2000
62 Correct 29 ms 24440 KB n=2000
63 Correct 29 ms 24412 KB n=2000
64 Correct 28 ms 24440 KB n=2000
65 Correct 28 ms 24316 KB n=2000
66 Correct 36 ms 24440 KB n=2000
67 Correct 29 ms 24388 KB n=2000
68 Correct 28 ms 24428 KB n=2000
69 Correct 27 ms 24440 KB n=2000
70 Correct 27 ms 24408 KB n=2000
71 Correct 26 ms 24440 KB n=2000
72 Correct 27 ms 24440 KB n=2000
73 Correct 27 ms 24440 KB n=2000
74 Correct 27 ms 24312 KB n=1844
75 Correct 27 ms 24312 KB n=2000
76 Correct 28 ms 24276 KB n=2000
77 Correct 28 ms 24440 KB n=2000
78 Correct 32 ms 24440 KB n=2000
79 Correct 28 ms 24440 KB n=2000
80 Correct 27 ms 24440 KB n=2000
81 Correct 27 ms 24440 KB n=2000
82 Correct 28 ms 24312 KB n=2000
83 Correct 27 ms 24440 KB n=2000
84 Correct 28 ms 24316 KB n=2000
85 Correct 28 ms 24312 KB n=2000
86 Correct 27 ms 24360 KB n=2000
87 Correct 28 ms 24440 KB n=2000
88 Correct 27 ms 24440 KB n=2000
89 Correct 27 ms 24440 KB n=2000
90 Correct 27 ms 24440 KB n=2000
91 Correct 27 ms 24568 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 24 ms 23800 KB n=5
2 Correct 24 ms 23928 KB n=100
3 Correct 24 ms 23800 KB n=100
4 Correct 24 ms 23880 KB n=100
5 Correct 24 ms 23928 KB n=100
6 Correct 23 ms 23800 KB n=100
7 Correct 23 ms 23928 KB n=100
8 Correct 24 ms 23844 KB n=100
9 Correct 24 ms 23928 KB n=100
10 Correct 24 ms 23928 KB n=100
11 Correct 24 ms 23928 KB n=100
12 Correct 24 ms 23800 KB n=100
13 Correct 25 ms 23928 KB n=100
14 Correct 23 ms 23928 KB n=100
15 Correct 24 ms 23800 KB n=100
16 Correct 25 ms 23976 KB n=100
17 Correct 24 ms 23924 KB n=100
18 Correct 25 ms 23928 KB n=100
19 Correct 24 ms 23928 KB n=100
20 Correct 24 ms 23932 KB n=100
21 Correct 24 ms 23928 KB n=100
22 Correct 24 ms 23928 KB n=100
23 Correct 23 ms 23928 KB n=100
24 Correct 23 ms 23928 KB n=100
25 Correct 24 ms 23928 KB n=100
26 Correct 24 ms 23928 KB n=12
27 Correct 24 ms 23800 KB n=100
28 Correct 25 ms 23900 KB n=500
29 Correct 24 ms 23928 KB n=500
30 Correct 24 ms 23956 KB n=500
31 Correct 24 ms 23928 KB n=500
32 Correct 24 ms 23928 KB n=500
33 Correct 25 ms 23928 KB n=500
34 Correct 29 ms 23928 KB n=500
35 Correct 24 ms 24056 KB n=500
36 Correct 24 ms 23928 KB n=500
37 Correct 24 ms 23928 KB n=500
38 Correct 24 ms 23932 KB n=500
39 Correct 24 ms 23928 KB n=500
40 Correct 29 ms 24056 KB n=500
41 Correct 25 ms 23928 KB n=500
42 Correct 25 ms 23928 KB n=500
43 Correct 25 ms 23928 KB n=500
44 Correct 25 ms 23928 KB n=500
45 Correct 25 ms 23928 KB n=500
46 Correct 24 ms 24056 KB n=500
47 Correct 25 ms 24056 KB n=500
48 Correct 26 ms 23976 KB n=500
49 Correct 24 ms 24056 KB n=500
50 Correct 25 ms 23928 KB n=500
51 Correct 25 ms 23932 KB n=500
52 Correct 24 ms 24056 KB n=500
53 Correct 24 ms 23928 KB n=500
54 Correct 24 ms 24060 KB n=500
55 Correct 24 ms 23928 KB n=278
56 Correct 25 ms 24056 KB n=500
57 Correct 25 ms 24056 KB n=500
58 Correct 24 ms 23932 KB n=500
59 Correct 33 ms 24440 KB n=2000
60 Correct 31 ms 24440 KB n=2000
61 Correct 28 ms 24440 KB n=2000
62 Correct 29 ms 24440 KB n=2000
63 Correct 29 ms 24412 KB n=2000
64 Correct 28 ms 24440 KB n=2000
65 Correct 28 ms 24316 KB n=2000
66 Correct 36 ms 24440 KB n=2000
67 Correct 29 ms 24388 KB n=2000
68 Correct 28 ms 24428 KB n=2000
69 Correct 27 ms 24440 KB n=2000
70 Correct 27 ms 24408 KB n=2000
71 Correct 26 ms 24440 KB n=2000
72 Correct 27 ms 24440 KB n=2000
73 Correct 27 ms 24440 KB n=2000
74 Correct 27 ms 24312 KB n=1844
75 Correct 27 ms 24312 KB n=2000
76 Correct 28 ms 24276 KB n=2000
77 Correct 28 ms 24440 KB n=2000
78 Correct 32 ms 24440 KB n=2000
79 Correct 28 ms 24440 KB n=2000
80 Correct 27 ms 24440 KB n=2000
81 Correct 27 ms 24440 KB n=2000
82 Correct 28 ms 24312 KB n=2000
83 Correct 27 ms 24440 KB n=2000
84 Correct 28 ms 24316 KB n=2000
85 Correct 28 ms 24312 KB n=2000
86 Correct 27 ms 24360 KB n=2000
87 Correct 28 ms 24440 KB n=2000
88 Correct 27 ms 24440 KB n=2000
89 Correct 27 ms 24440 KB n=2000
90 Correct 27 ms 24440 KB n=2000
91 Correct 27 ms 24568 KB n=2000
92 Correct 907 ms 75484 KB n=200000
93 Correct 951 ms 80296 KB n=200000
94 Correct 770 ms 83680 KB n=200000
95 Correct 881 ms 75244 KB n=200000
96 Correct 895 ms 75248 KB n=200000
97 Correct 1052 ms 79520 KB n=200000
98 Correct 931 ms 75204 KB n=200000
99 Correct 1022 ms 75404 KB n=200000
100 Correct 917 ms 75300 KB n=200000
101 Correct 701 ms 84988 KB n=200000
102 Correct 533 ms 76408 KB n=200000
103 Correct 540 ms 76536 KB n=200000
104 Correct 528 ms 76412 KB n=200000
105 Correct 556 ms 76792 KB n=200000
106 Correct 532 ms 76792 KB n=200000
107 Correct 567 ms 76772 KB n=200000
108 Correct 923 ms 75396 KB n=200000
109 Correct 982 ms 75404 KB n=200000
110 Correct 932 ms 75452 KB n=200000
111 Correct 904 ms 74736 KB n=200000
112 Correct 738 ms 84012 KB n=200000
113 Correct 1122 ms 79412 KB n=200000
114 Correct 934 ms 74984 KB n=200000
115 Correct 1237 ms 77292 KB n=200000
116 Correct 869 ms 75600 KB n=200000
117 Correct 885 ms 84344 KB n=200000
118 Correct 1114 ms 78236 KB n=200000
119 Correct 890 ms 75472 KB n=200000
120 Correct 614 ms 83796 KB n=200000
121 Correct 642 ms 83832 KB n=200000
122 Correct 621 ms 84100 KB n=200000
123 Correct 539 ms 76636 KB n=200000
124 Correct 199 ms 38904 KB n=25264