Submission #503482

# Submission time Handle Problem Language Result Execution time Memory
503482 2022-01-08T07:20:44 Z PoPularPlusPlus Birthday gift (IZhO18_treearray) C++17
100 / 100
969 ms 75776 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long 
#define pb(e) push_back(e)
#define sv(a) sort(a.begin(),a.end())
#define sa(a,n) sort(a,a+n)
#define mp(a,b) make_pair(a,b)
#define vf first
#define vs second
#define ar array
#define all(x) x.begin(),x.end()
const int inf = 0x3f3f3f3f;
const int mod = 1000000007; 
const double PI=3.14159265358979323846264338327950288419716939937510582097494459230;
bool remender(ll a , ll b){return a%b;}

/* in each query , the chosen x and y would be x == y or x + 1 == y */
/*
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
*/
const int N = 200004;

vector<int> adj[N];

const int L = 25;

int timer , up[N][L] , tin[N] , tout[N];

void dfs(int node , int par = 1){
	//cout << node << ' ' << par << endl;
	tin[node] = ++timer;
	//cout << node << ' ';
	up[node][0] = par;
	for(int i = 1; i < L; i++)up[node][i] = up[up[node][i-1]][i-1];
	for(int i : adj[node]){
		if(i!=par)dfs(i , node);
	}
	tout[node] = ++timer;
}

bool islca(int x , int y){
	return (tin[x] <= tin[y] && tout[x] >= tout[y]);
}

int findlca(int x , int y){
	if(islca(x,y))return x;
	if(islca(y,x))return y;
	for(int i = L - 1; i >= 0; i--){
		if(!islca(up[x][i] , y))x = up[x][i];
	}
	return up[x][0];
}

void solve(){
	int n;
	cin >> n;
	int m;
	cin >> m;
	int q;
	cin >> q;
	for(int i = 0; i < n-1; i++){
		int a , b;
		cin >> a >> b;
		adj[a].pb(b);
		adj[b].pb(a);
	}
	timer = 0;
	dfs(1);
	int arr[m];
	for(int i = 0; i < m; i++)cin >> arr[i];
	set<int> cnt[n + 1];
	set<int> itself[n + 1];
	for(int i = 0; i < m - 1; i++){
		cnt[findlca(arr[i] , arr[i + 1])].insert(i);
		//cout << findlca(arr[i] , arr[i + 1]) << ' ';
	}
	for(int i = 0; i < m; i++){
		itself[arr[i]].insert(i);
	}
	while(q--){
		int op;
		cin >> op;
		if(op == 1){
			int pos , v;
			cin >> pos >> v;
			pos--;
			itself[arr[pos]].erase(pos);
			if(pos + 1 < m)
			cnt[findlca(arr[pos] , arr[pos + 1])].erase(pos);
			if(pos - 1 >= 0)
			cnt[findlca(arr[pos - 1] , arr[pos])].erase(pos-1);
			arr[pos] = v;
			itself[v].insert(pos);
			if(pos + 1 < m){
				cnt[findlca(v , arr[pos + 1])].insert(pos);
			}
			if(pos - 1 >= 0){
				cnt[findlca(v,  arr[pos - 1])].insert(pos - 1);
			}
		}
		else {
			int l , r , v;
			cin >> l >> r >> v;
			l--;
			r--;
			int ans1 , ans2;
			ans1 = ans2 = -2;
			auto it = itself[v].lower_bound(l);
			if(it!=itself[v].end()){
			int p = *it;
			if(p <= r){
				ans1 = ans2 = p;
			}
			}
			if(l < r){
				it = cnt[v].lower_bound(l);
				if(it!=cnt[v].end()){
				int pos = *it;
				if(pos < r){
					ans1 = pos;
					ans2 = pos + 1;
				}
			}
			}
			if(ans1 >= l && ans2 <= r)
			cout << ans1 + 1 << ' ' << ans2 + 1 << '\n';
			else if(ans1 == -2 && ans2 == -2)cout << ans1 + 1 << ' ' << ans2 + 1 << '\n';
			else {
				while(1){
					;
				}
			}
		}
	}
}

