Submission #684583

# Submission time Handle Problem Language Result Execution time Memory
684583 2023-01-21T20:55:03 Z mychecksedad Birthday gift (IZhO18_treearray) C++17
100 / 100
769 ms 151344 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define pb push_back
#define MOD (1e9+7)
#define all(x) x.begin(), x.end()
const int N = 1e6, K = 20;

int n, m, q, a[N], in[N], dp[N][K];
vector<int> g[N], euler;
set<pair<int, int>> v[N];

void dfs(int v, int p){
	in[v] = euler.size();
	euler.pb(v);
	for(int u: g[v]){
		if(u != p){
			dfs(u, v);
			euler.pb(v);
		}
	}
}
int lca(int a, int b){
	a = in[a], b = in[b];
	if(a > b) swap(a, b);
	int k = int(log2(b-a+1));
	int x = dp[a][k], y = dp[b - (1<<k) + 1][k];
	return (in[x] < in[y] ? x : y);
}
void solve(){
	cin >> n >> m >> q;
	for(int i = 0; i < n - 1; ++i){
		int u, v; cin >> u >> v;
		g[u].pb(v);
		g[v].pb(u);
	}

	euler.pb(0);

	dfs(1, 0);


	for(int i = 1; i < euler.size(); ++i) dp[i][0] = euler[i];

	for(int j = 1; j < K; ++j){
		for(int i = 1; i + (1<<j) < euler.size() + 1; ++i){
			int a, b;
			a = dp[i][j - 1];
			b = dp[i + (1<<(j-1))][j - 1];
			if(in[a] < in[b])
				dp[i][j] = a;
			else
				dp[i][j] = b;
		}
	}

	for(int i = 1; i <= m; ++i) cin >> a[i];

	for(int i = 1; i < m; ++i){
		v[lca(a[i], a[i + 1])].insert({i, i + 1});
	}
	for(int i = 1; i <= m; ++i) v[a[i]].insert({i, i});
	

	while(q--){
		int tp; cin >> tp;
		if(tp == 1){
			int pos, u; cin >> pos >> u;
			if(pos > 1){
				int lc = lca(a[pos], a[pos - 1]);
				v[lc].erase(make_pair(pos - 1, pos));
			}
			if(pos < m){
				int lc = lca(a[pos], a[pos + 1]);
				v[lc].erase(make_pair(pos, pos + 1));
			}
			v[a[pos]].erase(make_pair(pos, pos));
			a[pos] = u;
			if(pos > 1){
				int lc = lca(a[pos], a[pos - 1]);
				v[lc].insert(make_pair(pos - 1, pos));
			}
			if(pos < m){
				int lc = lca(a[pos], a[pos + 1]);
				v[lc].insert(make_pair(pos, pos + 1));
			}
			v[a[pos]].insert(make_pair(pos, pos));
		}else{
			int l, r, u; cin >> l >> r >> u;
			auto it = v[u].lower_bound(make_pair(l, -1));
			if(it == v[u].end() || (*it).second > r) cout << "-1 -1\n";
			else cout << (*it).first << ' ' << (*it).second << '\n';
		}
	}
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	solve();
	return 0;
}

Compilation message

treearray.cpp: In function 'void solve()':
treearray.cpp:43:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |  for(int i = 1; i < euler.size(); ++i) dp[i][0] = euler[i];
      |                 ~~^~~~~~~~~~~~~~
