Submission #525451

# Submission time Handle Problem Language Result Execution time Memory
525451 2022-02-11T16:24:00 Z s_samchenko Birthday gift (IZhO18_treearray) C++17
100 / 100
1592 ms 109252 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native") //bad
//#pragma GCC target("avx,avx2")
#define ll long long
#define ull unsigned ll
#define ff first
#define ss second
#define int ll
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb push_back
#define pii pair <int, int>
#define pdd pair <double, double>
#define vi vector <int>
using namespace std;
using namespace __gnu_pbds;
template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
const ll inf = 1e15;
constexpr ll mod = 1e9+7;
const int N = 3e5+100, B = 430;

vector < vector <int> > g, up;
vector <int> d;

void dfs(int v = 1, int pr = 1, int h = 0){
	d[v] = h;
	up[v][0] = pr;
	for (int j = 1; j <= 19; ++j) up[v][j] = up[up[v][j-1]][j-1];
	for (auto to : g[v]){
		if (to == pr) continue;
		dfs(to, v, h+1);
	}
}

int lca(int u, int v){
	if (d[v] < d[u]) swap(u, v);
	for (int j = 19, H = d[v] - d[u]; j >= 0 && H; --j){
		if (H < (1 << j)) continue;
		H -= (1 << j);
		v = up[v][j];
	}
	if (u == v) return u;
	for (int j = 19; j >= 0; --j){
		int v1 = up[v][j], u1 = up[u][j];
		if (u1 != v1) u = u1, v = v1;
	}
	return up[v][0];
}

void solve(){
	int n, m, q;
	cin >> n >> m >> q;
	g.resize(n+1);
	up.resize(n+1, vector <int> (20));
	d.resize(n+1);

	for (int i = 1; i < n; ++i){
		int a, b; cin >> a >> b;
		g[a].pb(b);
		g[b].pb(a);
	}
	dfs();
	vector <int> a(m+1);
	for (int i = 1; i <= m; ++i) cin >> a[i];
	
	vector <set <int> > one(n+1), s(n+1);
	for (int i = 1; i <= m; ++i){
		one[a[i]].insert(i);
		if (i+1 <= m){
			s[lca(a[i], a[i+1])].insert(i);
		}
	}
	
	while (q--){
		int type; cin >> type;
		if (type == 1){
			int pos, v; cin >> pos >> v;
			one[a[pos]].erase(pos);
			if (pos + 1 <= m) s[lca(a[pos], a[pos+1])].erase(pos);
			if (pos - 1 >= 0) s[lca(a[pos], a[pos-1])].erase(pos-1);

			a[pos] = v;
			
			one[a[pos]].insert(pos);
			if (pos + 1 <= m) s[lca(a[pos], a[pos+1])].insert(pos);
			if (pos - 1 >= 0) s[lca(a[pos], a[pos-1])].insert(pos-1);
			continue;
		}
		int l, r, v; cin >> l >> r >> v;
		auto it1 = one[v].lower_bound(l);
		if (it1 != one[v].end() && *it1 <= r){
			cout << *it1 << " " << *it1 << '\n';
			continue;
		}
		
		auto it = s[v].lower_bound(l);
		if (it != s[v].end() && *it + 1 <= r){
			cout << *it << " " << *it + 1 << '\n';
			continue;
		}
		cout << "-1 -1\n";
	}
}

