Submission #865581

# Submission time Handle Problem Language Result Execution time Memory
865581 2023-10-24T11:11:10 Z vjudge1 Birthday gift (IZhO18_treearray) C++17
100 / 100
641 ms 85216 KB
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <fstream>
#include <unordered_map>
using namespace std;

typedef long long ll;
typedef long double ld;

#define all(x) x.begin(),x.end()
#define pb push_back
#define ent "\n"

const int maxn = (int)2e5 + 13;
const ll inf = (long long)1e18 + 20;
const int mod = (int)1e9 + 7;

int n,m,q;
int a[maxn];
int up[maxn][21];
int sz[maxn];
int b[maxn];
vector<int>g[maxn];
set<int>pos[maxn],pos1[maxn];

void calc(int v,int p ){
	for(int to : g[v]){
		if(to != p){
			up[to][0] = v;
			sz[to] = sz[v] + 1;
			calc(to,v); 
		}
	}
}

int lca(int x,int y){
	if(sz[x] > sz[y])swap(x,y);
	int dif = sz[y] - sz[x];
	for(int i = 0 ; i <= 20 ; i ++){
		if((dif >> i)&1){
			y = up[y][i];
		}
	}
	if(x == y)return x;
	for(int i = 20 ; i >= 0 ; i --){
		if(up[x][i]!=up[y][i] && min(up[x][i],up[y][i]) > 0){
			x = up[x][i];
			y = up[y][i];
		}
	}
	return up[x][0];
}

