답안 #868435

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
868435 2023-10-31T13:24:49 Z vjudge1 Birthday gift (IZhO18_treearray) C++17
100 / 100
632 ms 87424 KB
//Bismillahir-Rahmanir-Rahim
#include <bits/stdc++.h>
         
using namespace std;
      
#define flash ios_base::sync_with_stdio(0),cin.tie(0)
#define pb push_back
#define ll long long
#define ld long double
#define dbg(x) cerr << #x << " = " << x << "\n";
#define ff first
#define ss second
 
 /*     
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma comment (linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
*/ 
 
const long long INF = 1e9 + 7;
const long long MOD = 1e9 + 7;
const int maxn = 2e5 + 10;
const int lg = 22;

int n, m, q, a[maxn], upp[maxn][lg + 1], tin[maxn], tout[maxn], timer;
vector <int> g[maxn];
set <int> s[maxn];
set <int> s2[maxn];

void dfs(int v, int p) {
	upp[v][0] = p;
	tin[v] = ++timer;
	for (int i = 1; i <= lg; i++) {
		upp[v][i] = upp[upp[v][i - 1]][i - 1];
	}
	for (auto to: g[v]) {
		if (to != p) {
			dfs(to, v);
		}
	} 
	tout[v] = timer;
}

bool check(int a, int b) {
	if (tin[a] <= tin[b] && tout[a] >= tout[b]) {
		return 1;
	}
	return 0;
}

int lca(int a, int b) {
	if (check(a, b)) {
		return a;
	}
	if (check(b, a)) {
		return b;
	}
	for (int i = lg; i >= 0; i--) {
		if (!check(upp[a][i], b) && upp[a][i] != 0) {
			a = upp[a][i];
		}
	}
	return upp[a][0];
}

void press_F_() {
	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];
	}
	dfs(1, 0);
	for (int i = 1; i <= m; i++) {
		s[a[i]].insert(i);
	}
	for (int i = 1; i < m; i++) {
		s2[lca(a[i], a[i + 1])].insert(i);
	}
	while(q--) {
		int tp;
		cin >> tp;
		if (tp == 1) {
			int pos, x;
			cin >> pos >> x;
			s[a[pos]].erase(pos);
			if (pos < m) {
				s2[lca(a[pos], a[pos + 1])].erase(pos);
			}
			if (pos > 1) {
				s2[lca(a[pos - 1], a[pos])].erase(pos - 1);
			}			
			a[pos] = x;
			s[a[pos]].insert(pos);
			if (pos < m) {
				s2[lca(a[pos], a[pos + 1])].insert(pos);
			}
			if (pos > 1) {
				s2[lca(a[pos - 1], a[pos])].insert(pos - 1);
			}
		} else {
			int l, r, x;
			cin >> l >> r >> x;
			if (s[x].lower_bound(l) != s[x].end() && (*s[x].lower_bound(l)) <= r) {
				int p = (*s[x].lower_bound(l));
				cout << p << " " << p << '\n';
				continue;
			}
			if (s2[x].lower_bound(l) != s2[x].end() && (*s2[x].lower_bound(l)) < r) {
				int p = (*s2[x].lower_bound(l));
				cout << p << " " << p + 1 << '\n';
				continue;
			}
			cout << -1 << " " << -1 << '\n';
		}
	}
} 

