Submission #470617

# Submission time Handle Problem Language Result Execution time Memory
470617 2021-09-04T15:19:44 Z Return_0 Birthday gift (IZhO18_treearray) C++17
100 / 100
841 ms 99144 KB
#pragma GCC optimize("Ofast,unroll-loops")
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native")

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

using namespace std;
using namespace __gnu_pbds;

typedef int ll;
typedef long double ld;
typedef pair <ll, ll> pll;

#ifdef SINA
#define dbg(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << std::endl; }
template <typename Arg1, typename... Args> void __f (const char* names, Arg1&& arg1, Args&&... args) {
	const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); }
#define dbg2(x, j, n) cout<< #x << " : "; output((j), (n), x, 1); cout.flush();
#else
#define dbg(...) 0
#define dbg2(x, j, n) 0
#endif
#define SZ(x) ((ll)((x).size()))
#define File(s, t) freopen(s ".txt", "r", stdin); freopen(t ".txt", "w", stdout);
#define input(j, n, a) for (int _i = (j); _i < (n)+(j); _i++) cin>> a[_i];
#define output(j, n, a, t) for (int _i = (j); _i < (n)+(j); _i++) cout<< a[_i] << (((t) && _i != (n)+(j)-1)? ' ' : '\n');
#define kill(x) return cout<< (x) << endl, 0
#define cl const ll
#define fr first
#define sc second
#define lc (v << 1)
#define rc (lc | 1)
#define mid ((l + r) >> 1)
#define All(x) (x).begin(), (x).end()

cl inf = sizeof(ll) == 4 ? (1e9 + 10) : (3e18), mod = 1e9 + 7, MOD = 998244353;

template <class A,class B> ostream& operator << (ostream& out,const pair<A,B>&a){return out<<'('<<a.first<<", "<<a.second<<')';}
template <class A> ostream& operator << (ostream& out, const vector<A> &a) {
	out<< '['; for (int i = -1; ++i < int(a.size());) out<< a[i] << (i + 1 < int(a.size()) ? ", " : ""); return out<<']'; }
template <class T, typename _t = less <T> > using Tree = tree <T, null_type, _t, rb_tree_tag, tree_order_statistics_node_update>;

cl N = 2e5 + 7;

ll a [N], h [N], par [N], hd [N], bc [N], sz [N], n, m, q;
set <ll> pos [N], st [N];
vector <ll> adj [N];

ll DFS (cl &v = 1, cl &pr = 0) {
	h[v] = h[par[v] = pr] + 1;
	for (auto &u : adj[v]) if (u != pr) sz[v] += DFS(u, v), bc[v] = sz[u] > sz[bc[v]] ? u : bc[v];
	return ++sz[v];
}

void HLD (cl &v = 1, cl &head = 1) {
	hd[v] = head;
	if (bc[v]) HLD(bc[v], head);
	for (auto &u : adj[v]) if (u != par[v] && u != bc[v]) HLD(u, u);
}

inline ll LCA (ll v, ll u) {
	while (hd[v] != hd[u]) {
		if (h[hd[v]] < h[hd[u]]) u = par[hd[u]];
		else v = par[hd[v]];
	}
	return h[v] < h[u] ? v : u;
}