void solve(){
	cin >> n >> m >> q;
	for(int i = 1 ; i < n ; i ++){
		int x,y;cin >> x >> y;
		g[x].pb(y);
		g[y].pb(x);
	}
	for(int i = 1 ; i <= m ; i ++){
		cin >> a[i];
		pos[a[i]].insert(i);
	}
	calc(1,0);
	for(int i = 1 ; i <= 20 ; i ++){
		for(int j = 1 ; j <= n ; j ++){
			up[j][i] = up[up[j][i - 1]][i - 1];
		}
	}
	for(int i = 1 ; i < m ; i ++){
		pos1[lca(a[i],a[i + 1])].insert(i);
	}
	while(q--){
		int tp;cin >> tp;
		if(tp == 2){
			int l,r,v;cin >> l >> r >> v;
			if(pos[v].lower_bound(l) != pos[v].end() && *pos[v].lower_bound(l) <= r){
				int x = *pos[v].lower_bound(l);
				cout << x <<' ' << x << ent;
				continue;
			}
			if(pos1[v].lower_bound(l) != pos1[v].end() && *pos1[v].lower_bound(l) < r){
				int x = *pos1[v].lower_bound(l);
				cout << x << ' ' << x + 1 << ent;
				continue;
			}
			cout << "-1 -1\n";
		}
		else{
			int in,v;cin >> in >> v;
			pos[a[in]].erase(in);
			pos[v].insert(in);
			if(in < m)pos1[lca(a[in],a[in + 1])].erase(in);
			if(in > 1)pos1[lca(a[in - 1],a[in])].erase(in - 1);
			a[in] = v;
			if(in < m)pos1[lca(a[in],a[in + 1])].insert(in);
			if(in > 1)pos1[lca(a[in - 1],a[in])].insert(in - 1);
		}
	}
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int t = 1;
	//cin >> t;
	while(t --){
		solve();
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 27224 KB n=5
2 Correct 5 ms 27228 KB n=100
3 Correct 5 ms 27224 KB n=100
4 Correct 5 ms 27228 KB n=100
5 Correct 5 ms 27228 KB n=100
6 Correct 5 ms 27228 KB n=100
7 Correct 5 ms 27228 KB n=100
8 Correct 5 ms 27228 KB n=100
9 Correct 5 ms 27316 KB n=100
10 Correct 5 ms 27224 KB n=100
11 Correct 5 ms 27228 KB n=100
12 Correct 5 ms 27224 KB n=100
13 Correct 5 ms 27228 KB n=100
14 Correct 5 ms 27228 KB n=100
15 Correct 5 ms 27228 KB n=100
16 Correct 5 ms 27228 KB n=100
17 Correct 5 ms 27228 KB n=100
18 Correct 5 ms 27324 KB n=100
19 Correct 5 ms 27228 KB n=100
20 Correct 5 ms 27224 KB n=100
21 Correct 6 ms 27332 KB n=100
22 Correct 5 ms 27228 KB n=100
23 Correct 5 ms 27228 KB n=100
24 Correct 5 ms 27228 KB n=100
25 Correct 5 ms 27224 KB n=100
26 Correct 5 ms 27228 KB n=12
27 Correct 5 ms 27224 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 5 ms 27224 KB n=5
2 Correct 5 ms 27228 KB n=100
3 Correct 5 ms 27224 KB n=100
4 Correct 5 ms 27228 KB n=100
5 Correct 5 ms 27228 KB n=100
6 Correct 5 ms 27228 KB n=100
7 Correct 5 ms 27228 KB n=100
8 Correct 5 ms 27228 KB n=100
9 Correct 5 ms 27316 KB n=100
10 Correct 5 ms 27224 KB n=100
11 Correct 5 ms 27228 KB n=100
12 Correct 5 ms 27224 KB n=100
13 Correct 5 ms 27228 KB n=100
14 Correct 5 ms 27228 KB n=100
15 Correct 5 ms 27228 KB n=100
16 Correct 5 ms 27228 KB n=100
17 Correct 5 ms 27228 KB n=100
18 Correct 5 ms 27324 KB n=100
19 Correct 5 ms 27228 KB n=100
20 Correct 5 ms 27224 KB n=100
21 Correct 6 ms 27332 KB n=100
22 Correct 5 ms 27228 KB n=100
23 Correct 5 ms 27228 KB n=100
24 Correct 5 ms 27228 KB n=100
25 Correct 5 ms 27224 KB n=100
26 Correct 5 ms 27228 KB n=12
27 Correct 5 ms 27224 KB n=100
28 Correct 5 ms 27228 KB n=500
29 Correct 5 ms 27228 KB n=500
30 Correct 7 ms 27740 KB n=500
31 Correct 6 ms 27224 KB n=500
32 Correct 5 ms 27228 KB n=500
33 Correct 6 ms 27228 KB n=500
34 Correct 6 ms 27412 KB n=500
35 Correct 6 ms 27228 KB n=500
36 Correct 5 ms 27316 KB n=500
37 Correct 5 ms 27228 KB n=500
38 Correct 6 ms 27316 KB n=500
39 Correct 5 ms 27228 KB n=500
40 Correct 6 ms 27228 KB n=500
41 Correct 6 ms 27228 KB n=500
42 Correct 6 ms 27224 KB n=500
43 Correct 6 ms 27228 KB n=500
44 Correct 6 ms 27228 KB n=500
45 Correct 6 ms 27224 KB n=500
46 Correct 6 ms 27228 KB n=500
47 Correct 6 ms 27224 KB n=500
48 Correct 6 ms 27228 KB n=500
49 Correct 6 ms 27228 KB n=500
50 Correct 6 ms 27228 KB n=500
51 Correct 6 ms 27228 KB n=500
52 Correct 6 ms 27228 KB n=500
53 Correct 6 ms 27228 KB n=500
54 Correct 6 ms 27228 KB n=500
55 Correct 6 ms 27228 KB n=278
56 Correct 6 ms 27228 KB n=500
57 Correct 6 ms 27228 KB n=500
58 Correct 6 ms 27224 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 5 ms 27224 KB n=5
2 Correct 5 ms 27228 KB n=100
3 Correct 5 ms 27224 KB n=100
4 Correct 5 ms 27228 KB n=100
5 Correct 5 ms 27228 KB n=100
6 Correct 5 ms 27228 KB n=100
7 Correct 5 ms 27228 KB n=100
8 Correct 5 ms 27228 KB n=100
9 Correct 5 ms 27316 KB n=100
10 Correct 5 ms 27224 KB n=100
11 Correct 5 ms 27228 KB n=100
12 Correct 5 ms 27224 KB n=100
13 Correct 5 ms 27228 KB n=100
14 Correct 5 ms 27228 KB n=100
15 Correct 5 ms 27228 KB n=100
16 Correct 5 ms 27228 KB n=100
17 Correct 5 ms 27228 KB n=100
18 Correct 5 ms 27324 KB n=100
19 Correct 5 ms 27228 KB n=100
20 Correct 5 ms 27224 KB n=100
21 Correct 6 ms 27332 KB n=100
22 Correct 5 ms 27228 KB n=100
23 Correct 5 ms 27228 KB n=100
24 Correct 5 ms 27228 KB n=100
25 Correct 5 ms 27224 KB n=100
26 Correct 5 ms 27228 KB n=12
27 Correct 5 ms 27224 KB n=100
28 Correct 5 ms 27228 KB n=500
29 Correct 5 ms 27228 KB n=500
30 Correct 7 ms 27740 KB n=500
31 Correct 6 ms 27224 KB n=500
32 Correct 5 ms 27228 KB n=500
33 Correct 6 ms 27228 KB n=500
34 Correct 6 ms 27412 KB n=500
35 Correct 6 ms 27228 KB n=500
36 Correct 5 ms 27316 KB n=500
37 Correct 5 ms 27228 KB n=500
38 Correct 6 ms 27316 KB n=500
39 Correct 5 ms 27228 KB n=500
40 Correct 6 ms 27228 KB n=500
41 Correct 6 ms 27228 KB n=500
42 Correct 6 ms 27224 KB n=500
43 Correct 6 ms 27228 KB n=500
44 Correct 6 ms 27228 KB n=500
45 Correct 6 ms 27224 KB n=500
46 Correct 6 ms 27228 KB n=500
47 Correct 6 ms 27224 KB n=500
48 Correct 6 ms 27228 KB n=500
49 Correct 6 ms 27228 KB n=500
50 Correct 6 ms 27228 KB n=500
51 Correct 6 ms 27228 KB n=500
52 Correct 6 ms 27228 KB n=500
53 Correct 6 ms 27228 KB n=500
54 Correct 6 ms 27228 KB n=500
55 Correct 6 ms 27228 KB n=278
56 Correct 6 ms 27228 KB n=500
57 Correct 6 ms 27228 KB n=500
58 Correct 6 ms 27224 KB n=500
59 Correct 8 ms 27648 KB n=2000
60 Correct 8 ms 27740 KB n=2000
61 Correct 8 ms 27484 KB n=2000
62 Correct 8 ms 27668 KB n=2000
63 Correct 8 ms 27484 KB n=2000
64 Correct 8 ms 27484 KB n=2000
65 Correct 8 ms 27484 KB n=2000
66 Correct 7 ms 27716 KB n=2000
67 Correct 8 ms 27484 KB n=2000
68 Correct 7 ms 27700 KB n=2000
69 Correct 7 ms 27480 KB n=2000
70 Correct 7 ms 27568 KB n=2000
71 Correct 7 ms 27484 KB n=2000
72 Correct 7 ms 27484 KB n=2000
73 Correct 7 ms 27480 KB n=2000
74 Correct 7 ms 27484 KB n=1844
75 Correct 7 ms 27480 KB n=2000
76 Correct 7 ms 27480 KB n=2000
77 Correct 8 ms 27484 KB n=2000
78 Correct 7 ms 27484 KB n=2000
79 Correct 7 ms 27484 KB n=2000
80 Correct 7 ms 27736 KB n=2000
81 Correct 7 ms 27572 KB n=2000
82 Correct 7 ms 27892 KB n=2000
83 Correct 8 ms 27576 KB n=2000
84 Correct 8 ms 27480 KB n=2000
85 Correct 7 ms 27680 KB n=2000
86 Correct 8 ms 27484 KB n=2000
87 Correct 10 ms 27480 KB n=2000
88 Correct 7 ms 27736 KB n=2000
89 Correct 7 ms 27736 KB n=2000
90 Correct 7 ms 27740 KB n=2000
91 Correct 7 ms 27684 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 5 ms 27224 KB n=5
2 Correct 5 ms 27228 KB n=100
3 Correct 5 ms 27224 KB n=100
4 Correct 5 ms 27228 KB n=100
5 Correct 5 ms 27228 KB n=100
6 Correct 5 ms 27228 KB n=100
7 Correct 5 ms 27228 KB n=100
8 Correct 5 ms 27228 KB n=100
9 Correct 5 ms 27316 KB n=100
10 Correct 5 ms 27224 KB n=100
11 Correct 5 ms 27228 KB n=100
12 Correct 5 ms 27224 KB n=100
13 Correct 5 ms 27228 KB n=100
14 Correct 5 ms 27228 KB n=100
15 Correct 5 ms 27228 KB n=100
16 Correct 5 ms 27228 KB n=100
17 Correct 5 ms 27228 KB n=100
18 Correct 5 ms 27324 KB n=100
19 Correct 5 ms 27228 KB n=100
20 Correct 5 ms 27224 KB n=100
21 Correct 6 ms 27332 KB n=100
22 Correct 5 ms 27228 KB n=100
23 Correct 5 ms 27228 KB n=100
24 Correct 5 ms 27228 KB n=100
25 Correct 5 ms 27224 KB n=100
26 Correct 5 ms 27228 KB n=12
27 Correct 5 ms 27224 KB n=100
28 Correct 5 ms 27228 KB n=500
29 Correct 5 ms 27228 KB n=500
30 Correct 7 ms 27740 KB n=500
31 Correct 6 ms 27224 KB n=500
32 Correct 5 ms 27228 KB n=500
33 Correct 6 ms 27228 KB n=500
34 Correct 6 ms 27412 KB n=500
35 Correct 6 ms 27228 KB n=500
36 Correct 5 ms 27316 KB n=500
37 Correct 5 ms 27228 KB n=500
38 Correct 6 ms 27316 KB n=500
39 Correct 5 ms 27228 KB n=500
40 Correct 6 ms 27228 KB n=500
41 Correct 6 ms 27228 KB n=500
42 Correct 6 ms 27224 KB n=500
43 Correct 6 ms 27228 KB n=500
44 Correct 6 ms 27228 KB n=500
45 Correct 6 ms 27224 KB n=500
46 Correct 6 ms 27228 KB n=500
47 Correct 6 ms 27224 KB n=500
48 Correct 6 ms 27228 KB n=500
49 Correct 6 ms 27228 KB n=500
50 Correct 6 ms 27228 KB n=500
51 Correct 6 ms 27228 KB n=500
52 Correct 6 ms 27228 KB n=500
53 Correct 6 ms 27228 KB n=500
54 Correct 6 ms 27228 KB n=500
55 Correct 6 ms 27228 KB n=278
56 Correct 6 ms 27228 KB n=500
57 Correct 6 ms 27228 KB n=500
58 Correct 6 ms 27224 KB n=500
59 Correct 8 ms 27648 KB n=2000
60 Correct 8 ms 27740 KB n=2000
61 Correct 8 ms 27484 KB n=2000
62 Correct 8 ms 27668 KB n=2000
63 Correct 8 ms 27484 KB n=2000
64 Correct 8 ms 27484 KB n=2000
65 Correct 8 ms 27484 KB n=2000
66 Correct 7 ms 27716 KB n=2000
67 Correct 8 ms 27484 KB n=2000
68 Correct 7 ms 27700 KB n=2000
69 Correct 7 ms 27480 KB n=2000
70 Correct 7 ms 27568 KB n=2000
71 Correct 7 ms 27484 KB n=2000
72 Correct 7 ms 27484 KB n=2000
73 Correct 7 ms 27480 KB n=2000
74 Correct 7 ms 27484 KB n=1844
75 Correct 7 ms 27480 KB n=2000
76 Correct 7 ms 27480 KB n=2000
77 Correct 8 ms 27484 KB n=2000
78 Correct 7 ms 27484 KB n=2000
79 Correct 7 ms 27484 KB n=2000
80 Correct 7 ms 27736 KB n=2000
81 Correct 7 ms 27572 KB n=2000
82 Correct 7 ms 27892 KB n=2000
83 Correct 8 ms 27576 KB n=2000
84 Correct 8 ms 27480 KB n=2000
85 Correct 7 ms 27680 KB n=2000
86 Correct 8 ms 27484 KB n=2000
87 Correct 10 ms 27480 KB n=2000
88 Correct 7 ms 27736 KB n=2000
89 Correct 7 ms 27736 KB n=2000
90 Correct 7 ms 27740 KB n=2000
91 Correct 7 ms 27684 KB n=2000
92 Correct 466 ms 73948 KB n=200000
93 Correct 594 ms 79244 KB n=200000
94 Correct 571 ms 83668 KB n=200000
95 Correct 448 ms 73196 KB n=200000
96 Correct 423 ms 73156 KB n=200000
97 Correct 641 ms 77728 KB n=200000
98 Correct 446 ms 73188 KB n=200000
99 Correct 638 ms 72932 KB n=200000
100 Correct 472 ms 73252 KB n=200000
101 Correct 543 ms 85216 KB n=200000
102 Correct 245 ms 74068 KB n=200000
103 Correct 300 ms 74020 KB n=200000
104 Correct 279 ms 74288 KB n=200000
105 Correct 248 ms 74064 KB n=200000
106 Correct 257 ms 74068 KB n=200000
107 Correct 255 ms 70304 KB n=200000
108 Correct 522 ms 69040 KB n=200000
109 Correct 481 ms 69456 KB n=200000
110 Correct 477 ms 69468 KB n=200000
111 Correct 462 ms 69776 KB n=200000
112 Correct 574 ms 80020 KB n=200000
113 Correct 636 ms 73544 KB n=200000
114 Correct 453 ms 69480 KB n=200000
115 Correct 621 ms 71184 KB n=200000
116 Correct 429 ms 69220 KB n=200000
117 Correct 534 ms 80300 KB n=200000
118 Correct 600 ms 72016 KB n=200000
119 Correct 452 ms 69360 KB n=200000
120 Correct 596 ms 81100 KB n=200000
121 Correct 613 ms 80708 KB n=200000
122 Correct 588 ms 81156 KB n=200000
123 Correct 274 ms 69972 KB n=200000
124 Correct 116 ms 40528 KB n=25264