signed main(){
	ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	int tt = 1;
	//cin >> tt;
	while (tt--){
		solve();
		cout << '\n';
	}
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 204 KB n=5
2 Correct 1 ms 304 KB n=100
3 Correct 1 ms 332 KB n=100
4 Correct 1 ms 332 KB n=100
5 Correct 2 ms 332 KB n=100
6 Correct 1 ms 332 KB n=100
7 Correct 1 ms 332 KB n=100
8 Correct 1 ms 332 KB n=100
9 Correct 1 ms 332 KB n=100
10 Correct 1 ms 344 KB n=100
11 Correct 2 ms 332 KB n=100
12 Correct 3 ms 312 KB n=100
13 Correct 1 ms 332 KB n=100
14 Correct 1 ms 336 KB n=100
15 Correct 1 ms 332 KB n=100
16 Correct 1 ms 332 KB n=100
17 Correct 1 ms 316 KB n=100
18 Correct 1 ms 332 KB n=100
19 Correct 1 ms 332 KB n=100
20 Correct 1 ms 332 KB n=100
21 Correct 1 ms 332 KB n=100
22 Correct 1 ms 332 KB n=100
23 Correct 2 ms 332 KB n=100
24 Correct 1 ms 336 KB n=100
25 Correct 1 ms 332 KB n=100
26 Correct 1 ms 204 KB n=12
27 Correct 1 ms 336 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 2 ms 204 KB n=5
2 Correct 1 ms 304 KB n=100
3 Correct 1 ms 332 KB n=100
4 Correct 1 ms 332 KB n=100
5 Correct 2 ms 332 KB n=100
6 Correct 1 ms 332 KB n=100
7 Correct 1 ms 332 KB n=100
8 Correct 1 ms 332 KB n=100
9 Correct 1 ms 332 KB n=100
10 Correct 1 ms 344 KB n=100
11 Correct 2 ms 332 KB n=100
12 Correct 3 ms 312 KB n=100
13 Correct 1 ms 332 KB n=100
14 Correct 1 ms 336 KB n=100
15 Correct 1 ms 332 KB n=100
16 Correct 1 ms 332 KB n=100
17 Correct 1 ms 316 KB n=100
18 Correct 1 ms 332 KB n=100
19 Correct 1 ms 332 KB n=100
20 Correct 1 ms 332 KB n=100
21 Correct 1 ms 332 KB n=100
22 Correct 1 ms 332 KB n=100
23 Correct 2 ms 332 KB n=100
24 Correct 1 ms 336 KB n=100
25 Correct 1 ms 332 KB n=100
26 Correct 1 ms 204 KB n=12
27 Correct 1 ms 336 KB n=100
28 Correct 1 ms 464 KB n=500
29 Correct 2 ms 460 KB n=500
30 Correct 1 ms 452 KB n=500
31 Correct 1 ms 460 KB n=500
32 Correct 2 ms 456 KB n=500
33 Correct 1 ms 464 KB n=500
34 Correct 1 ms 460 KB n=500
35 Correct 2 ms 468 KB n=500
36 Correct 1 ms 424 KB n=500
37 Correct 1 ms 460 KB n=500
38 Correct 2 ms 464 KB n=500
39 Correct 4 ms 460 KB n=500
40 Correct 1 ms 460 KB n=500
41 Correct 1 ms 460 KB n=500
42 Correct 1 ms 452 KB n=500
43 Correct 1 ms 460 KB n=500
44 Correct 3 ms 448 KB n=500
45 Correct 1 ms 432 KB n=500
46 Correct 1 ms 464 KB n=500
47 Correct 1 ms 456 KB n=500
48 Correct 1 ms 468 KB n=500
49 Correct 1 ms 468 KB n=500
50 Correct 2 ms 460 KB n=500
51 Correct 1 ms 460 KB n=500
52 Correct 1 ms 468 KB n=500
53 Correct 1 ms 460 KB n=500
54 Correct 2 ms 576 KB n=500
55 Correct 1 ms 460 KB n=278
56 Correct 1 ms 460 KB n=500
57 Correct 2 ms 460 KB n=500
58 Correct 1 ms 472 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 2 ms 204 KB n=5
2 Correct 1 ms 304 KB n=100
3 Correct 1 ms 332 KB n=100
4 Correct 1 ms 332 KB n=100
5 Correct 2 ms 332 KB n=100
6 Correct 1 ms 332 KB n=100
7 Correct 1 ms 332 KB n=100
8 Correct 1 ms 332 KB n=100
9 Correct 1 ms 332 KB n=100
10 Correct 1 ms 344 KB n=100
11 Correct 2 ms 332 KB n=100
12 Correct 3 ms 312 KB n=100
13 Correct 1 ms 332 KB n=100
14 Correct 1 ms 336 KB n=100
15 Correct 1 ms 332 KB n=100
16 Correct 1 ms 332 KB n=100
17 Correct 1 ms 316 KB n=100
18 Correct 1 ms 332 KB n=100
19 Correct 1 ms 332 KB n=100
20 Correct 1 ms 332 KB n=100
21 Correct 1 ms 332 KB n=100
22 Correct 1 ms 332 KB n=100
23 Correct 2 ms 332 KB n=100
24 Correct 1 ms 336 KB n=100
25 Correct 1 ms 332 KB n=100
26 Correct 1 ms 204 KB n=12
27 Correct 1 ms 336 KB n=100
28 Correct 1 ms 464 KB n=500
29 Correct 2 ms 460 KB n=500
30 Correct 1 ms 452 KB n=500
31 Correct 1 ms 460 KB n=500
32 Correct 2 ms 456 KB n=500
33 Correct 1 ms 464 KB n=500
34 Correct 1 ms 460 KB n=500
35 Correct 2 ms 468 KB n=500
36 Correct 1 ms 424 KB n=500
37 Correct 1 ms 460 KB n=500
38 Correct 2 ms 464 KB n=500
39 Correct 4 ms 460 KB n=500
40 Correct 1 ms 460 KB n=500
41 Correct 1 ms 460 KB n=500
42 Correct 1 ms 452 KB n=500
43 Correct 1 ms 460 KB n=500
44 Correct 3 ms 448 KB n=500
45 Correct 1 ms 432 KB n=500
46 Correct 1 ms 464 KB n=500
47 Correct 1 ms 456 KB n=500
48 Correct 1 ms 468 KB n=500
49 Correct 1 ms 468 KB n=500
50 Correct 2 ms 460 KB n=500
51 Correct 1 ms 460 KB n=500
52 Correct 1 ms 468 KB n=500
53 Correct 1 ms 460 KB n=500
54 Correct 2 ms 576 KB n=500
55 Correct 1 ms 460 KB n=278
56 Correct 1 ms 460 KB n=500
57 Correct 2 ms 460 KB n=500
58 Correct 1 ms 472 KB n=500
59 Correct 5 ms 1228 KB n=2000
60 Correct 4 ms 1356 KB n=2000
61 Correct 5 ms 1228 KB n=2000
62 Correct 4 ms 1228 KB n=2000
63 Correct 4 ms 1228 KB n=2000
64 Correct 5 ms 1220 KB n=2000
65 Correct 4 ms 1248 KB n=2000
66 Correct 4 ms 1356 KB n=2000
67 Correct 4 ms 1228 KB n=2000
68 Correct 4 ms 1196 KB n=2000
69 Correct 4 ms 1232 KB n=2000
70 Correct 3 ms 1228 KB n=2000
71 Correct 3 ms 1216 KB n=2000
72 Correct 3 ms 1224 KB n=2000
73 Correct 3 ms 1228 KB n=2000
74 Correct 3 ms 1220 KB n=1844
75 Correct 5 ms 1220 KB n=2000
76 Correct 7 ms 1272 KB n=2000
77 Correct 16 ms 1228 KB n=2000
78 Correct 5 ms 1232 KB n=2000
79 Correct 4 ms 1228 KB n=2000
80 Correct 4 ms 1352 KB n=2000
81 Correct 4 ms 1228 KB n=2000
82 Correct 4 ms 1244 KB n=2000
83 Correct 4 ms 1228 KB n=2000
84 Correct 4 ms 1228 KB n=2000
85 Correct 15 ms 1224 KB n=2000
86 Correct 6 ms 1240 KB n=2000
87 Correct 5 ms 1220 KB n=2000
88 Correct 4 ms 1344 KB n=2000
89 Correct 4 ms 1340 KB n=2000
90 Correct 3 ms 1356 KB n=2000
91 Correct 4 ms 1228 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 2 ms 204 KB n=5
2 Correct 1 ms 304 KB n=100
3 Correct 1 ms 332 KB n=100
4 Correct 1 ms 332 KB n=100
5 Correct 2 ms 332 KB n=100
6 Correct 1 ms 332 KB n=100
7 Correct 1 ms 332 KB n=100
8 Correct 1 ms 332 KB n=100
9 Correct 1 ms 332 KB n=100
10 Correct 1 ms 344 KB n=100
11 Correct 2 ms 332 KB n=100
12 Correct 3 ms 312 KB n=100
13 Correct 1 ms 332 KB n=100
14 Correct 1 ms 336 KB n=100
15 Correct 1 ms 332 KB n=100
16 Correct 1 ms 332 KB n=100
17 Correct 1 ms 316 KB n=100
18 Correct 1 ms 332 KB n=100
19 Correct 1 ms 332 KB n=100
20 Correct 1 ms 332 KB n=100
21 Correct 1 ms 332 KB n=100
22 Correct 1 ms 332 KB n=100
23 Correct 2 ms 332 KB n=100
24 Correct 1 ms 336 KB n=100
25 Correct 1 ms 332 KB n=100
26 Correct 1 ms 204 KB n=12
27 Correct 1 ms 336 KB n=100
28 Correct 1 ms 464 KB n=500
29 Correct 2 ms 460 KB n=500
30 Correct 1 ms 452 KB n=500
31 Correct 1 ms 460 KB n=500
32 Correct 2 ms 456 KB n=500
33 Correct 1 ms 464 KB n=500
34 Correct 1 ms 460 KB n=500
35 Correct 2 ms 468 KB n=500
36 Correct 1 ms 424 KB n=500
37 Correct 1 ms 460 KB n=500
38 Correct 2 ms 464 KB n=500
39 Correct 4 ms 460 KB n=500
40 Correct 1 ms 460 KB n=500
41 Correct 1 ms 460 KB n=500
42 Correct 1 ms 452 KB n=500
43 Correct 1 ms 460 KB n=500
44 Correct 3 ms 448 KB n=500
45 Correct 1 ms 432 KB n=500
46 Correct 1 ms 464 KB n=500
47 Correct 1 ms 456 KB n=500
48 Correct 1 ms 468 KB n=500
49 Correct 1 ms 468 KB n=500
50 Correct 2 ms 460 KB n=500
51 Correct 1 ms 460 KB n=500
52 Correct 1 ms 468 KB n=500
53 Correct 1 ms 460 KB n=500
54 Correct 2 ms 576 KB n=500
55 Correct 1 ms 460 KB n=278
56 Correct 1 ms 460 KB n=500
57 Correct 2 ms 460 KB n=500
58 Correct 1 ms 472 KB n=500
59 Correct 5 ms 1228 KB n=2000
60 Correct 4 ms 1356 KB n=2000
61 Correct 5 ms 1228 KB n=2000
62 Correct 4 ms 1228 KB n=2000
63 Correct 4 ms 1228 KB n=2000
64 Correct 5 ms 1220 KB n=2000
65 Correct 4 ms 1248 KB n=2000
66 Correct 4 ms 1356 KB n=2000
67 Correct 4 ms 1228 KB n=2000
68 Correct 4 ms 1196 KB n=2000
69 Correct 4 ms 1232 KB n=2000
70 Correct 3 ms 1228 KB n=2000
71 Correct 3 ms 1216 KB n=2000
72 Correct 3 ms 1224 KB n=2000
73 Correct 3 ms 1228 KB n=2000
74 Correct 3 ms 1220 KB n=1844
75 Correct 5 ms 1220 KB n=2000
76 Correct 7 ms 1272 KB n=2000
77 Correct 16 ms 1228 KB n=2000
78 Correct 5 ms 1232 KB n=2000
79 Correct 4 ms 1228 KB n=2000
80 Correct 4 ms 1352 KB n=2000
81 Correct 4 ms 1228 KB n=2000
82 Correct 4 ms 1244 KB n=2000
83 Correct 4 ms 1228 KB n=2000
84 Correct 4 ms 1228 KB n=2000
85 Correct 15 ms 1224 KB n=2000
86 Correct 6 ms 1240 KB n=2000
87 Correct 5 ms 1220 KB n=2000
88 Correct 4 ms 1344 KB n=2000
89 Correct 4 ms 1340 KB n=2000
90 Correct 3 ms 1356 KB n=2000
91 Correct 4 ms 1228 KB n=2000
92 Correct 826 ms 100364 KB n=200000
93 Correct 1575 ms 105316 KB n=200000
94 Correct 1434 ms 108192 KB n=200000
95 Correct 795 ms 100152 KB n=200000
96 Correct 798 ms 100260 KB n=200000
97 Correct 1592 ms 104380 KB n=200000
98 Correct 776 ms 100132 KB n=200000
99 Correct 1199 ms 100588 KB n=200000
100 Correct 824 ms 100192 KB n=200000
101 Correct 1564 ms 109252 KB n=200000
102 Correct 585 ms 101548 KB n=200000
103 Correct 556 ms 101584 KB n=200000
104 Correct 574 ms 101592 KB n=200000
105 Correct 601 ms 102044 KB n=200000
106 Correct 695 ms 102196 KB n=200000
107 Correct 570 ms 101984 KB n=200000
108 Correct 1079 ms 100424 KB n=200000
109 Correct 1097 ms 100524 KB n=200000
110 Correct 1025 ms 100516 KB n=200000
111 Correct 844 ms 99680 KB n=200000
112 Correct 1361 ms 108436 KB n=200000
113 Correct 1423 ms 104488 KB n=200000
114 Correct 714 ms 99660 KB n=200000
115 Correct 1589 ms 102412 KB n=200000
116 Correct 718 ms 100340 KB n=200000
117 Correct 1343 ms 108692 KB n=200000
118 Correct 1433 ms 103276 KB n=200000
119 Correct 671 ms 100364 KB n=200000
120 Correct 1219 ms 108084 KB n=200000
121 Correct 1229 ms 108104 KB n=200000
122 Correct 1221 ms 108484 KB n=200000
123 Correct 549 ms 101808 KB n=200000
124 Correct 244 ms 21592 KB n=25264