int main ()
{
	cin.tie(0)->sync_with_stdio(false);

	cin>> n >> m >> q;
	for (ll v, u, i = 1; i < n; i++) {
		cin>> v >> u;
		adj[v].push_back(u);
		adj[u].push_back(v);
	}
	assert(DFS() == n); HLD();
	for (ll i = 1; i <= m; i++) cin>> a[i], pos[a[i]].insert(i);
	for (ll i = 1; i < m; i++) st[LCA(a[i], a[i + 1])].insert(i);
	for (ll tp, x, y, z; q--;) {
		cin>> tp >> x >> y;
		if (tp == 1) {
			pos[a[x]].erase(x);
			if (x > 1) st[LCA(a[x - 1], a[x])].erase(x - 1);
			if (x < m) st[LCA(a[x], a[x + 1])].erase(x);
			pos[a[x] = y].insert(x);
			if (x > 1) st[LCA(a[x - 1], a[x])].insert(x - 1);
			if (x < m) st[LCA(a[x], a[x + 1])].insert(x);
		} else {
			cin>> z;
			auto it = pos[z].lower_bound(x);
			if (it != pos[z].end() && *it <= y) { cout<< *it << ' ' << *it << '\n'; continue; }
			it = st[z].lower_bound(x);
			if (it != st[z].end() && *it < y) { cout<< *it << ' ' << *it + 1 << '\n'; continue; }
			cout<< "-1 -1\n";
		}
	}

	cerr<< "\n" __TIMESTAMP__ " - Time elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";

	return 0;
}
/*
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

*/
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23756 KB n=5
2 Correct 16 ms 23756 KB n=100
3 Correct 16 ms 23756 KB n=100
4 Correct 16 ms 23808 KB n=100
5 Correct 18 ms 23840 KB n=100
6 Correct 16 ms 23756 KB n=100
7 Correct 15 ms 23760 KB n=100
8 Correct 15 ms 23804 KB n=100
9 Correct 16 ms 23756 KB n=100
10 Correct 15 ms 23756 KB n=100
11 Correct 16 ms 23756 KB n=100
12 Correct 15 ms 23736 KB n=100
13 Correct 15 ms 23832 KB n=100
14 Correct 16 ms 23808 KB n=100
15 Correct 16 ms 23756 KB n=100
16 Correct 15 ms 23756 KB n=100
17 Correct 15 ms 23756 KB n=100
18 Correct 16 ms 23808 KB n=100
19 Correct 16 ms 23756 KB n=100
20 Correct 16 ms 23804 KB n=100
21 Correct 15 ms 23804 KB n=100
22 Correct 16 ms 23832 KB n=100
23 Correct 16 ms 23756 KB n=100
24 Correct 16 ms 23808 KB n=100
25 Correct 16 ms 23808 KB n=100
26 Correct 15 ms 23812 KB n=12
27 Correct 15 ms 23776 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23756 KB n=5
2 Correct 16 ms 23756 KB n=100
3 Correct 16 ms 23756 KB n=100
4 Correct 16 ms 23808 KB n=100
5 Correct 18 ms 23840 KB n=100
6 Correct 16 ms 23756 KB n=100
7 Correct 15 ms 23760 KB n=100
8 Correct 15 ms 23804 KB n=100
9 Correct 16 ms 23756 KB n=100
10 Correct 15 ms 23756 KB n=100
11 Correct 16 ms 23756 KB n=100
12 Correct 15 ms 23736 KB n=100
13 Correct 15 ms 23832 KB n=100
14 Correct 16 ms 23808 KB n=100
15 Correct 16 ms 23756 KB n=100
16 Correct 15 ms 23756 KB n=100
17 Correct 15 ms 23756 KB n=100
18 Correct 16 ms 23808 KB n=100
19 Correct 16 ms 23756 KB n=100
20 Correct 16 ms 23804 KB n=100
21 Correct 15 ms 23804 KB n=100
22 Correct 16 ms 23832 KB n=100
23 Correct 16 ms 23756 KB n=100
24 Correct 16 ms 23808 KB n=100
25 Correct 16 ms 23808 KB n=100
26 Correct 15 ms 23812 KB n=12
27 Correct 15 ms 23776 KB n=100
28 Correct 16 ms 23884 KB n=500
29 Correct 16 ms 23928 KB n=500
30 Correct 16 ms 23948 KB n=500
31 Correct 16 ms 23884 KB n=500
32 Correct 16 ms 23900 KB n=500
33 Correct 16 ms 23884 KB n=500
34 Correct 16 ms 23812 KB n=500
35 Correct 16 ms 24012 KB n=500
36 Correct 15 ms 23884 KB n=500
37 Correct 16 ms 23904 KB n=500
38 Correct 18 ms 23884 KB n=500
39 Correct 16 ms 23884 KB n=500
40 Correct 16 ms 23920 KB n=500
41 Correct 16 ms 23872 KB n=500
42 Correct 16 ms 23816 KB n=500
43 Correct 16 ms 23884 KB n=500
44 Correct 17 ms 23884 KB n=500
45 Correct 16 ms 23884 KB n=500
46 Correct 16 ms 24012 KB n=500
47 Correct 16 ms 23916 KB n=500
48 Correct 17 ms 23884 KB n=500
49 Correct 16 ms 23988 KB n=500
50 Correct 16 ms 23816 KB n=500
51 Correct 17 ms 23884 KB n=500
52 Correct 16 ms 23992 KB n=500
53 Correct 16 ms 23884 KB n=500
54 Correct 16 ms 24012 KB n=500
55 Correct 16 ms 23812 KB n=278
56 Correct 16 ms 23948 KB n=500
57 Correct 16 ms 24016 KB n=500
58 Correct 16 ms 23836 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23756 KB n=5
2 Correct 16 ms 23756 KB n=100
3 Correct 16 ms 23756 KB n=100
4 Correct 16 ms 23808 KB n=100
5 Correct 18 ms 23840 KB n=100
6 Correct 16 ms 23756 KB n=100
7 Correct 15 ms 23760 KB n=100
8 Correct 15 ms 23804 KB n=100
9 Correct 16 ms 23756 KB n=100
10 Correct 15 ms 23756 KB n=100
11 Correct 16 ms 23756 KB n=100
12 Correct 15 ms 23736 KB n=100
13 Correct 15 ms 23832 KB n=100
14 Correct 16 ms 23808 KB n=100
15 Correct 16 ms 23756 KB n=100
16 Correct 15 ms 23756 KB n=100
17 Correct 15 ms 23756 KB n=100
18 Correct 16 ms 23808 KB n=100
19 Correct 16 ms 23756 KB n=100
20 Correct 16 ms 23804 KB n=100
21 Correct 15 ms 23804 KB n=100
22 Correct 16 ms 23832 KB n=100
23 Correct 16 ms 23756 KB n=100
24 Correct 16 ms 23808 KB n=100
25 Correct 16 ms 23808 KB n=100
26 Correct 15 ms 23812 KB n=12
27 Correct 15 ms 23776 KB n=100
28 Correct 16 ms 23884 KB n=500
29 Correct 16 ms 23928 KB n=500
30 Correct 16 ms 23948 KB n=500
31 Correct 16 ms 23884 KB n=500
32 Correct 16 ms 23900 KB n=500
33 Correct 16 ms 23884 KB n=500
34 Correct 16 ms 23812 KB n=500
35 Correct 16 ms 24012 KB n=500
36 Correct 15 ms 23884 KB n=500
37 Correct 16 ms 23904 KB n=500
38 Correct 18 ms 23884 KB n=500
39 Correct 16 ms 23884 KB n=500
40 Correct 16 ms 23920 KB n=500
41 Correct 16 ms 23872 KB n=500
42 Correct 16 ms 23816 KB n=500
43 Correct 16 ms 23884 KB n=500
44 Correct 17 ms 23884 KB n=500
45 Correct 16 ms 23884 KB n=500
46 Correct 16 ms 24012 KB n=500
47 Correct 16 ms 23916 KB n=500
48 Correct 17 ms 23884 KB n=500
49 Correct 16 ms 23988 KB n=500
50 Correct 16 ms 23816 KB n=500
51 Correct 17 ms 23884 KB n=500
52 Correct 16 ms 23992 KB n=500
53 Correct 16 ms 23884 KB n=500
54 Correct 16 ms 24012 KB n=500
55 Correct 16 ms 23812 KB n=278
56 Correct 16 ms 23948 KB n=500
57 Correct 16 ms 24016 KB n=500
58 Correct 16 ms 23836 KB n=500
59 Correct 19 ms 24196 KB n=2000
60 Correct 18 ms 24488 KB n=2000
61 Correct 19 ms 24360 KB n=2000
62 Correct 18 ms 24248 KB n=2000
63 Correct 18 ms 24088 KB n=2000
64 Correct 19 ms 24268 KB n=2000
65 Correct 19 ms 24096 KB n=2000
66 Correct 19 ms 24456 KB n=2000
67 Correct 19 ms 24156 KB n=2000
68 Correct 19 ms 24268 KB n=2000
69 Correct 18 ms 24152 KB n=2000
70 Correct 18 ms 24112 KB n=2000
71 Correct 19 ms 24140 KB n=2000
72 Correct 18 ms 24144 KB n=2000
73 Correct 18 ms 24140 KB n=2000
74 Correct 18 ms 24144 KB n=1844
75 Correct 18 ms 24140 KB n=2000
76 Correct 19 ms 24124 KB n=2000
77 Correct 19 ms 24168 KB n=2000
78 Correct 19 ms 24140 KB n=2000
79 Correct 19 ms 24172 KB n=2000
80 Correct 18 ms 24420 KB n=2000
81 Correct 19 ms 24356 KB n=2000
82 Correct 18 ms 24172 KB n=2000
83 Correct 19 ms 24388 KB n=2000
84 Correct 19 ms 24140 KB n=2000
85 Correct 19 ms 24332 KB n=2000
86 Correct 19 ms 24268 KB n=2000
87 Correct 19 ms 24092 KB n=2000
88 Correct 18 ms 24524 KB n=2000
89 Correct 19 ms 24452 KB n=2000
90 Correct 18 ms 24460 KB n=2000
91 Correct 18 ms 24188 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23756 KB n=5
2 Correct 16 ms 23756 KB n=100
3 Correct 16 ms 23756 KB n=100
4 Correct 16 ms 23808 KB n=100
5 Correct 18 ms 23840 KB n=100
6 Correct 16 ms 23756 KB n=100
7 Correct 15 ms 23760 KB n=100
8 Correct 15 ms 23804 KB n=100
9 Correct 16 ms 23756 KB n=100
10 Correct 15 ms 23756 KB n=100
11 Correct 16 ms 23756 KB n=100
12 Correct 15 ms 23736 KB n=100
13 Correct 15 ms 23832 KB n=100
14 Correct 16 ms 23808 KB n=100
15 Correct 16 ms 23756 KB n=100
16 Correct 15 ms 23756 KB n=100
17 Correct 15 ms 23756 KB n=100
18 Correct 16 ms 23808 KB n=100
19 Correct 16 ms 23756 KB n=100
20 Correct 16 ms 23804 KB n=100
21 Correct 15 ms 23804 KB n=100
22 Correct 16 ms 23832 KB n=100
23 Correct 16 ms 23756 KB n=100
24 Correct 16 ms 23808 KB n=100
25 Correct 16 ms 23808 KB n=100
26 Correct 15 ms 23812 KB n=12
27 Correct 15 ms 23776 KB n=100
28 Correct 16 ms 23884 KB n=500
29 Correct 16 ms 23928 KB n=500
30 Correct 16 ms 23948 KB n=500
31 Correct 16 ms 23884 KB n=500
32 Correct 16 ms 23900 KB n=500
33 Correct 16 ms 23884 KB n=500
34 Correct 16 ms 23812 KB n=500
35 Correct 16 ms 24012 KB n=500
36 Correct 15 ms 23884 KB n=500
37 Correct 16 ms 23904 KB n=500
38 Correct 18 ms 23884 KB n=500
39 Correct 16 ms 23884 KB n=500
40 Correct 16 ms 23920 KB n=500
41 Correct 16 ms 23872 KB n=500
42 Correct 16 ms 23816 KB n=500
43 Correct 16 ms 23884 KB n=500
44 Correct 17 ms 23884 KB n=500
45 Correct 16 ms 23884 KB n=500
46 Correct 16 ms 24012 KB n=500
47 Correct 16 ms 23916 KB n=500
48 Correct 17 ms 23884 KB n=500
49 Correct 16 ms 23988 KB n=500
50 Correct 16 ms 23816 KB n=500
51 Correct 17 ms 23884 KB n=500
52 Correct 16 ms 23992 KB n=500
53 Correct 16 ms 23884 KB n=500
54 Correct 16 ms 24012 KB n=500
55 Correct 16 ms 23812 KB n=278
56 Correct 16 ms 23948 KB n=500
57 Correct 16 ms 24016 KB n=500
58 Correct 16 ms 23836 KB n=500
59 Correct 19 ms 24196 KB n=2000
60 Correct 18 ms 24488 KB n=2000
61 Correct 19 ms 24360 KB n=2000
62 Correct 18 ms 24248 KB n=2000
63 Correct 18 ms 24088 KB n=2000
64 Correct 19 ms 24268 KB n=2000
65 Correct 19 ms 24096 KB n=2000
66 Correct 19 ms 24456 KB n=2000
67 Correct 19 ms 24156 KB n=2000
68 Correct 19 ms 24268 KB n=2000
69 Correct 18 ms 24152 KB n=2000
70 Correct 18 ms 24112 KB n=2000
71 Correct 19 ms 24140 KB n=2000
72 Correct 18 ms 24144 KB n=2000
73 Correct 18 ms 24140 KB n=2000
74 Correct 18 ms 24144 KB n=1844
75 Correct 18 ms 24140 KB n=2000
76 Correct 19 ms 24124 KB n=2000
77 Correct 19 ms 24168 KB n=2000
78 Correct 19 ms 24140 KB n=2000
79 Correct 19 ms 24172 KB n=2000
80 Correct 18 ms 24420 KB n=2000
81 Correct 19 ms 24356 KB n=2000
82 Correct 18 ms 24172 KB n=2000
83 Correct 19 ms 24388 KB n=2000
84 Correct 19 ms 24140 KB n=2000
85 Correct 19 ms 24332 KB n=2000
86 Correct 19 ms 24268 KB n=2000
87 Correct 19 ms 24092 KB n=2000
88 Correct 18 ms 24524 KB n=2000
89 Correct 19 ms 24452 KB n=2000
90 Correct 18 ms 24460 KB n=2000
91 Correct 18 ms 24188 KB n=2000
92 Correct 699 ms 61256 KB n=200000
93 Correct 688 ms 80740 KB n=200000
94 Correct 679 ms 94136 KB n=200000
95 Correct 656 ms 60936 KB n=200000
96 Correct 635 ms 61096 KB n=200000
97 Correct 700 ms 77076 KB n=200000
98 Correct 668 ms 61080 KB n=200000
99 Correct 836 ms 62068 KB n=200000
100 Correct 669 ms 61120 KB n=200000
101 Correct 622 ms 99144 KB n=200000
102 Correct 429 ms 62960 KB n=200000
103 Correct 431 ms 62936 KB n=200000
104 Correct 419 ms 63020 KB n=200000
105 Correct 431 ms 63368 KB n=200000
106 Correct 450 ms 63440 KB n=200000
107 Correct 442 ms 63456 KB n=200000
108 Correct 841 ms 61892 KB n=200000
109 Correct 830 ms 61948 KB n=200000
110 Correct 801 ms 61872 KB n=200000
111 Correct 658 ms 60756 KB n=200000
112 Correct 641 ms 95228 KB n=200000
113 Correct 686 ms 77084 KB n=200000
114 Correct 714 ms 60600 KB n=200000
115 Correct 787 ms 68728 KB n=200000
116 Correct 638 ms 61116 KB n=200000
117 Correct 611 ms 97268 KB n=200000
118 Correct 752 ms 72972 KB n=200000
119 Correct 611 ms 61244 KB n=200000
120 Correct 564 ms 98628 KB n=200000
121 Correct 564 ms 98636 KB n=200000
122 Correct 566 ms 98884 KB n=200000
123 Correct 453 ms 63424 KB n=200000
124 Correct 178 ms 40132 KB n=25264