답안 #865578

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
865578 2023-10-24T11:09:50 Z vjudge1 Birthday gift (IZhO18_treearray) C++17
100 / 100
867 ms 103528 KB
//order_of_key(k): Number of items strictly smaller than k .
//find_by_order(k): K-th element in a set (counting from zero).

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define file(s) if(fopen(s".in","r")) freopen(s".in","r",stdin);freopen(s".out","w",stdout)
#define all(x) (x).begin(), (x).end()
#define len(x) (int)x.size()
#define tm (tl + tr >> 1)
#define ls v << 1, tl, tm
#define rs v << 1 | 1, tm + 1, tr
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define elif else if
#define F first
#define S second
#define int long long

using namespace std;
using namespace __gnu_pbds;

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef unsigned long long ull;
typedef long long ll;
typedef double db;
typedef long double ld;

const int MOD = 1e9 + 7;
const int N = 2e5 + 7;
const int P = 911;
const ll INF = 1e18;

int rnd() {
	int x = rand() << 15;
	return x ^ rand();
}

int tin[N], tout[N], up[N][19], t[N << 2], a[N], temp, m;
vector <int> g[N];

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

bool upper(int v, int u) {
	return tin[v] <= tin[u] && tout[v] >= tout[u];
}

int lca(int v, int u) {
	if (v && (!u || upper(v, u))) return v;
	if (u && (!v || upper(u, v))) return u;
	for (int i = 18; i >= 0; i--) {
		if (!up[v][i]) continue;
		if (!upper(up[v][i], u)) v = up[v][i];
	}
	return up[v][0];
}

set <int> pos[N], ext[N];

void GazizMadi() {
	int n, q;
	cin >> n >> m >> q;
	for (int i = 1, v, u; i < n; i++) {
		cin >> v >> u;
		g[v].pb(u);
		g[u].pb(v);
	}
	dfs(1, 0);
	for (int i = 1; i <= m; i++) {
		cin >> a[i];
		pos[a[i]].insert(i);
		if (i > 1) {
			ext[lca(a[i - 1], a[i])].insert(i - 1);
		}
	}
	while (q--) {
		int ty;
		cin >> ty;
		if (ty == 1) {
			int i, val;
			cin >> i >> val;
			if (i < m) ext[lca(a[i], a[i + 1])].erase(i);
			if (i > 1) ext[lca(a[i - 1], a[i])].erase(i - 1);
			pos[a[i]].erase(i);
			a[i] = val;
			pos[a[i]].insert(i);
			if (i < m) ext[lca(a[i], a[i + 1])].insert(i);
			if (i > 1) ext[lca(a[i - 1], a[i])].insert(i - 1);
		} else {
			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 << '\n';
			} elif (ext[v].lower_bound(l) != ext[v].end() && (*ext[v].lower_bound(l)) < r) {
				int x = *(ext[v].lower_bound(l));
				cout << x << ' ' << x + 1 << '\n';
			} else {
				cout << -1 << ' ' << -1 << '\n';
			}
		}
	}
	
}

const bool Cases = 0;

signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	srand(time(0));
	int TT = 1;
	if (Cases) cin >> TT;
	for (int i = 1; i <= TT; i++) {
		//cout << "Case " << i << ": ";
		GazizMadi();
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 31320 KB n=5
2 Correct 6 ms 31324 KB n=100
3 Correct 6 ms 31324 KB n=100
4 Correct 6 ms 31324 KB n=100
5 Correct 6 ms 31324 KB n=100
6 Correct 6 ms 31324 KB n=100
7 Correct 6 ms 31324 KB n=100
8 Correct 6 ms 31324 KB n=100
9 Correct 6 ms 31176 KB n=100
10 Correct 6 ms 31324 KB n=100
11 Correct 6 ms 31324 KB n=100
12 Correct 6 ms 31324 KB n=100
13 Correct 6 ms 31324 KB n=100
14 Correct 6 ms 31360 KB n=100
15 Correct 6 ms 31320 KB n=100
16 Correct 6 ms 31324 KB n=100
17 Correct 6 ms 31324 KB n=100
18 Correct 6 ms 31324 KB n=100
19 Correct 7 ms 31324 KB n=100
20 Correct 6 ms 31420 KB n=100
21 Correct 7 ms 31424 KB n=100
22 Correct 7 ms 31320 KB n=100
23 Correct 7 ms 31324 KB n=100
24 Correct 6 ms 31392 KB n=100
25 Correct 7 ms 31320 KB n=100
26 Correct 6 ms 31324 KB n=12
27 Correct 6 ms 31324 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 31320 KB n=5
2 Correct 6 ms 31324 KB n=100
3 Correct 6 ms 31324 KB n=100
4 Correct 6 ms 31324 KB n=100
5 Correct 6 ms 31324 KB n=100
6 Correct 6 ms 31324 KB n=100
7 Correct 6 ms 31324 KB n=100
8 Correct 6 ms 31324 KB n=100
9 Correct 6 ms 31176 KB n=100
10 Correct 6 ms 31324 KB n=100
11 Correct 6 ms 31324 KB n=100
12 Correct 6 ms 31324 KB n=100
13 Correct 6 ms 31324 KB n=100
14 Correct 6 ms 31360 KB n=100
15 Correct 6 ms 31320 KB n=100
16 Correct 6 ms 31324 KB n=100
17 Correct 6 ms 31324 KB n=100
18 Correct 6 ms 31324 KB n=100
19 Correct 7 ms 31324 KB n=100
20 Correct 6 ms 31420 KB n=100
21 Correct 7 ms 31424 KB n=100
22 Correct 7 ms 31320 KB n=100
23 Correct 7 ms 31324 KB n=100
24 Correct 6 ms 31392 KB n=100
25 Correct 7 ms 31320 KB n=100
26 Correct 6 ms 31324 KB n=12
27 Correct 6 ms 31324 KB n=100
28 Correct 7 ms 31320 KB n=500
29 Correct 7 ms 31320 KB n=500
30 Correct 7 ms 31324 KB n=500
31 Correct 7 ms 31324 KB n=500
32 Correct 7 ms 31324 KB n=500
33 Correct 7 ms 31448 KB n=500
34 Correct 7 ms 31324 KB n=500
35 Correct 7 ms 31324 KB n=500
36 Correct 8 ms 31580 KB n=500
37 Correct 7 ms 31320 KB n=500
38 Correct 7 ms 31324 KB n=500
39 Correct 7 ms 31324 KB n=500
40 Correct 7 ms 31324 KB n=500
41 Correct 7 ms 31320 KB n=500
42 Correct 7 ms 31320 KB n=500
43 Correct 7 ms 31324 KB n=500
44 Correct 7 ms 31324 KB n=500
45 Correct 7 ms 31324 KB n=500
46 Correct 7 ms 31320 KB n=500
47 Correct 7 ms 31324 KB n=500
48 Correct 7 ms 31324 KB n=500
49 Correct 7 ms 31320 KB n=500
50 Correct 7 ms 31324 KB n=500
51 Correct 7 ms 31324 KB n=500
52 Correct 7 ms 31320 KB n=500
53 Correct 7 ms 31324 KB n=500
54 Correct 7 ms 31324 KB n=500
55 Correct 7 ms 31320 KB n=278
56 Correct 7 ms 31324 KB n=500
57 Correct 7 ms 31324 KB n=500
58 Correct 7 ms 31324 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 31320 KB n=5
2 Correct 6 ms 31324 KB n=100
3 Correct 6 ms 31324 KB n=100
4 Correct 6 ms 31324 KB n=100
5 Correct 6 ms 31324 KB n=100
6 Correct 6 ms 31324 KB n=100
7 Correct 6 ms 31324 KB n=100
8 Correct 6 ms 31324 KB n=100
9 Correct 6 ms 31176 KB n=100
10 Correct 6 ms 31324 KB n=100
11 Correct 6 ms 31324 KB n=100
12 Correct 6 ms 31324 KB n=100
13 Correct 6 ms 31324 KB n=100
14 Correct 6 ms 31360 KB n=100
15 Correct 6 ms 31320 KB n=100
16 Correct 6 ms 31324 KB n=100
17 Correct 6 ms 31324 KB n=100
18 Correct 6 ms 31324 KB n=100
19 Correct 7 ms 31324 KB n=100
20 Correct 6 ms 31420 KB n=100
21 Correct 7 ms 31424 KB n=100
22 Correct 7 ms 31320 KB n=100
23 Correct 7 ms 31324 KB n=100
24 Correct 6 ms 31392 KB n=100
25 Correct 7 ms 31320 KB n=100
26 Correct 6 ms 31324 KB n=12
27 Correct 6 ms 31324 KB n=100
28 Correct 7 ms 31320 KB n=500
29 Correct 7 ms 31320 KB n=500
30 Correct 7 ms 31324 KB n=500
31 Correct 7 ms 31324 KB n=500
32 Correct 7 ms 31324 KB n=500
33 Correct 7 ms 31448 KB n=500
34 Correct 7 ms 31324 KB n=500
35 Correct 7 ms 31324 KB n=500
36 Correct 8 ms 31580 KB n=500
37 Correct 7 ms 31320 KB n=500
38 Correct 7 ms 31324 KB n=500
39 Correct 7 ms 31324 KB n=500
40 Correct 7 ms 31324 KB n=500
41 Correct 7 ms 31320 KB n=500
42 Correct 7 ms 31320 KB n=500
43 Correct 7 ms 31324 KB n=500
44 Correct 7 ms 31324 KB n=500
45 Correct 7 ms 31324 KB n=500
46 Correct 7 ms 31320 KB n=500
47 Correct 7 ms 31324 KB n=500
48 Correct 7 ms 31324 KB n=500
49 Correct 7 ms 31320 KB n=500
50 Correct 7 ms 31324 KB n=500
51 Correct 7 ms 31324 KB n=500
52 Correct 7 ms 31320 KB n=500
53 Correct 7 ms 31324 KB n=500
54 Correct 7 ms 31324 KB n=500
55 Correct 7 ms 31320 KB n=278
56 Correct 7 ms 31324 KB n=500
57 Correct 7 ms 31324 KB n=500
58 Correct 7 ms 31324 KB n=500
59 Correct 9 ms 31580 KB n=2000
60 Correct 8 ms 31580 KB n=2000
61 Correct 8 ms 31576 KB n=2000
62 Correct 10 ms 32300 KB n=2000
63 Correct 8 ms 31736 KB n=2000
64 Correct 8 ms 31576 KB n=2000
65 Correct 9 ms 31580 KB n=2000
66 Correct 8 ms 31580 KB n=2000
67 Correct 9 ms 31576 KB n=2000
68 Correct 9 ms 31576 KB n=2000
69 Correct 10 ms 31836 KB n=2000
70 Correct 8 ms 31576 KB n=2000
71 Correct 9 ms 31580 KB n=2000
72 Correct 8 ms 31576 KB n=2000
73 Correct 8 ms 31580 KB n=2000
74 Correct 8 ms 31576 KB n=1844
75 Correct 8 ms 31580 KB n=2000
76 Correct 9 ms 31580 KB n=2000
77 Correct 9 ms 31580 KB n=2000
78 Correct 9 ms 31580 KB n=2000
79 Correct 10 ms 31580 KB n=2000
80 Correct 8 ms 31580 KB n=2000
81 Correct 9 ms 31576 KB n=2000
82 Correct 9 ms 31580 KB n=2000
83 Correct 9 ms 31576 KB n=2000
84 Correct 9 ms 31580 KB n=2000
85 Correct 9 ms 31580 KB n=2000
86 Correct 8 ms 31580 KB n=2000
87 Correct 9 ms 31576 KB n=2000
88 Correct 8 ms 31576 KB n=2000
89 Correct 10 ms 31580 KB n=2000
90 Correct 8 ms 31580 KB n=2000
91 Correct 8 ms 31580 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 31320 KB n=5
2 Correct 6 ms 31324 KB n=100
3 Correct 6 ms 31324 KB n=100
4 Correct 6 ms 31324 KB n=100
5 Correct 6 ms 31324 KB n=100
6 Correct 6 ms 31324 KB n=100
7 Correct 6 ms 31324 KB n=100
8 Correct 6 ms 31324 KB n=100
9 Correct 6 ms 31176 KB n=100
10 Correct 6 ms 31324 KB n=100
11 Correct 6 ms 31324 KB n=100
12 Correct 6 ms 31324 KB n=100
13 Correct 6 ms 31324 KB n=100
14 Correct 6 ms 31360 KB n=100
15 Correct 6 ms 31320 KB n=100
16 Correct 6 ms 31324 KB n=100
17 Correct 6 ms 31324 KB n=100
18 Correct 6 ms 31324 KB n=100
19 Correct 7 ms 31324 KB n=100
20 Correct 6 ms 31420 KB n=100
21 Correct 7 ms 31424 KB n=100
22 Correct 7 ms 31320 KB n=100
23 Correct 7 ms 31324 KB n=100
24 Correct 6 ms 31392 KB n=100
25 Correct 7 ms 31320 KB n=100
26 Correct 6 ms 31324 KB n=12
27 Correct 6 ms 31324 KB n=100
28 Correct 7 ms 31320 KB n=500
29 Correct 7 ms 31320 KB n=500
30 Correct 7 ms 31324 KB n=500
31 Correct 7 ms 31324 KB n=500
32 Correct 7 ms 31324 KB n=500
33 Correct 7 ms 31448 KB n=500
34 Correct 7 ms 31324 KB n=500
35 Correct 7 ms 31324 KB n=500
36 Correct 8 ms 31580 KB n=500
37 Correct 7 ms 31320 KB n=500
38 Correct 7 ms 31324 KB n=500
39 Correct 7 ms 31324 KB n=500
40 Correct 7 ms 31324 KB n=500
41 Correct 7 ms 31320 KB n=500
42 Correct 7 ms 31320 KB n=500
43 Correct 7 ms 31324 KB n=500
44 Correct 7 ms 31324 KB n=500
45 Correct 7 ms 31324 KB n=500
46 Correct 7 ms 31320 KB n=500
47 Correct 7 ms 31324 KB n=500
48 Correct 7 ms 31324 KB n=500
49 Correct 7 ms 31320 KB n=500
50 Correct 7 ms 31324 KB n=500
51 Correct 7 ms 31324 KB n=500
52 Correct 7 ms 31320 KB n=500
53 Correct 7 ms 31324 KB n=500
54 Correct 7 ms 31324 KB n=500
55 Correct 7 ms 31320 KB n=278
56 Correct 7 ms 31324 KB n=500
57 Correct 7 ms 31324 KB n=500
58 Correct 7 ms 31324 KB n=500
59 Correct 9 ms 31580 KB n=2000
60 Correct 8 ms 31580 KB n=2000
61 Correct 8 ms 31576 KB n=2000
62 Correct 10 ms 32300 KB n=2000
63 Correct 8 ms 31736 KB n=2000
64 Correct 8 ms 31576 KB n=2000
65 Correct 9 ms 31580 KB n=2000
66 Correct 8 ms 31580 KB n=2000
67 Correct 9 ms 31576 KB n=2000
68 Correct 9 ms 31576 KB n=2000
69 Correct 10 ms 31836 KB n=2000
70 Correct 8 ms 31576 KB n=2000
71 Correct 9 ms 31580 KB n=2000
72 Correct 8 ms 31576 KB n=2000
73 Correct 8 ms 31580 KB n=2000
74 Correct 8 ms 31576 KB n=1844
75 Correct 8 ms 31580 KB n=2000
76 Correct 9 ms 31580 KB n=2000
77 Correct 9 ms 31580 KB n=2000
78 Correct 9 ms 31580 KB n=2000
79 Correct 10 ms 31580 KB n=2000
80 Correct 8 ms 31580 KB n=2000
81 Correct 9 ms 31576 KB n=2000
82 Correct 9 ms 31580 KB n=2000
83 Correct 9 ms 31576 KB n=2000
84 Correct 9 ms 31580 KB n=2000
85 Correct 9 ms 31580 KB n=2000
86 Correct 8 ms 31580 KB n=2000
87 Correct 9 ms 31576 KB n=2000
88 Correct 8 ms 31576 KB n=2000
89 Correct 10 ms 31580 KB n=2000
90 Correct 8 ms 31580 KB n=2000
91 Correct 8 ms 31580 KB n=2000
92 Correct 566 ms 93140 KB n=200000
93 Correct 596 ms 99224 KB n=200000
94 Correct 415 ms 102684 KB n=200000
95 Correct 495 ms 94656 KB n=200000
96 Correct 492 ms 94680 KB n=200000
97 Correct 679 ms 98784 KB n=200000
98 Correct 500 ms 94656 KB n=200000
99 Correct 573 ms 94916 KB n=200000
100 Correct 497 ms 94748 KB n=200000
101 Correct 364 ms 103528 KB n=200000
102 Correct 286 ms 96084 KB n=200000
103 Correct 296 ms 95944 KB n=200000
104 Correct 286 ms 96080 KB n=200000
105 Correct 302 ms 96492 KB n=200000
106 Correct 313 ms 96340 KB n=200000
107 Correct 319 ms 96236 KB n=200000
108 Correct 598 ms 94736 KB n=200000
109 Correct 568 ms 94796 KB n=200000
110 Correct 556 ms 94948 KB n=200000
111 Correct 553 ms 94128 KB n=200000
112 Correct 449 ms 102736 KB n=200000
113 Correct 659 ms 98852 KB n=200000
114 Correct 584 ms 94140 KB n=200000
115 Correct 867 ms 96848 KB n=200000
116 Correct 543 ms 94832 KB n=200000
117 Correct 489 ms 102996 KB n=200000
118 Correct 803 ms 97656 KB n=200000
119 Correct 511 ms 94676 KB n=200000
120 Correct 353 ms 102516 KB n=200000
121 Correct 351 ms 102736 KB n=200000
122 Correct 358 ms 102720 KB n=200000
123 Correct 340 ms 96084 KB n=200000
124 Correct 93 ms 47896 KB n=25264