/*
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
*/
int main() {
    flash;
    // srand(time(0));
    int T = 1;
    // cin >> T;
    for (int i = 1; i <= T; i++) {
        // cout << "Case" << " " << i << ':' << "\n";
        press_F_();
    }
    //Respa gold 2023-2024 InshAllah
    return 0;
}
/*
Maybe not today and tomorrow, but InshAllah one day I will reach expert
*/
// g++ -std=c++17 main.cpp
// ./a.out
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 29276 KB n=5
2 Correct 5 ms 29276 KB n=100
3 Correct 5 ms 29528 KB n=100
4 Correct 5 ms 29276 KB n=100
5 Correct 5 ms 29276 KB n=100
6 Correct 5 ms 29276 KB n=100
7 Correct 6 ms 29276 KB n=100
8 Correct 5 ms 29276 KB n=100
9 Correct 5 ms 29272 KB n=100
10 Correct 6 ms 29276 KB n=100
11 Correct 6 ms 29124 KB n=100
12 Correct 5 ms 29276 KB n=100
13 Correct 5 ms 29276 KB n=100
14 Correct 6 ms 29528 KB n=100
15 Correct 6 ms 29276 KB n=100
16 Correct 6 ms 29276 KB n=100
17 Correct 5 ms 29272 KB n=100
18 Correct 5 ms 29276 KB n=100
19 Correct 5 ms 29276 KB n=100
20 Correct 5 ms 29276 KB n=100
21 Correct 6 ms 29276 KB n=100
22 Correct 5 ms 29276 KB n=100
23 Correct 5 ms 29372 KB n=100
24 Correct 5 ms 29276 KB n=100
25 Correct 5 ms 29276 KB n=100
26 Correct 6 ms 29276 KB n=12
27 Correct 6 ms 29276 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 29276 KB n=5
2 Correct 5 ms 29276 KB n=100
3 Correct 5 ms 29528 KB n=100
4 Correct 5 ms 29276 KB n=100
5 Correct 5 ms 29276 KB n=100
6 Correct 5 ms 29276 KB n=100
7 Correct 6 ms 29276 KB n=100
8 Correct 5 ms 29276 KB n=100
9 Correct 5 ms 29272 KB n=100
10 Correct 6 ms 29276 KB n=100
11 Correct 6 ms 29124 KB n=100
12 Correct 5 ms 29276 KB n=100
13 Correct 5 ms 29276 KB n=100
14 Correct 6 ms 29528 KB n=100
15 Correct 6 ms 29276 KB n=100
16 Correct 6 ms 29276 KB n=100
17 Correct 5 ms 29272 KB n=100
18 Correct 5 ms 29276 KB n=100
19 Correct 5 ms 29276 KB n=100
20 Correct 5 ms 29276 KB n=100
21 Correct 6 ms 29276 KB n=100
22 Correct 5 ms 29276 KB n=100
23 Correct 5 ms 29372 KB n=100
24 Correct 5 ms 29276 KB n=100
25 Correct 5 ms 29276 KB n=100
26 Correct 6 ms 29276 KB n=12
27 Correct 6 ms 29276 KB n=100
28 Correct 6 ms 29272 KB n=500
29 Correct 7 ms 29392 KB n=500
30 Correct 6 ms 29276 KB n=500
31 Correct 6 ms 29528 KB n=500
32 Correct 6 ms 29472 KB n=500
33 Correct 6 ms 29276 KB n=500
34 Correct 7 ms 29276 KB n=500
35 Correct 6 ms 29452 KB n=500
36 Correct 6 ms 29308 KB n=500
37 Correct 6 ms 29276 KB n=500
38 Correct 6 ms 29276 KB n=500
39 Correct 6 ms 29528 KB n=500
40 Correct 6 ms 29276 KB n=500
41 Correct 6 ms 29276 KB n=500
42 Correct 6 ms 29276 KB n=500
43 Correct 6 ms 29276 KB n=500
44 Correct 6 ms 29276 KB n=500
45 Correct 6 ms 29276 KB n=500
46 Correct 6 ms 29272 KB n=500
47 Correct 6 ms 29276 KB n=500
48 Correct 6 ms 29276 KB n=500
49 Correct 6 ms 29272 KB n=500
50 Correct 6 ms 29276 KB n=500
51 Correct 6 ms 29368 KB n=500
52 Correct 6 ms 29272 KB n=500
53 Correct 6 ms 29276 KB n=500
54 Correct 6 ms 29272 KB n=500
55 Correct 6 ms 29272 KB n=278
56 Correct 6 ms 29276 KB n=500
57 Correct 6 ms 29532 KB n=500
58 Correct 6 ms 29276 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 29276 KB n=5
2 Correct 5 ms 29276 KB n=100
3 Correct 5 ms 29528 KB n=100
4 Correct 5 ms 29276 KB n=100
5 Correct 5 ms 29276 KB n=100
6 Correct 5 ms 29276 KB n=100
7 Correct 6 ms 29276 KB n=100
8 Correct 5 ms 29276 KB n=100
9 Correct 5 ms 29272 KB n=100
10 Correct 6 ms 29276 KB n=100
11 Correct 6 ms 29124 KB n=100
12 Correct 5 ms 29276 KB n=100
13 Correct 5 ms 29276 KB n=100
14 Correct 6 ms 29528 KB n=100
15 Correct 6 ms 29276 KB n=100
16 Correct 6 ms 29276 KB n=100
17 Correct 5 ms 29272 KB n=100
18 Correct 5 ms 29276 KB n=100
19 Correct 5 ms 29276 KB n=100
20 Correct 5 ms 29276 KB n=100
21 Correct 6 ms 29276 KB n=100
22 Correct 5 ms 29276 KB n=100
23 Correct 5 ms 29372 KB n=100
24 Correct 5 ms 29276 KB n=100
25 Correct 5 ms 29276 KB n=100
26 Correct 6 ms 29276 KB n=12
27 Correct 6 ms 29276 KB n=100
28 Correct 6 ms 29272 KB n=500
29 Correct 7 ms 29392 KB n=500
30 Correct 6 ms 29276 KB n=500
31 Correct 6 ms 29528 KB n=500
32 Correct 6 ms 29472 KB n=500
33 Correct 6 ms 29276 KB n=500
34 Correct 7 ms 29276 KB n=500
35 Correct 6 ms 29452 KB n=500
36 Correct 6 ms 29308 KB n=500
37 Correct 6 ms 29276 KB n=500
38 Correct 6 ms 29276 KB n=500
39 Correct 6 ms 29528 KB n=500
40 Correct 6 ms 29276 KB n=500
41 Correct 6 ms 29276 KB n=500
42 Correct 6 ms 29276 KB n=500
43 Correct 6 ms 29276 KB n=500
44 Correct 6 ms 29276 KB n=500
45 Correct 6 ms 29276 KB n=500
46 Correct 6 ms 29272 KB n=500
47 Correct 6 ms 29276 KB n=500
48 Correct 6 ms 29276 KB n=500
49 Correct 6 ms 29272 KB n=500
50 Correct 6 ms 29276 KB n=500
51 Correct 6 ms 29368 KB n=500
52 Correct 6 ms 29272 KB n=500
53 Correct 6 ms 29276 KB n=500
54 Correct 6 ms 29272 KB n=500
55 Correct 6 ms 29272 KB n=278
56 Correct 6 ms 29276 KB n=500
57 Correct 6 ms 29532 KB n=500
58 Correct 6 ms 29276 KB n=500
59 Correct 8 ms 29684 KB n=2000
60 Correct 8 ms 29532 KB n=2000
61 Correct 8 ms 29528 KB n=2000
62 Correct 8 ms 29528 KB n=2000
63 Correct 8 ms 29528 KB n=2000
64 Correct 8 ms 29528 KB n=2000
65 Correct 8 ms 29532 KB n=2000
66 Correct 10 ms 29532 KB n=2000
67 Correct 8 ms 29532 KB n=2000
68 Correct 8 ms 29532 KB n=2000
69 Correct 8 ms 29624 KB n=2000
70 Correct 8 ms 29532 KB n=2000
71 Correct 8 ms 29684 KB n=2000
72 Correct 8 ms 29532 KB n=2000
73 Correct 7 ms 29532 KB n=2000
74 Correct 8 ms 29644 KB n=1844
75 Correct 8 ms 29528 KB n=2000
76 Correct 8 ms 29532 KB n=2000
77 Correct 8 ms 29984 KB n=2000
78 Correct 10 ms 29884 KB n=2000
79 Correct 8 ms 29528 KB n=2000
80 Correct 8 ms 29532 KB n=2000
81 Correct 9 ms 29712 KB n=2000
82 Correct 8 ms 29532 KB n=2000
83 Correct 8 ms 29532 KB n=2000
84 Correct 9 ms 29532 KB n=2000
85 Correct 8 ms 29532 KB n=2000
86 Correct 8 ms 29532 KB n=2000
87 Correct 8 ms 29532 KB n=2000
88 Correct 7 ms 29532 KB n=2000
89 Correct 8 ms 29532 KB n=2000
90 Correct 8 ms 29760 KB n=2000
91 Correct 7 ms 29528 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 29276 KB n=5
2 Correct 5 ms 29276 KB n=100
3 Correct 5 ms 29528 KB n=100
4 Correct 5 ms 29276 KB n=100
5 Correct 5 ms 29276 KB n=100
6 Correct 5 ms 29276 KB n=100
7 Correct 6 ms 29276 KB n=100
8 Correct 5 ms 29276 KB n=100
9 Correct 5 ms 29272 KB n=100
10 Correct 6 ms 29276 KB n=100
11 Correct 6 ms 29124 KB n=100
12 Correct 5 ms 29276 KB n=100
13 Correct 5 ms 29276 KB n=100
14 Correct 6 ms 29528 KB n=100
15 Correct 6 ms 29276 KB n=100
16 Correct 6 ms 29276 KB n=100
17 Correct 5 ms 29272 KB n=100
18 Correct 5 ms 29276 KB n=100
19 Correct 5 ms 29276 KB n=100
20 Correct 5 ms 29276 KB n=100
21 Correct 6 ms 29276 KB n=100
22 Correct 5 ms 29276 KB n=100
23 Correct 5 ms 29372 KB n=100
24 Correct 5 ms 29276 KB n=100
25 Correct 5 ms 29276 KB n=100
26 Correct 6 ms 29276 KB n=12
27 Correct 6 ms 29276 KB n=100
28 Correct 6 ms 29272 KB n=500
29 Correct 7 ms 29392 KB n=500
30 Correct 6 ms 29276 KB n=500
31 Correct 6 ms 29528 KB n=500
32 Correct 6 ms 29472 KB n=500
33 Correct 6 ms 29276 KB n=500
34 Correct 7 ms 29276 KB n=500
35 Correct 6 ms 29452 KB n=500
36 Correct 6 ms 29308 KB n=500
37 Correct 6 ms 29276 KB n=500
38 Correct 6 ms 29276 KB n=500
39 Correct 6 ms 29528 KB n=500
40 Correct 6 ms 29276 KB n=500
41 Correct 6 ms 29276 KB n=500
42 Correct 6 ms 29276 KB n=500
43 Correct 6 ms 29276 KB n=500
44 Correct 6 ms 29276 KB n=500
45 Correct 6 ms 29276 KB n=500
46 Correct 6 ms 29272 KB n=500
47 Correct 6 ms 29276 KB n=500
48 Correct 6 ms 29276 KB n=500
49 Correct 6 ms 29272 KB n=500
50 Correct 6 ms 29276 KB n=500
51 Correct 6 ms 29368 KB n=500
52 Correct 6 ms 29272 KB n=500
53 Correct 6 ms 29276 KB n=500
54 Correct 6 ms 29272 KB n=500
55 Correct 6 ms 29272 KB n=278
56 Correct 6 ms 29276 KB n=500
57 Correct 6 ms 29532 KB n=500
58 Correct 6 ms 29276 KB n=500
59 Correct 8 ms 29684 KB n=2000
60 Correct 8 ms 29532 KB n=2000
61 Correct 8 ms 29528 KB n=2000
62 Correct 8 ms 29528 KB n=2000
63 Correct 8 ms 29528 KB n=2000
64 Correct 8 ms 29528 KB n=2000
65 Correct 8 ms 29532 KB n=2000
66 Correct 10 ms 29532 KB n=2000
67 Correct 8 ms 29532 KB n=2000
68 Correct 8 ms 29532 KB n=2000
69 Correct 8 ms 29624 KB n=2000
70 Correct 8 ms 29532 KB n=2000
71 Correct 8 ms 29684 KB n=2000
72 Correct 8 ms 29532 KB n=2000
73 Correct 7 ms 29532 KB n=2000
74 Correct 8 ms 29644 KB n=1844
75 Correct 8 ms 29528 KB n=2000
76 Correct 8 ms 29532 KB n=2000
77 Correct 8 ms 29984 KB n=2000
78 Correct 10 ms 29884 KB n=2000
79 Correct 8 ms 29528 KB n=2000
80 Correct 8 ms 29532 KB n=2000
81 Correct 9 ms 29712 KB n=2000
82 Correct 8 ms 29532 KB n=2000
83 Correct 8 ms 29532 KB n=2000
84 Correct 9 ms 29532 KB n=2000
85 Correct 8 ms 29532 KB n=2000
86 Correct 8 ms 29532 KB n=2000
87 Correct 8 ms 29532 KB n=2000
88 Correct 7 ms 29532 KB n=2000
89 Correct 8 ms 29532 KB n=2000
90 Correct 8 ms 29760 KB n=2000
91 Correct 7 ms 29528 KB n=2000
92 Correct 547 ms 77820 KB n=200000
93 Correct 585 ms 82552 KB n=200000
94 Correct 421 ms 85844 KB n=200000
95 Correct 527 ms 77436 KB n=200000
96 Correct 489 ms 77508 KB n=200000
97 Correct 591 ms 81816 KB n=200000
98 Correct 514 ms 77780 KB n=200000
99 Correct 580 ms 77652 KB n=200000
100 Correct 526 ms 77672 KB n=200000
101 Correct 381 ms 87424 KB n=200000
102 Correct 279 ms 78800 KB n=200000
103 Correct 284 ms 78672 KB n=200000
104 Correct 278 ms 78648 KB n=200000
105 Correct 303 ms 79184 KB n=200000
106 Correct 306 ms 79280 KB n=200000
107 Correct 289 ms 79188 KB n=200000
108 Correct 520 ms 77904 KB n=200000
109 Correct 523 ms 77644 KB n=200000
110 Correct 511 ms 77916 KB n=200000
111 Correct 515 ms 77128 KB n=200000
112 Correct 387 ms 86280 KB n=200000
113 Correct 550 ms 81744 KB n=200000
114 Correct 522 ms 77256 KB n=200000
115 Correct 632 ms 79748 KB n=200000
116 Correct 547 ms 77764 KB n=200000
117 Correct 392 ms 87016 KB n=200000
118 Correct 631 ms 80464 KB n=200000
119 Correct 490 ms 77768 KB n=200000
120 Correct 327 ms 86096 KB n=200000
121 Correct 323 ms 86152 KB n=200000
122 Correct 306 ms 86352 KB n=200000
123 Correct 305 ms 78832 KB n=200000
124 Correct 93 ms 43788 KB n=25264