treearray.cpp:46:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |   for(int i = 1; i + (1<<j) < euler.size() + 1; ++i){
      |                  ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 31 ms 70740 KB n=5
2 Correct 33 ms 70888 KB n=100
3 Correct 33 ms 70740 KB n=100
4 Correct 31 ms 70740 KB n=100
5 Correct 32 ms 70704 KB n=100
6 Correct 35 ms 70732 KB n=100
7 Correct 33 ms 70860 KB n=100
8 Correct 31 ms 70740 KB n=100
9 Correct 33 ms 70796 KB n=100
10 Correct 35 ms 70800 KB n=100
11 Correct 32 ms 70804 KB n=100
12 Correct 32 ms 70800 KB n=100
13 Correct 33 ms 70772 KB n=100
14 Correct 32 ms 70740 KB n=100
15 Correct 33 ms 70736 KB n=100
16 Correct 32 ms 70740 KB n=100
17 Correct 34 ms 70708 KB n=100
18 Correct 33 ms 70732 KB n=100
19 Correct 32 ms 70740 KB n=100
20 Correct 32 ms 70816 KB n=100
21 Correct 37 ms 70740 KB n=100
22 Correct 35 ms 70700 KB n=100
23 Correct 38 ms 70808 KB n=100
24 Correct 38 ms 70784 KB n=100
25 Correct 32 ms 70700 KB n=100
26 Correct 32 ms 70684 KB n=12
27 Correct 33 ms 70768 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 31 ms 70740 KB n=5
2 Correct 33 ms 70888 KB n=100
3 Correct 33 ms 70740 KB n=100
4 Correct 31 ms 70740 KB n=100
5 Correct 32 ms 70704 KB n=100
6 Correct 35 ms 70732 KB n=100
7 Correct 33 ms 70860 KB n=100
8 Correct 31 ms 70740 KB n=100
9 Correct 33 ms 70796 KB n=100
10 Correct 35 ms 70800 KB n=100
11 Correct 32 ms 70804 KB n=100
12 Correct 32 ms 70800 KB n=100
13 Correct 33 ms 70772 KB n=100
14 Correct 32 ms 70740 KB n=100
15 Correct 33 ms 70736 KB n=100
16 Correct 32 ms 70740 KB n=100
17 Correct 34 ms 70708 KB n=100
18 Correct 33 ms 70732 KB n=100
19 Correct 32 ms 70740 KB n=100
20 Correct 32 ms 70816 KB n=100
21 Correct 37 ms 70740 KB n=100
22 Correct 35 ms 70700 KB n=100
23 Correct 38 ms 70808 KB n=100
24 Correct 38 ms 70784 KB n=100
25 Correct 32 ms 70700 KB n=100
26 Correct 32 ms 70684 KB n=12
27 Correct 33 ms 70768 KB n=100
28 Correct 32 ms 70928 KB n=500
29 Correct 35 ms 70860 KB n=500
30 Correct 42 ms 70864 KB n=500
31 Correct 35 ms 70960 KB n=500
32 Correct 34 ms 70988 KB n=500
33 Correct 34 ms 70936 KB n=500
34 Correct 34 ms 70944 KB n=500
35 Correct 35 ms 70904 KB n=500
36 Correct 34 ms 70920 KB n=500
37 Correct 33 ms 70860 KB n=500
38 Correct 33 ms 70868 KB n=500
39 Correct 33 ms 70924 KB n=500
40 Correct 36 ms 70924 KB n=500
41 Correct 35 ms 70940 KB n=500
42 Correct 34 ms 70924 KB n=500
43 Correct 34 ms 70852 KB n=500
44 Correct 33 ms 70880 KB n=500
45 Correct 33 ms 70908 KB n=500
46 Correct 37 ms 70860 KB n=500
47 Correct 39 ms 70916 KB n=500
48 Correct 33 ms 70948 KB n=500
49 Correct 38 ms 70960 KB n=500
50 Correct 33 ms 70860 KB n=500
51 Correct 32 ms 70960 KB n=500
52 Correct 33 ms 70900 KB n=500
53 Correct 35 ms 70864 KB n=500
54 Correct 38 ms 70896 KB n=500
55 Correct 32 ms 70800 KB n=278
56 Correct 33 ms 70908 KB n=500
57 Correct 34 ms 70860 KB n=500
58 Correct 33 ms 70868 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 31 ms 70740 KB n=5
2 Correct 33 ms 70888 KB n=100
3 Correct 33 ms 70740 KB n=100
4 Correct 31 ms 70740 KB n=100
5 Correct 32 ms 70704 KB n=100
6 Correct 35 ms 70732 KB n=100
7 Correct 33 ms 70860 KB n=100
8 Correct 31 ms 70740 KB n=100
9 Correct 33 ms 70796 KB n=100
10 Correct 35 ms 70800 KB n=100
11 Correct 32 ms 70804 KB n=100
12 Correct 32 ms 70800 KB n=100
13 Correct 33 ms 70772 KB n=100
14 Correct 32 ms 70740 KB n=100
15 Correct 33 ms 70736 KB n=100
16 Correct 32 ms 70740 KB n=100
17 Correct 34 ms 70708 KB n=100
18 Correct 33 ms 70732 KB n=100
19 Correct 32 ms 70740 KB n=100
20 Correct 32 ms 70816 KB n=100
21 Correct 37 ms 70740 KB n=100
22 Correct 35 ms 70700 KB n=100
23 Correct 38 ms 70808 KB n=100
24 Correct 38 ms 70784 KB n=100
25 Correct 32 ms 70700 KB n=100
26 Correct 32 ms 70684 KB n=12
27 Correct 33 ms 70768 KB n=100
28 Correct 32 ms 70928 KB n=500
29 Correct 35 ms 70860 KB n=500
30 Correct 42 ms 70864 KB n=500
31 Correct 35 ms 70960 KB n=500
32 Correct 34 ms 70988 KB n=500
33 Correct 34 ms 70936 KB n=500
34 Correct 34 ms 70944 KB n=500
35 Correct 35 ms 70904 KB n=500
36 Correct 34 ms 70920 KB n=500
37 Correct 33 ms 70860 KB n=500
38 Correct 33 ms 70868 KB n=500
39 Correct 33 ms 70924 KB n=500
40 Correct 36 ms 70924 KB n=500
41 Correct 35 ms 70940 KB n=500
42 Correct 34 ms 70924 KB n=500
43 Correct 34 ms 70852 KB n=500
44 Correct 33 ms 70880 KB n=500
45 Correct 33 ms 70908 KB n=500
46 Correct 37 ms 70860 KB n=500
47 Correct 39 ms 70916 KB n=500
48 Correct 33 ms 70948 KB n=500
49 Correct 38 ms 70960 KB n=500
50 Correct 33 ms 70860 KB n=500
51 Correct 32 ms 70960 KB n=500
52 Correct 33 ms 70900 KB n=500
53 Correct 35 ms 70864 KB n=500
54 Correct 38 ms 70896 KB n=500
55 Correct 32 ms 70800 KB n=278
56 Correct 33 ms 70908 KB n=500
57 Correct 34 ms 70860 KB n=500
58 Correct 33 ms 70868 KB n=500
59 Correct 37 ms 71436 KB n=2000
60 Correct 39 ms 71500 KB n=2000
61 Correct 35 ms 71496 KB n=2000
62 Correct 35 ms 71400 KB n=2000
63 Correct 35 ms 71436 KB n=2000
64 Correct 36 ms 71380 KB n=2000
65 Correct 37 ms 71368 KB n=2000
66 Correct 36 ms 71432 KB n=2000
67 Correct 36 ms 71436 KB n=2000
68 Correct 34 ms 71500 KB n=2000
69 Correct 34 ms 71444 KB n=2000
70 Correct 34 ms 71448 KB n=2000
71 Correct 35 ms 71372 KB n=2000
72 Correct 34 ms 71376 KB n=2000
73 Correct 35 ms 71380 KB n=2000
74 Correct 38 ms 71336 KB n=1844
75 Correct 41 ms 71340 KB n=2000
76 Correct 34 ms 71380 KB n=2000
77 Correct 36 ms 71372 KB n=2000
78 Correct 36 ms 71396 KB n=2000
79 Correct 34 ms 71372 KB n=2000
80 Correct 38 ms 71464 KB n=2000
81 Correct 41 ms 71412 KB n=2000
82 Correct 37 ms 71436 KB n=2000
83 Correct 34 ms 71452 KB n=2000
84 Correct 35 ms 71468 KB n=2000
85 Correct 35 ms 71404 KB n=2000
86 Correct 35 ms 71436 KB n=2000
87 Correct 34 ms 71372 KB n=2000
88 Correct 38 ms 71556 KB n=2000
89 Correct 34 ms 71500 KB n=2000
90 Correct 41 ms 71460 KB n=2000
91 Correct 38 ms 71448 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 31 ms 70740 KB n=5
2 Correct 33 ms 70888 KB n=100
3 Correct 33 ms 70740 KB n=100
4 Correct 31 ms 70740 KB n=100
5 Correct 32 ms 70704 KB n=100
6 Correct 35 ms 70732 KB n=100
7 Correct 33 ms 70860 KB n=100
8 Correct 31 ms 70740 KB n=100
9 Correct 33 ms 70796 KB n=100
10 Correct 35 ms 70800 KB n=100
11 Correct 32 ms 70804 KB n=100
12 Correct 32 ms 70800 KB n=100
13 Correct 33 ms 70772 KB n=100
14 Correct 32 ms 70740 KB n=100
15 Correct 33 ms 70736 KB n=100
16 Correct 32 ms 70740 KB n=100
17 Correct 34 ms 70708 KB n=100
18 Correct 33 ms 70732 KB n=100
19 Correct 32 ms 70740 KB n=100
20 Correct 32 ms 70816 KB n=100
21 Correct 37 ms 70740 KB n=100
22 Correct 35 ms 70700 KB n=100
23 Correct 38 ms 70808 KB n=100
24 Correct 38 ms 70784 KB n=100
25 Correct 32 ms 70700 KB n=100
26 Correct 32 ms 70684 KB n=12
27 Correct 33 ms 70768 KB n=100
28 Correct 32 ms 70928 KB n=500
29 Correct 35 ms 70860 KB n=500
30 Correct 42 ms 70864 KB n=500
31 Correct 35 ms 70960 KB n=500
32 Correct 34 ms 70988 KB n=500
33 Correct 34 ms 70936 KB n=500
34 Correct 34 ms 70944 KB n=500
35 Correct 35 ms 70904 KB n=500
36 Correct 34 ms 70920 KB n=500
37 Correct 33 ms 70860 KB n=500
38 Correct 33 ms 70868 KB n=500
39 Correct 33 ms 70924 KB n=500
40 Correct 36 ms 70924 KB n=500
41 Correct 35 ms 70940 KB n=500
42 Correct 34 ms 70924 KB n=500
43 Correct 34 ms 70852 KB n=500
44 Correct 33 ms 70880 KB n=500
45 Correct 33 ms 70908 KB n=500
46 Correct 37 ms 70860 KB n=500
47 Correct 39 ms 70916 KB n=500
48 Correct 33 ms 70948 KB n=500
49 Correct 38 ms 70960 KB n=500
50 Correct 33 ms 70860 KB n=500
51 Correct 32 ms 70960 KB n=500
52 Correct 33 ms 70900 KB n=500
53 Correct 35 ms 70864 KB n=500
54 Correct 38 ms 70896 KB n=500
55 Correct 32 ms 70800 KB n=278
56 Correct 33 ms 70908 KB n=500
57 Correct 34 ms 70860 KB n=500
58 Correct 33 ms 70868 KB n=500
59 Correct 37 ms 71436 KB n=2000
60 Correct 39 ms 71500 KB n=2000
61 Correct 35 ms 71496 KB n=2000
62 Correct 35 ms 71400 KB n=2000
63 Correct 35 ms 71436 KB n=2000
64 Correct 36 ms 71380 KB n=2000
65 Correct 37 ms 71368 KB n=2000
66 Correct 36 ms 71432 KB n=2000
67 Correct 36 ms 71436 KB n=2000
68 Correct 34 ms 71500 KB n=2000
69 Correct 34 ms 71444 KB n=2000
70 Correct 34 ms 71448 KB n=2000
71 Correct 35 ms 71372 KB n=2000
72 Correct 34 ms 71376 KB n=2000
73 Correct 35 ms 71380 KB n=2000
74 Correct 38 ms 71336 KB n=1844
75 Correct 41 ms 71340 KB n=2000
76 Correct 34 ms 71380 KB n=2000
77 Correct 36 ms 71372 KB n=2000
78 Correct 36 ms 71396 KB n=2000
79 Correct 34 ms 71372 KB n=2000
80 Correct 38 ms 71464 KB n=2000
81 Correct 41 ms 71412 KB n=2000
82 Correct 37 ms 71436 KB n=2000
83 Correct 34 ms 71452 KB n=2000
84 Correct 35 ms 71468 KB n=2000
85 Correct 35 ms 71404 KB n=2000
86 Correct 35 ms 71436 KB n=2000
87 Correct 34 ms 71372 KB n=2000
88 Correct 38 ms 71556 KB n=2000
89 Correct 34 ms 71500 KB n=2000
90 Correct 41 ms 71460 KB n=2000
91 Correct 38 ms 71448 KB n=2000
92 Correct 768 ms 138736 KB n=200000
93 Correct 699 ms 145180 KB n=200000
94 Correct 669 ms 149780 KB n=200000
95 Correct 702 ms 138484 KB n=200000
96 Correct 700 ms 138620 KB n=200000
97 Correct 720 ms 144072 KB n=200000
98 Correct 703 ms 138552 KB n=200000
99 Correct 769 ms 138684 KB n=200000
100 Correct 692 ms 138680 KB n=200000
101 Correct 666 ms 151344 KB n=200000
102 Correct 390 ms 139760 KB n=200000
103 Correct 395 ms 139852 KB n=200000
104 Correct 416 ms 139800 KB n=200000
105 Correct 428 ms 140088 KB n=200000
106 Correct 397 ms 140128 KB n=200000
107 Correct 398 ms 140116 KB n=200000
108 Correct 709 ms 138644 KB n=200000
109 Correct 682 ms 138592 KB n=200000
110 Correct 703 ms 138688 KB n=200000
111 Correct 694 ms 138196 KB n=200000
112 Correct 675 ms 149964 KB n=200000
113 Correct 719 ms 143856 KB n=200000
114 Correct 695 ms 138124 KB n=200000
115 Correct 727 ms 141100 KB n=200000
116 Correct 650 ms 138684 KB n=200000
117 Correct 666 ms 150452 KB n=200000
118 Correct 752 ms 142404 KB n=200000
119 Correct 685 ms 138612 KB n=200000
120 Correct 617 ms 150448 KB n=200000
121 Correct 620 ms 150324 KB n=200000
122 Correct 627 ms 150748 KB n=200000
123 Correct 422 ms 139868 KB n=200000
124 Correct 189 ms 88144 KB n=25264