Submission #492389

# Submission time Handle Problem Language Result Execution time Memory
492389 2021-12-07T04:05:29 Z hollwo_pelw Birthday gift (IZhO18_treearray) C++17
100 / 100
1135 ms 74904 KB
/* 
// is short or still long ???
hollwo_pelw's template(short)
// Note : -Dhollwo_pelw_local
*/

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

using namespace std;
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;

void FAST_IO(string filein = "", string fileout = "", string fileerr = ""){
	if (fopen(filein.c_str(), "r")){
		freopen(fileout.c_str(), "w", stdout);
		freopen(filein.c_str(), "r", stdin);
#ifdef hollwo_pelw_local
		freopen(fileerr.c_str(), "w", stderr);
#endif
	}
	cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
}

void Hollwo_Pelw();

signed main(){
#ifdef hollwo_pelw_local
	FAST_IO("input.inp", "output.out", "error.err");
	auto start = chrono::steady_clock::now();
#else
	FAST_IO("hollwo_pelw.inp", "hollwo_pelw.out");
#endif
	int testcases = 1;
	// cin >> testcases;
	for (int test = 1; test <= testcases; test++){
		// cout << "Case #" << test << ": ";
		Hollwo_Pelw();
	}
#ifdef hollwo_pelw_local
	auto end = chrono::steady_clock::now();
	cout << endl;
	cout << "Excution time : " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif
	return 0;
}

const int N = 2e5 + 5;

int n, m, q, a[N], h[N], par[18][N];
vector<int> adj[N];
set<int> st1[N], st2[N];

void pre_dfs(int u, int p) {
	h[u] = h[p] + 1;
	par[0][u] = p;
	for (int i = 1; i <= 17; i++)
		par[i][u] = par[i - 1][par[i - 1][u]];

	for (auto v : adj[u]) if (v != p)
		pre_dfs(v, u);
}

inline int lca(int u, int v) {
	if (h[u] > h[v]) swap(u, v);
	for (int i = 17; ~i; i--)
		if (h[v] - h[u] >> i & 1)
			v = par[i][v];
	if (u == v) return u;
	for (int i = 17; ~i; i--)
		if (par[i][u] ^ par[i][v]) {
			v = par[i][v];
			u = par[i][u];
		}
	return par[0][u];
}

void Hollwo_Pelw() {
	cin >> n >> m >> q;
	for (int i = 1, u, v; i < n; i++)
		cin >> u >> v, adj[u].push_back(v), adj[v].push_back(u);
	for (int i = 1; i <= m; i++) cin >> a[i];
	pre_dfs(1, 0);

	for (int i = 1; i <= m; i++) {
		st1[a[i]].insert(i);
		if (i < m)
			st2[lca(a[i], a[i + 1])].insert(i);
	}
	for (int t, p, l, r, v; q --; ) {
		cin >> t;
		if (t == 1) {
			cin >> p >> v;
			st1[a[p]].erase(p);
			if (p < m) st2[lca(a[p], a[p + 1])].erase(p);
			if (p > 1) st2[lca(a[p], a[p - 1])].erase(p - 1);

			a[p] = v;
			
			st1[a[p]].insert(p);
			if (p < m) st2[lca(a[p], a[p + 1])].insert(p);
			if (p > 1) st2[lca(a[p], a[p - 1])].insert(p - 1);
		} else {
			cin >> l >> r >> v;
			{
				auto it = st1[v].lower_bound(l);
				if (it != st1[v].end() && *it <= r) {
					cout << *it << ' ' << *it << '\n';
					continue ;
				}
			}
			{
				auto it = st2[v].lower_bound(l);
				if (it != st2[v].end() && *it < r) {
					cout << *it << ' ' << (*it) + 1 << '\n';
					continue ;
				}
			}
			cout << "-1 -1\n";
		}
	}
}

Compilation message