int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//freopen("dec.in", "r", stdin);
//freopen("dec.out", "w", stdout);
	//int t;cin >> t;while(t--)
	solve();
	return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 2 ms 4940 KB n=5
2 Correct 2 ms 4940 KB n=100
3 Correct 3 ms 4940 KB n=100
4 Correct 3 ms 5012 KB n=100
5 Correct 3 ms 5008 KB n=100
6 Correct 3 ms 5000 KB n=100
7 Correct 4 ms 5008 KB n=100
8 Correct 3 ms 4940 KB n=100
9 Correct 3 ms 5012 KB n=100
10 Correct 3 ms 5056 KB n=100
11 Correct 3 ms 4940 KB n=100
12 Correct 2 ms 4940 KB n=100
13 Correct 2 ms 4940 KB n=100
14 Correct 3 ms 5020 KB n=100
15 Correct 3 ms 4940 KB n=100
16 Correct 3 ms 4940 KB n=100
17 Correct 3 ms 5020 KB n=100
18 Correct 3 ms 5016 KB n=100
19 Correct 3 ms 4940 KB n=100
20 Correct 4 ms 4940 KB n=100
21 Correct 4 ms 5012 KB n=100
22 Correct 3 ms 5020 KB n=100
23 Correct 3 ms 4940 KB n=100
24 Correct 3 ms 4940 KB n=100
25 Correct 3 ms 4940 KB n=100
26 Correct 3 ms 4940 KB n=12
27 Correct 3 ms 5024 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4940 KB n=5
2 Correct 2 ms 4940 KB n=100
3 Correct 3 ms 4940 KB n=100
4 Correct 3 ms 5012 KB n=100
5 Correct 3 ms 5008 KB n=100
6 Correct 3 ms 5000 KB n=100
7 Correct 4 ms 5008 KB n=100
8 Correct 3 ms 4940 KB n=100
9 Correct 3 ms 5012 KB n=100
10 Correct 3 ms 5056 KB n=100
11 Correct 3 ms 4940 KB n=100
12 Correct 2 ms 4940 KB n=100
13 Correct 2 ms 4940 KB n=100
14 Correct 3 ms 5020 KB n=100
15 Correct 3 ms 4940 KB n=100
16 Correct 3 ms 4940 KB n=100
17 Correct 3 ms 5020 KB n=100
18 Correct 3 ms 5016 KB n=100
19 Correct 3 ms 4940 KB n=100
20 Correct 4 ms 4940 KB n=100
21 Correct 4 ms 5012 KB n=100
22 Correct 3 ms 5020 KB n=100
23 Correct 3 ms 4940 KB n=100
24 Correct 3 ms 4940 KB n=100
25 Correct 3 ms 4940 KB n=100
26 Correct 3 ms 4940 KB n=12
27 Correct 3 ms 5024 KB n=100
28 Correct 4 ms 5196 KB n=500
29 Correct 3 ms 5196 KB n=500
30 Correct 5 ms 5196 KB n=500
31 Correct 3 ms 5196 KB n=500
32 Correct 4 ms 5196 KB n=500
33 Correct 3 ms 5196 KB n=500
34 Correct 3 ms 5124 KB n=500
35 Correct 3 ms 5196 KB n=500
36 Correct 3 ms 5196 KB n=500
37 Correct 3 ms 5196 KB n=500
38 Correct 3 ms 5156 KB n=500
39 Correct 4 ms 5196 KB n=500
40 Correct 3 ms 5196 KB n=500
41 Correct 3 ms 5196 KB n=500
42 Correct 3 ms 5196 KB n=500
43 Correct 4 ms 5196 KB n=500
44 Correct 3 ms 5196 KB n=500
45 Correct 3 ms 5156 KB n=500
46 Correct 3 ms 5196 KB n=500
47 Correct 3 ms 5160 KB n=500
48 Correct 4 ms 5196 KB n=500
49 Correct 3 ms 5196 KB n=500
50 Correct 3 ms 5196 KB n=500
51 Correct 3 ms 5196 KB n=500
52 Correct 3 ms 5196 KB n=500
53 Correct 4 ms 5196 KB n=500
54 Correct 3 ms 5196 KB n=500
55 Correct 3 ms 5076 KB n=278
56 Correct 4 ms 5152 KB n=500
57 Correct 3 ms 5196 KB n=500
58 Correct 4 ms 5216 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4940 KB n=5
2 Correct 2 ms 4940 KB n=100
3 Correct 3 ms 4940 KB n=100
4 Correct 3 ms 5012 KB n=100
5 Correct 3 ms 5008 KB n=100
6 Correct 3 ms 5000 KB n=100
7 Correct 4 ms 5008 KB n=100
8 Correct 3 ms 4940 KB n=100
9 Correct 3 ms 5012 KB n=100
10 Correct 3 ms 5056 KB n=100
11 Correct 3 ms 4940 KB n=100
12 Correct 2 ms 4940 KB n=100
13 Correct 2 ms 4940 KB n=100
14 Correct 3 ms 5020 KB n=100
15 Correct 3 ms 4940 KB n=100
16 Correct 3 ms 4940 KB n=100
17 Correct 3 ms 5020 KB n=100
18 Correct 3 ms 5016 KB n=100
19 Correct 3 ms 4940 KB n=100
20 Correct 4 ms 4940 KB n=100
21 Correct 4 ms 5012 KB n=100
22 Correct 3 ms 5020 KB n=100
23 Correct 3 ms 4940 KB n=100
24 Correct 3 ms 4940 KB n=100
25 Correct 3 ms 4940 KB n=100
26 Correct 3 ms 4940 KB n=12
27 Correct 3 ms 5024 KB n=100
28 Correct 4 ms 5196 KB n=500
29 Correct 3 ms 5196 KB n=500
30 Correct 5 ms 5196 KB n=500
31 Correct 3 ms 5196 KB n=500
32 Correct 4 ms 5196 KB n=500
33 Correct 3 ms 5196 KB n=500
34 Correct 3 ms 5124 KB n=500
35 Correct 3 ms 5196 KB n=500
36 Correct 3 ms 5196 KB n=500
37 Correct 3 ms 5196 KB n=500
38 Correct 3 ms 5156 KB n=500
39 Correct 4 ms 5196 KB n=500
40 Correct 3 ms 5196 KB n=500
41 Correct 3 ms 5196 KB n=500
42 Correct 3 ms 5196 KB n=500
43 Correct 4 ms 5196 KB n=500
44 Correct 3 ms 5196 KB n=500
45 Correct 3 ms 5156 KB n=500
46 Correct 3 ms 5196 KB n=500
47 Correct 3 ms 5160 KB n=500
48 Correct 4 ms 5196 KB n=500
49 Correct 3 ms 5196 KB n=500
50 Correct 3 ms 5196 KB n=500
51 Correct 3 ms 5196 KB n=500
52 Correct 3 ms 5196 KB n=500
53 Correct 4 ms 5196 KB n=500
54 Correct 3 ms 5196 KB n=500
55 Correct 3 ms 5076 KB n=278
56 Correct 4 ms 5152 KB n=500
57 Correct 3 ms 5196 KB n=500
58 Correct 4 ms 5216 KB n=500
59 Correct 7 ms 5708 KB n=2000
60 Correct 5 ms 5676 KB n=2000
61 Correct 6 ms 5708 KB n=2000
62 Correct 8 ms 5624 KB n=2000
63 Correct 6 ms 5644 KB n=2000
64 Correct 5 ms 5720 KB n=2000
65 Correct 7 ms 5720 KB n=2000
66 Correct 6 ms 5684 KB n=2000
67 Correct 6 ms 5720 KB n=2000
68 Correct 5 ms 5720 KB n=2000
69 Correct 5 ms 5720 KB n=2000
70 Correct 5 ms 5708 KB n=2000
71 Correct 5 ms 5692 KB n=2000
72 Correct 5 ms 5708 KB n=2000
73 Correct 5 ms 5708 KB n=2000
74 Correct 6 ms 5708 KB n=1844
75 Correct 7 ms 5676 KB n=2000
76 Correct 6 ms 5672 KB n=2000
77 Correct 7 ms 5708 KB n=2000
78 Correct 6 ms 5708 KB n=2000
79 Correct 6 ms 5708 KB n=2000
80 Correct 5 ms 5708 KB n=2000
81 Correct 7 ms 5628 KB n=2000
82 Correct 6 ms 5708 KB n=2000
83 Correct 5 ms 5708 KB n=2000
84 Correct 7 ms 5708 KB n=2000
85 Correct 6 ms 5708 KB n=2000
86 Correct 7 ms 5640 KB n=2000
87 Correct 8 ms 5708 KB n=2000
88 Correct 5 ms 5672 KB n=2000
89 Correct 5 ms 5708 KB n=2000
90 Correct 5 ms 5708 KB n=2000
91 Correct 5 ms 5708 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4940 KB n=5
2 Correct 2 ms 4940 KB n=100
3 Correct 3 ms 4940 KB n=100
4 Correct 3 ms 5012 KB n=100
5 Correct 3 ms 5008 KB n=100
6 Correct 3 ms 5000 KB n=100
7 Correct 4 ms 5008 KB n=100
8 Correct 3 ms 4940 KB n=100
9 Correct 3 ms 5012 KB n=100
10 Correct 3 ms 5056 KB n=100
11 Correct 3 ms 4940 KB n=100
12 Correct 2 ms 4940 KB n=100
13 Correct 2 ms 4940 KB n=100
14 Correct 3 ms 5020 KB n=100
15 Correct 3 ms 4940 KB n=100
16 Correct 3 ms 4940 KB n=100
17 Correct 3 ms 5020 KB n=100
18 Correct 3 ms 5016 KB n=100
19 Correct 3 ms 4940 KB n=100
20 Correct 4 ms 4940 KB n=100
21 Correct 4 ms 5012 KB n=100
22 Correct 3 ms 5020 KB n=100
23 Correct 3 ms 4940 KB n=100
24 Correct 3 ms 4940 KB n=100
25 Correct 3 ms 4940 KB n=100
26 Correct 3 ms 4940 KB n=12
27 Correct 3 ms 5024 KB n=100
28 Correct 4 ms 5196 KB n=500
29 Correct 3 ms 5196 KB n=500
30 Correct 5 ms 5196 KB n=500
31 Correct 3 ms 5196 KB n=500
32 Correct 4 ms 5196 KB n=500
33 Correct 3 ms 5196 KB n=500
34 Correct 3 ms 5124 KB n=500
35 Correct 3 ms 5196 KB n=500
36 Correct 3 ms 5196 KB n=500
37 Correct 3 ms 5196 KB n=500
38 Correct 3 ms 5156 KB n=500
39 Correct 4 ms 5196 KB n=500
40 Correct 3 ms 5196 KB n=500
41 Correct 3 ms 5196 KB n=500
42 Correct 3 ms 5196 KB n=500
43 Correct 4 ms 5196 KB n=500
44 Correct 3 ms 5196 KB n=500
45 Correct 3 ms 5156 KB n=500
46 Correct 3 ms 5196 KB n=500
47 Correct 3 ms 5160 KB n=500
48 Correct 4 ms 5196 KB n=500
49 Correct 3 ms 5196 KB n=500
50 Correct 3 ms 5196 KB n=500
51 Correct 3 ms 5196 KB n=500
52 Correct 3 ms 5196 KB n=500
53 Correct 4 ms 5196 KB n=500
54 Correct 3 ms 5196 KB n=500
55 Correct 3 ms 5076 KB n=278
56 Correct 4 ms 5152 KB n=500
57 Correct 3 ms 5196 KB n=500
58 Correct 4 ms 5216 KB n=500
59 Correct 7 ms 5708 KB n=2000
60 Correct 5 ms 5676 KB n=2000
61 Correct 6 ms 5708 KB n=2000
62 Correct 8 ms 5624 KB n=2000
63 Correct 6 ms 5644 KB n=2000
64 Correct 5 ms 5720 KB n=2000
65 Correct 7 ms 5720 KB n=2000
66 Correct 6 ms 5684 KB n=2000
67 Correct 6 ms 5720 KB n=2000
68 Correct 5 ms 5720 KB n=2000
69 Correct 5 ms 5720 KB n=2000
70 Correct 5 ms 5708 KB n=2000
71 Correct 5 ms 5692 KB n=2000
72 Correct 5 ms 5708 KB n=2000
73 Correct 5 ms 5708 KB n=2000
74 Correct 6 ms 5708 KB n=1844
75 Correct 7 ms 5676 KB n=2000
76 Correct 6 ms 5672 KB n=2000
77 Correct 7 ms 5708 KB n=2000
78 Correct 6 ms 5708 KB n=2000
79 Correct 6 ms 5708 KB n=2000
80 Correct 5 ms 5708 KB n=2000
81 Correct 7 ms 5628 KB n=2000
82 Correct 6 ms 5708 KB n=2000
83 Correct 5 ms 5708 KB n=2000
84 Correct 7 ms 5708 KB n=2000
85 Correct 6 ms 5708 KB n=2000
86 Correct 7 ms 5640 KB n=2000
87 Correct 8 ms 5708 KB n=2000
88 Correct 5 ms 5672 KB n=2000
89 Correct 5 ms 5708 KB n=2000
90 Correct 5 ms 5708 KB n=2000
91 Correct 5 ms 5708 KB n=2000
92 Correct 690 ms 73344 KB n=200000
93 Correct 757 ms 72900 KB n=200000
94 Correct 540 ms 72412 KB n=200000
95 Correct 660 ms 73020 KB n=200000
96 Correct 682 ms 73116 KB n=200000
97 Correct 801 ms 72348 KB n=200000
98 Correct 709 ms 73140 KB n=200000
99 Correct 811 ms 72632 KB n=200000
100 Correct 688 ms 73276 KB n=200000
101 Correct 512 ms 72424 KB n=200000
102 Correct 395 ms 75776 KB n=200000
103 Correct 397 ms 75232 KB n=200000
104 Correct 406 ms 75432 KB n=200000
105 Correct 411 ms 75044 KB n=200000
106 Correct 388 ms 74960 KB n=200000
107 Correct 401 ms 74912 KB n=200000
108 Correct 725 ms 73916 KB n=200000
109 Correct 730 ms 74076 KB n=200000
110 Correct 724 ms 74052 KB n=200000
111 Correct 653 ms 74172 KB n=200000
112 Correct 514 ms 73500 KB n=200000
113 Correct 791 ms 73424 KB n=200000
114 Correct 666 ms 74164 KB n=200000
115 Correct 969 ms 73532 KB n=200000
116 Correct 645 ms 74048 KB n=200000
117 Correct 488 ms 73288 KB n=200000
118 Correct 865 ms 73284 KB n=200000
119 Correct 645 ms 73964 KB n=200000
120 Correct 450 ms 73012 KB n=200000
121 Correct 444 ms 72932 KB n=200000
122 Correct 425 ms 73448 KB n=200000
123 Correct 410 ms 74656 KB n=200000
124 Correct 140 ms 21676 KB n=25264