Submission #892940

# Submission time Handle Problem Language Result Execution time Memory
892940 2023-12-26T08:12:46 Z vjudge1 Birthday gift (IZhO18_treearray) C++17
100 / 100
1028 ms 162368 KB
#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;

//#pragma GCC optimization("g", on)
//#pragma GCC optimization("03")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("inline")
//#pragma GCC optimize("-fgcse,-fgcse-lm")
//#pragma GCC optimize("-ftree-pre,-ftree-vrp")
//#pragma GCC optimize("-ffast-math")
//#pragma GCC optimize("-fipa-sra")
//#pragma GCC optimize("-fpeephole2")
//#pragma GCC optimize("-fsched-spec")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
//#pragma GCC optimize("unroll-loops")

#define aboba ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define br break;
#define sp " "
#define en "\n"
#define pb push_back
#define sz(a) int(a.size())
#define bg begin()
#define ed end()
#define in insert
#define ss second
#define ff first
#define setp(a) cout << fixed; cout << setprecision(a);
#define all(v) v.begin(), v.end()
 
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef double db;
typedef tree<
    long long,
    null_type,
    less_equal<long long>,
    rb_tree_tag,
    tree_order_statistics_node_update> orset;

void freopen(string s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); }
ll bp(ll x, ll y, ll z) { ll res = 1; while (y) { if (y & 1) { res = (res * x) % z; y--; } x = (x * x) % z; y >>= 1; } return res; }
// C(n, k) = ((fact[n] * bp(fact[k], mod - 2)) % mod * bp(fact[n - k], mod - 2)) % mod;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll lcm(ll a, ll b) { return (a / __gcd(a, b)) * b; }

const ll N = 1e6 + 11;
const ll inf = 1e18 + 7;
ll tt = 1;
ll n, m, q;
vector<ll> g[N];
ll a[N];
ll d[N], up[N][22];
multiset<pll> st[N];

void dfs(ll v, ll p) {
	d[v] = d[p] + 1;
	up[v][0] = p;
	for (int i = 1;i <= 20;i++) up[v][i] = up[up[v][i - 1]][i - 1];
	for (auto to : g[v]) {
		if (to == p) continue;
		dfs(to, v);
	}
}

ll lca(ll x, ll y) {
	if (d[x] < d[y]) swap(x, y);
	ll k = d[x] - d[y];
	for (int i = 20;i >= 0;i--) {
		if (k & (1 << i)) {
			x = up[x][i];
		}
	}
	if (x == y) return x;
	for (int i = 20;i >= 0;i--) {
		if (up[x][i] != up[y][i]) {
			x = up[x][i];
			y = up[y][i];
		}
	}
	return up[x][0];
}

void solve() {
	cin >> n >> m >> q;
	for (int i = 1;i < n;i++) {
		ll u, v; cin >> u >> v;
		g[u].pb(v);
		g[v].pb(u);
	}
	dfs(1, 0);
	for (int i = 1;i <= m;i++) cin >> a[i];
	for (int i = 1;i <= m;i++) {
		st[a[i]].insert({i, i});
		if (i < m) st[lca(a[i], a[i + 1])].insert({i, i + 1});
	}
	while (q--) {
		ll tp, l, r; cin >> tp >> l >> r;
		if (tp == 1) {
			st[a[l]].erase({l, l});
			st[lca(a[l - 1], a[l])].erase({l - 1, l});
			if (l < m) st[lca(a[l], a[l + 1])].erase({l, l + 1});
			a[l] = r;
			st[a[l]].insert({l, l});
			st[lca(a[l - 1], a[l])].insert({l - 1, l});
			if (l < m) st[lca(a[l], a[l + 1])].insert({l, l + 1});
		} else {
			ll v; cin >> v;
			pll p = {l, l};
			auto to = st[v].lower_bound(p);
			if (to != st[v].ed) {
				p = *to;
				if (p.ff >= l && p.ss <= r) {
					cout << p.ff << sp << p.ss << en;
				} else cout << -1 << sp << -1 << en;
			} else cout << -1 << sp << -1 << en;
		}
	}
}
   