treearray.cpp: In function 'int lca(int, int)':
treearray.cpp:69:12: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
   69 |   if (h[v] - h[u] >> i & 1)
      |       ~~~~~^~~~~~
treearray.cpp: In function 'void FAST_IO(std::string, std::string, std::string)':
treearray.cpp:18:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |   freopen(fileout.c_str(), "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:19:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |   freopen(filein.c_str(), "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23884 KB n=5
2 Correct 13 ms 23936 KB n=100
3 Correct 16 ms 23804 KB n=100
4 Correct 19 ms 23892 KB n=100
5 Correct 13 ms 23884 KB n=100
6 Correct 13 ms 23884 KB n=100
7 Correct 11 ms 23924 KB n=100
8 Correct 11 ms 23884 KB n=100
9 Correct 12 ms 23884 KB n=100
10 Correct 12 ms 23872 KB n=100
11 Correct 12 ms 23884 KB n=100
12 Correct 15 ms 23892 KB n=100
13 Correct 13 ms 23912 KB n=100
14 Correct 16 ms 23884 KB n=100
15 Correct 16 ms 23884 KB n=100
16 Correct 21 ms 23884 KB n=100
17 Correct 12 ms 23928 KB n=100
18 Correct 12 ms 23884 KB n=100
19 Correct 13 ms 23828 KB n=100
20 Correct 14 ms 23856 KB n=100
21 Correct 17 ms 23912 KB n=100
22 Correct 13 ms 23884 KB n=100
23 Correct 19 ms 23868 KB n=100
24 Correct 13 ms 23884 KB n=100
25 Correct 14 ms 23924 KB n=100
26 Correct 13 ms 23884 KB n=12
27 Correct 12 ms 23932 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23884 KB n=5
2 Correct 13 ms 23936 KB n=100
3 Correct 16 ms 23804 KB n=100
4 Correct 19 ms 23892 KB n=100
5 Correct 13 ms 23884 KB n=100
6 Correct 13 ms 23884 KB n=100
7 Correct 11 ms 23924 KB n=100
8 Correct 11 ms 23884 KB n=100
9 Correct 12 ms 23884 KB n=100
10 Correct 12 ms 23872 KB n=100
11 Correct 12 ms 23884 KB n=100
12 Correct 15 ms 23892 KB n=100
13 Correct 13 ms 23912 KB n=100
14 Correct 16 ms 23884 KB n=100
15 Correct 16 ms 23884 KB n=100
16 Correct 21 ms 23884 KB n=100
17 Correct 12 ms 23928 KB n=100
18 Correct 12 ms 23884 KB n=100
19 Correct 13 ms 23828 KB n=100
20 Correct 14 ms 23856 KB n=100
21 Correct 17 ms 23912 KB n=100
22 Correct 13 ms 23884 KB n=100
23 Correct 19 ms 23868 KB n=100
24 Correct 13 ms 23884 KB n=100
25 Correct 14 ms 23924 KB n=100
26 Correct 13 ms 23884 KB n=12
27 Correct 12 ms 23932 KB n=100
28 Correct 14 ms 24024 KB n=500
29 Correct 13 ms 23964 KB n=500
30 Correct 13 ms 23948 KB n=500
31 Correct 17 ms 24004 KB n=500
32 Correct 14 ms 24012 KB n=500
33 Correct 20 ms 23992 KB n=500
34 Correct 19 ms 24012 KB n=500
35 Correct 19 ms 23928 KB n=500
36 Correct 14 ms 24012 KB n=500
37 Correct 12 ms 24004 KB n=500
38 Correct 13 ms 24012 KB n=500
39 Correct 13 ms 23944 KB n=500
40 Correct 13 ms 24012 KB n=500
41 Correct 13 ms 23948 KB n=500
42 Correct 13 ms 23992 KB n=500
43 Correct 13 ms 24012 KB n=500
44 Correct 13 ms 24012 KB n=500
45 Correct 14 ms 24012 KB n=500
46 Correct 14 ms 23960 KB n=500
47 Correct 13 ms 24040 KB n=500
48 Correct 15 ms 23936 KB n=500
49 Correct 16 ms 23948 KB n=500
50 Correct 14 ms 23916 KB n=500
51 Correct 16 ms 24004 KB n=500
52 Correct 12 ms 24012 KB n=500
53 Correct 12 ms 23948 KB n=500
54 Correct 14 ms 24016 KB n=500
55 Correct 17 ms 23884 KB n=278
56 Correct 13 ms 23940 KB n=500
57 Correct 14 ms 23964 KB n=500
58 Correct 13 ms 23940 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23884 KB n=5
2 Correct 13 ms 23936 KB n=100
3 Correct 16 ms 23804 KB n=100
4 Correct 19 ms 23892 KB n=100
5 Correct 13 ms 23884 KB n=100
6 Correct 13 ms 23884 KB n=100
7 Correct 11 ms 23924 KB n=100
8 Correct 11 ms 23884 KB n=100
9 Correct 12 ms 23884 KB n=100
10 Correct 12 ms 23872 KB n=100
11 Correct 12 ms 23884 KB n=100
12 Correct 15 ms 23892 KB n=100
13 Correct 13 ms 23912 KB n=100
14 Correct 16 ms 23884 KB n=100
15 Correct 16 ms 23884 KB n=100
16 Correct 21 ms 23884 KB n=100
17 Correct 12 ms 23928 KB n=100
18 Correct 12 ms 23884 KB n=100
19 Correct 13 ms 23828 KB n=100
20 Correct 14 ms 23856 KB n=100
21 Correct 17 ms 23912 KB n=100
22 Correct 13 ms 23884 KB n=100
23 Correct 19 ms 23868 KB n=100
24 Correct 13 ms 23884 KB n=100
25 Correct 14 ms 23924 KB n=100
26 Correct 13 ms 23884 KB n=12
27 Correct 12 ms 23932 KB n=100
28 Correct 14 ms 24024 KB n=500
29 Correct 13 ms 23964 KB n=500
30 Correct 13 ms 23948 KB n=500
31 Correct 17 ms 24004 KB n=500
32 Correct 14 ms 24012 KB n=500
33 Correct 20 ms 23992 KB n=500
34 Correct 19 ms 24012 KB n=500
35 Correct 19 ms 23928 KB n=500
36 Correct 14 ms 24012 KB n=500
37 Correct 12 ms 24004 KB n=500
38 Correct 13 ms 24012 KB n=500
39 Correct 13 ms 23944 KB n=500
40 Correct 13 ms 24012 KB n=500
41 Correct 13 ms 23948 KB n=500
42 Correct 13 ms 23992 KB n=500
43 Correct 13 ms 24012 KB n=500
44 Correct 13 ms 24012 KB n=500
45 Correct 14 ms 24012 KB n=500
46 Correct 14 ms 23960 KB n=500
47 Correct 13 ms 24040 KB n=500
48 Correct 15 ms 23936 KB n=500
49 Correct 16 ms 23948 KB n=500
50 Correct 14 ms 23916 KB n=500
51 Correct 16 ms 24004 KB n=500
52 Correct 12 ms 24012 KB n=500
53 Correct 12 ms 23948 KB n=500
54 Correct 14 ms 24016 KB n=500
55 Correct 17 ms 23884 KB n=278
56 Correct 13 ms 23940 KB n=500
57 Correct 14 ms 23964 KB n=500
58 Correct 13 ms 23940 KB n=500
59 Correct 16 ms 24360 KB n=2000
60 Correct 16 ms 24396 KB n=2000
61 Correct 17 ms 24328 KB n=2000
62 Correct 15 ms 24332 KB n=2000
63 Correct 15 ms 24268 KB n=2000
64 Correct 17 ms 24320 KB n=2000
65 Correct 22 ms 24332 KB n=2000
66 Correct 15 ms 24396 KB n=2000
67 Correct 15 ms 24268 KB n=2000
68 Correct 17 ms 24316 KB n=2000
69 Correct 17 ms 24388 KB n=2000
70 Correct 19 ms 24336 KB n=2000
71 Correct 17 ms 24324 KB n=2000
72 Correct 12 ms 24268 KB n=2000
73 Correct 18 ms 24268 KB n=2000
74 Correct 15 ms 24268 KB n=1844
75 Correct 25 ms 24324 KB n=2000
76 Correct 15 ms 24268 KB n=2000
77 Correct 16 ms 24268 KB n=2000
78 Correct 20 ms 24336 KB n=2000
79 Correct 20 ms 24376 KB n=2000
80 Correct 16 ms 24444 KB n=2000
81 Correct 16 ms 24364 KB n=2000
82 Correct 17 ms 24248 KB n=2000
83 Correct 22 ms 24328 KB n=2000
84 Correct 14 ms 24308 KB n=2000
85 Correct 16 ms 24364 KB n=2000
86 Correct 16 ms 24396 KB n=2000
87 Correct 15 ms 24348 KB n=2000
88 Correct 15 ms 24456 KB n=2000
89 Correct 14 ms 24452 KB n=2000
90 Correct 16 ms 24392 KB n=2000
91 Correct 19 ms 24260 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23884 KB n=5
2 Correct 13 ms 23936 KB n=100
3 Correct 16 ms 23804 KB n=100
4 Correct 19 ms 23892 KB n=100
5 Correct 13 ms 23884 KB n=100
6 Correct 13 ms 23884 KB n=100
7 Correct 11 ms 23924 KB n=100
8 Correct 11 ms 23884 KB n=100
9 Correct 12 ms 23884 KB n=100
10 Correct 12 ms 23872 KB n=100
11 Correct 12 ms 23884 KB n=100
12 Correct 15 ms 23892 KB n=100
13 Correct 13 ms 23912 KB n=100
14 Correct 16 ms 23884 KB n=100
15 Correct 16 ms 23884 KB n=100
16 Correct 21 ms 23884 KB n=100
17 Correct 12 ms 23928 KB n=100
18 Correct 12 ms 23884 KB n=100
19 Correct 13 ms 23828 KB n=100
20 Correct 14 ms 23856 KB n=100
21 Correct 17 ms 23912 KB n=100
22 Correct 13 ms 23884 KB n=100
23 Correct 19 ms 23868 KB n=100
24 Correct 13 ms 23884 KB n=100
25 Correct 14 ms 23924 KB n=100
26 Correct 13 ms 23884 KB n=12
27 Correct 12 ms 23932 KB n=100
28 Correct 14 ms 24024 KB n=500
29 Correct 13 ms 23964 KB n=500
30 Correct 13 ms 23948 KB n=500
31 Correct 17 ms 24004 KB n=500
32 Correct 14 ms 24012 KB n=500
33 Correct 20 ms 23992 KB n=500
34 Correct 19 ms 24012 KB n=500
35 Correct 19 ms 23928 KB n=500
36 Correct 14 ms 24012 KB n=500
37 Correct 12 ms 24004 KB n=500
38 Correct 13 ms 24012 KB n=500
39 Correct 13 ms 23944 KB n=500
40 Correct 13 ms 24012 KB n=500
41 Correct 13 ms 23948 KB n=500
42 Correct 13 ms 23992 KB n=500
43 Correct 13 ms 24012 KB n=500
44 Correct 13 ms 24012 KB n=500
45 Correct 14 ms 24012 KB n=500
46 Correct 14 ms 23960 KB n=500
47 Correct 13 ms 24040 KB n=500
48 Correct 15 ms 23936 KB n=500
49 Correct 16 ms 23948 KB n=500
50 Correct 14 ms 23916 KB n=500
51 Correct 16 ms 24004 KB n=500
52 Correct 12 ms 24012 KB n=500
53 Correct 12 ms 23948 KB n=500
54 Correct 14 ms 24016 KB n=500
55 Correct 17 ms 23884 KB n=278
56 Correct 13 ms 23940 KB n=500
57 Correct 14 ms 23964 KB n=500
58 Correct 13 ms 23940 KB n=500
59 Correct 16 ms 24360 KB n=2000
60 Correct 16 ms 24396 KB n=2000
61 Correct 17 ms 24328 KB n=2000
62 Correct 15 ms 24332 KB n=2000
63 Correct 15 ms 24268 KB n=2000
64 Correct 17 ms 24320 KB n=2000
65 Correct 22 ms 24332 KB n=2000
66 Correct 15 ms 24396 KB n=2000
67 Correct 15 ms 24268 KB n=2000
68 Correct 17 ms 24316 KB n=2000
69 Correct 17 ms 24388 KB n=2000
70 Correct 19 ms 24336 KB n=2000
71 Correct 17 ms 24324 KB n=2000
72 Correct 12 ms 24268 KB n=2000
73 Correct 18 ms 24268 KB n=2000
74 Correct 15 ms 24268 KB n=1844
75 Correct 25 ms 24324 KB n=2000
76 Correct 15 ms 24268 KB n=2000
77 Correct 16 ms 24268 KB n=2000
78 Correct 20 ms 24336 KB n=2000
79 Correct 20 ms 24376 KB n=2000
80 Correct 16 ms 24444 KB n=2000
81 Correct 16 ms 24364 KB n=2000
82 Correct 17 ms 24248 KB n=2000
83 Correct 22 ms 24328 KB n=2000
84 Correct 14 ms 24308 KB n=2000
85 Correct 16 ms 24364 KB n=2000
86 Correct 16 ms 24396 KB n=2000
87 Correct 15 ms 24348 KB n=2000
88 Correct 15 ms 24456 KB n=2000
89 Correct 14 ms 24452 KB n=2000
90 Correct 16 ms 24392 KB n=2000
91 Correct 19 ms 24260 KB n=2000
92 Correct 783 ms 66820 KB n=200000
93 Correct 1117 ms 70800 KB n=200000
94 Correct 1004 ms 74016 KB n=200000
95 Correct 810 ms 66608 KB n=200000
96 Correct 719 ms 66740 KB n=200000
97 Correct 1061 ms 69684 KB n=200000
98 Correct 848 ms 66384 KB n=200000
99 Correct 1073 ms 65868 KB n=200000
100 Correct 802 ms 66444 KB n=200000
101 Correct 943 ms 74656 KB n=200000
102 Correct 450 ms 67196 KB n=200000
103 Correct 485 ms 67304 KB n=200000
104 Correct 451 ms 67280 KB n=200000
105 Correct 488 ms 67048 KB n=200000
106 Correct 509 ms 67156 KB n=200000
107 Correct 494 ms 67000 KB n=200000
108 Correct 949 ms 66076 KB n=200000
109 Correct 934 ms 66012 KB n=200000
110 Correct 867 ms 66016 KB n=200000
111 Correct 738 ms 66488 KB n=200000
112 Correct 911 ms 73924 KB n=200000
113 Correct 987 ms 69360 KB n=200000
114 Correct 734 ms 66368 KB n=200000
115 Correct 1135 ms 67196 KB n=200000
116 Correct 736 ms 66132 KB n=200000
117 Correct 920 ms 74072 KB n=200000
118 Correct 1036 ms 68052 KB n=200000
119 Correct 729 ms 66112 KB n=200000
120 Correct 883 ms 74420 KB n=200000
121 Correct 883 ms 74448 KB n=200000
122 Correct 911 ms 74904 KB n=200000
123 Correct 474 ms 66744 KB n=200000
124 Correct 229 ms 36600 KB n=25264