int main() {      
    aboba    
//    freopen("cownomics");
//    precalc();
//    cin >> tt;
    for (int i = 1;i <= tt;i++) {
		solve();
    }
}

Compilation message

treearray.cpp: In function 'void freopen(std::string)':
treearray.cpp:47:33: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 | void freopen(string s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); }
      |                          ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:47:75: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 | void freopen(string s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); }
      |                                                                    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 16 ms 74072 KB n=5
2 Correct 17 ms 74328 KB n=100
3 Correct 15 ms 74072 KB n=100
4 Correct 15 ms 74076 KB n=100
5 Correct 15 ms 74076 KB n=100
6 Correct 15 ms 74076 KB n=100
7 Correct 15 ms 74072 KB n=100
8 Correct 15 ms 73916 KB n=100
9 Correct 15 ms 74076 KB n=100
10 Correct 17 ms 74332 KB n=100
11 Correct 15 ms 74076 KB n=100
12 Correct 16 ms 74076 KB n=100
13 Correct 15 ms 74076 KB n=100
14 Correct 16 ms 74072 KB n=100
15 Correct 16 ms 74076 KB n=100
16 Correct 15 ms 74008 KB n=100
17 Correct 15 ms 74076 KB n=100
18 Correct 18 ms 74072 KB n=100
19 Correct 15 ms 74076 KB n=100
20 Correct 16 ms 74072 KB n=100
21 Correct 15 ms 74076 KB n=100
22 Correct 15 ms 74076 KB n=100
23 Correct 15 ms 74076 KB n=100
24 Correct 15 ms 74076 KB n=100
25 Correct 16 ms 74040 KB n=100
26 Correct 15 ms 74076 KB n=12
27 Correct 15 ms 74120 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 16 ms 74072 KB n=5
2 Correct 17 ms 74328 KB n=100
3 Correct 15 ms 74072 KB n=100
4 Correct 15 ms 74076 KB n=100
5 Correct 15 ms 74076 KB n=100
6 Correct 15 ms 74076 KB n=100
7 Correct 15 ms 74072 KB n=100
8 Correct 15 ms 73916 KB n=100
9 Correct 15 ms 74076 KB n=100
10 Correct 17 ms 74332 KB n=100
11 Correct 15 ms 74076 KB n=100
12 Correct 16 ms 74076 KB n=100
13 Correct 15 ms 74076 KB n=100
14 Correct 16 ms 74072 KB n=100
15 Correct 16 ms 74076 KB n=100
16 Correct 15 ms 74008 KB n=100
17 Correct 15 ms 74076 KB n=100
18 Correct 18 ms 74072 KB n=100
19 Correct 15 ms 74076 KB n=100
20 Correct 16 ms 74072 KB n=100
21 Correct 15 ms 74076 KB n=100
22 Correct 15 ms 74076 KB n=100
23 Correct 15 ms 74076 KB n=100
24 Correct 15 ms 74076 KB n=100
25 Correct 16 ms 74040 KB n=100
26 Correct 15 ms 74076 KB n=12
27 Correct 15 ms 74120 KB n=100
28 Correct 16 ms 74076 KB n=500
29 Correct 15 ms 74072 KB n=500
30 Correct 16 ms 74076 KB n=500
31 Correct 16 ms 74004 KB n=500
32 Correct 17 ms 74016 KB n=500
33 Correct 15 ms 74076 KB n=500
34 Correct 16 ms 74192 KB n=500
35 Correct 16 ms 74076 KB n=500
36 Correct 15 ms 74072 KB n=500
37 Correct 16 ms 74076 KB n=500
38 Correct 15 ms 74072 KB n=500
39 Correct 15 ms 74172 KB n=500
40 Correct 15 ms 74076 KB n=500
41 Correct 16 ms 74072 KB n=500
42 Correct 16 ms 74080 KB n=500
43 Correct 18 ms 74076 KB n=500
44 Correct 16 ms 74076 KB n=500
45 Correct 16 ms 74072 KB n=500
46 Correct 17 ms 74072 KB n=500
47 Correct 16 ms 74076 KB n=500
48 Correct 16 ms 74064 KB n=500
49 Correct 15 ms 74072 KB n=500
50 Correct 15 ms 74076 KB n=500
51 Correct 16 ms 74072 KB n=500
52 Correct 15 ms 74212 KB n=500
53 Correct 16 ms 74076 KB n=500
54 Correct 16 ms 74072 KB n=500
55 Correct 15 ms 74072 KB n=278
56 Correct 15 ms 74080 KB n=500
57 Correct 15 ms 74072 KB n=500
58 Correct 15 ms 74076 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 16 ms 74072 KB n=5
2 Correct 17 ms 74328 KB n=100
3 Correct 15 ms 74072 KB n=100
4 Correct 15 ms 74076 KB n=100
5 Correct 15 ms 74076 KB n=100
6 Correct 15 ms 74076 KB n=100
7 Correct 15 ms 74072 KB n=100
8 Correct 15 ms 73916 KB n=100
9 Correct 15 ms 74076 KB n=100
10 Correct 17 ms 74332 KB n=100
11 Correct 15 ms 74076 KB n=100
12 Correct 16 ms 74076 KB n=100
13 Correct 15 ms 74076 KB n=100
14 Correct 16 ms 74072 KB n=100
15 Correct 16 ms 74076 KB n=100
16 Correct 15 ms 74008 KB n=100
17 Correct 15 ms 74076 KB n=100
18 Correct 18 ms 74072 KB n=100
19 Correct 15 ms 74076 KB n=100
20 Correct 16 ms 74072 KB n=100
21 Correct 15 ms 74076 KB n=100
22 Correct 15 ms 74076 KB n=100
23 Correct 15 ms 74076 KB n=100
24 Correct 15 ms 74076 KB n=100
25 Correct 16 ms 74040 KB n=100
26 Correct 15 ms 74076 KB n=12
27 Correct 15 ms 74120 KB n=100
28 Correct 16 ms 74076 KB n=500
29 Correct 15 ms 74072 KB n=500
30 Correct 16 ms 74076 KB n=500
31 Correct 16 ms 74004 KB n=500
32 Correct 17 ms 74016 KB n=500
33 Correct 15 ms 74076 KB n=500
34 Correct 16 ms 74192 KB n=500
35 Correct 16 ms 74076 KB n=500
36 Correct 15 ms 74072 KB n=500
37 Correct 16 ms 74076 KB n=500
38 Correct 15 ms 74072 KB n=500
39 Correct 15 ms 74172 KB n=500
40 Correct 15 ms 74076 KB n=500
41 Correct 16 ms 74072 KB n=500
42 Correct 16 ms 74080 KB n=500
43 Correct 18 ms 74076 KB n=500
44 Correct 16 ms 74076 KB n=500
45 Correct 16 ms 74072 KB n=500
46 Correct 17 ms 74072 KB n=500
47 Correct 16 ms 74076 KB n=500
48 Correct 16 ms 74064 KB n=500
49 Correct 15 ms 74072 KB n=500
50 Correct 15 ms 74076 KB n=500
51 Correct 16 ms 74072 KB n=500
52 Correct 15 ms 74212 KB n=500
53 Correct 16 ms 74076 KB n=500
54 Correct 16 ms 74072 KB n=500
55 Correct 15 ms 74072 KB n=278
56 Correct 15 ms 74080 KB n=500
57 Correct 15 ms 74072 KB n=500
58 Correct 15 ms 74076 KB n=500
59 Correct 19 ms 74332 KB n=2000
60 Correct 17 ms 74332 KB n=2000
61 Correct 21 ms 74332 KB n=2000
62 Correct 18 ms 74464 KB n=2000
63 Correct 19 ms 74332 KB n=2000
64 Correct 19 ms 74328 KB n=2000
65 Correct 21 ms 74332 KB n=2000
66 Correct 18 ms 74328 KB n=2000
67 Correct 18 ms 74332 KB n=2000
68 Correct 18 ms 74328 KB n=2000
69 Correct 17 ms 74332 KB n=2000
70 Correct 17 ms 74328 KB n=2000
71 Correct 18 ms 74532 KB n=2000
72 Correct 17 ms 74332 KB n=2000
73 Correct 17 ms 74332 KB n=2000
74 Correct 18 ms 74328 KB n=1844
75 Correct 17 ms 74328 KB n=2000
76 Correct 19 ms 74332 KB n=2000
77 Correct 18 ms 74332 KB n=2000
78 Correct 18 ms 74332 KB n=2000
79 Correct 18 ms 74328 KB n=2000
80 Correct 20 ms 74328 KB n=2000
81 Correct 18 ms 74328 KB n=2000
82 Correct 20 ms 74584 KB n=2000
83 Correct 18 ms 74332 KB n=2000
84 Correct 18 ms 74332 KB n=2000
85 Correct 18 ms 74460 KB n=2000
86 Correct 18 ms 74332 KB n=2000
87 Correct 18 ms 74328 KB n=2000
88 Correct 18 ms 74328 KB n=2000
89 Correct 19 ms 74328 KB n=2000
90 Correct 17 ms 74328 KB n=2000
91 Correct 17 ms 74316 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 16 ms 74072 KB n=5
2 Correct 17 ms 74328 KB n=100
3 Correct 15 ms 74072 KB n=100
4 Correct 15 ms 74076 KB n=100
5 Correct 15 ms 74076 KB n=100
6 Correct 15 ms 74076 KB n=100
7 Correct 15 ms 74072 KB n=100
8 Correct 15 ms 73916 KB n=100
9 Correct 15 ms 74076 KB n=100
10 Correct 17 ms 74332 KB n=100
11 Correct 15 ms 74076 KB n=100
12 Correct 16 ms 74076 KB n=100
13 Correct 15 ms 74076 KB n=100
14 Correct 16 ms 74072 KB n=100
15 Correct 16 ms 74076 KB n=100
16 Correct 15 ms 74008 KB n=100
17 Correct 15 ms 74076 KB n=100
18 Correct 18 ms 74072 KB n=100
19 Correct 15 ms 74076 KB n=100
20 Correct 16 ms 74072 KB n=100
21 Correct 15 ms 74076 KB n=100
22 Correct 15 ms 74076 KB n=100
23 Correct 15 ms 74076 KB n=100
24 Correct 15 ms 74076 KB n=100
25 Correct 16 ms 74040 KB n=100
26 Correct 15 ms 74076 KB n=12
27 Correct 15 ms 74120 KB n=100
28 Correct 16 ms 74076 KB n=500
29 Correct 15 ms 74072 KB n=500
30 Correct 16 ms 74076 KB n=500
31 Correct 16 ms 74004 KB n=500
32 Correct 17 ms 74016 KB n=500
33 Correct 15 ms 74076 KB n=500
34 Correct 16 ms 74192 KB n=500
35 Correct 16 ms 74076 KB n=500
36 Correct 15 ms 74072 KB n=500
37 Correct 16 ms 74076 KB n=500
38 Correct 15 ms 74072 KB n=500
39 Correct 15 ms 74172 KB n=500
40 Correct 15 ms 74076 KB n=500
41 Correct 16 ms 74072 KB n=500
42 Correct 16 ms 74080 KB n=500
43 Correct 18 ms 74076 KB n=500
44 Correct 16 ms 74076 KB n=500
45 Correct 16 ms 74072 KB n=500
46 Correct 17 ms 74072 KB n=500
47 Correct 16 ms 74076 KB n=500
48 Correct 16 ms 74064 KB n=500
49 Correct 15 ms 74072 KB n=500
50 Correct 15 ms 74076 KB n=500
51 Correct 16 ms 74072 KB n=500
52 Correct 15 ms 74212 KB n=500
53 Correct 16 ms 74076 KB n=500
54 Correct 16 ms 74072 KB n=500
55 Correct 15 ms 74072 KB n=278
56 Correct 15 ms 74080 KB n=500
57 Correct 15 ms 74072 KB n=500
58 Correct 15 ms 74076 KB n=500
59 Correct 19 ms 74332 KB n=2000
60 Correct 17 ms 74332 KB n=2000
61 Correct 21 ms 74332 KB n=2000
62 Correct 18 ms 74464 KB n=2000
63 Correct 19 ms 74332 KB n=2000
64 Correct 19 ms 74328 KB n=2000
65 Correct 21 ms 74332 KB n=2000
66 Correct 18 ms 74328 KB n=2000
67 Correct 18 ms 74332 KB n=2000
68 Correct 18 ms 74328 KB n=2000
69 Correct 17 ms 74332 KB n=2000
70 Correct 17 ms 74328 KB n=2000
71 Correct 18 ms 74532 KB n=2000
72 Correct 17 ms 74332 KB n=2000
73 Correct 17 ms 74332 KB n=2000
74 Correct 18 ms 74328 KB n=1844
75 Correct 17 ms 74328 KB n=2000
76 Correct 19 ms 74332 KB n=2000
77 Correct 18 ms 74332 KB n=2000
78 Correct 18 ms 74332 KB n=2000
79 Correct 18 ms 74328 KB n=2000
80 Correct 20 ms 74328 KB n=2000
81 Correct 18 ms 74328 KB n=2000
82 Correct 20 ms 74584 KB n=2000
83 Correct 18 ms 74332 KB n=2000
84 Correct 18 ms 74332 KB n=2000
85 Correct 18 ms 74460 KB n=2000
86 Correct 18 ms 74332 KB n=2000
87 Correct 18 ms 74328 KB n=2000
88 Correct 18 ms 74328 KB n=2000
89 Correct 19 ms 74328 KB n=2000
90 Correct 17 ms 74328 KB n=2000
91 Correct 17 ms 74316 KB n=2000
92 Correct 640 ms 147664 KB n=200000
93 Correct 922 ms 155040 KB n=200000
94 Correct 867 ms 161376 KB n=200000
95 Correct 655 ms 153532 KB n=200000
96 Correct 625 ms 153788 KB n=200000
97 Correct 954 ms 157580 KB n=200000
98 Correct 653 ms 153720 KB n=200000
99 Correct 783 ms 154396 KB n=200000
100 Correct 648 ms 153732 KB n=200000
101 Correct 869 ms 162368 KB n=200000
102 Correct 369 ms 154976 KB n=200000
103 Correct 361 ms 155008 KB n=200000
104 Correct 368 ms 154964 KB n=200000
105 Correct 364 ms 155184 KB n=200000
106 Correct 360 ms 155420 KB n=200000
107 Correct 380 ms 155464 KB n=200000
108 Correct 742 ms 153888 KB n=200000
109 Correct 714 ms 153616 KB n=200000
110 Correct 721 ms 153784 KB n=200000
111 Correct 658 ms 153240 KB n=200000
112 Correct 882 ms 161700 KB n=200000
113 Correct 990 ms 158060 KB n=200000
114 Correct 679 ms 152932 KB n=200000
115 Correct 948 ms 155496 KB n=200000
116 Correct 645 ms 153664 KB n=200000
117 Correct 929 ms 162144 KB n=200000
118 Correct 1028 ms 156604 KB n=200000
119 Correct 628 ms 153708 KB n=200000
120 Correct 857 ms 161524 KB n=200000
121 Correct 836 ms 161384 KB n=200000
122 Correct 832 ms 161768 KB n=200000
123 Correct 388 ms 155104 KB n=200000
124 Correct 177 ms 93524 KB n=25264