Submission #865523

# Submission time Handle Problem Language Result Execution time Memory
865523 2023-10-24T09:22:49 Z vjudge1 Birthday gift (IZhO18_treearray) C++17
56 / 100
4000 ms 52364 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];
}

void build(int v = 1, int tl = 1, int tr = m) {
	if (tl == tr) {
		t[v] = a[tl];
		return;
	}
	build(ls);
	build(rs);
	t[v] = lca(t[v << 1], t[v << 1 | 1]);
}

void upd(int pos, int val, int v = 1, int tl = 1, int tr = m) {
	if (pos == tl && tr == pos) {
		a[tl] = val;
		t[v] = val;
		return;
	}
	if (pos <= tm) upd(pos, val, ls);
	else upd(pos, val, rs);
	t[v] = lca(t[v << 1], t[v << 1 | 1]);
}

int get(int l, int r, int v = 1, int tl = 1, int tr = m) {
	if (l <= tl && tr <= r) return t[v];
	elif (r < tl || tr < l) return 0;
	return lca(get(l, r, ls), get(l, r, rs));
}

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];
	}
	build();
	while (q--) {
		int ty;
		cin >> ty;
		if (ty == 1) {
			int pos, val;
			cin >> pos >> val;
			upd(pos, val);
		} else {
			int l, r, v, ans1 = -1, ans2 = -1;
			cin >> l >> r >> v;
			for (int i = l; i <= r; i++) {
				if (a[i] == v) {
					ans1 = i;
					ans2 = i;
					break;
				}
				if (lca(a[i], v) != v) continue;
				int now = get(i, r);
				if (now == v) {
					ans1 = i;
					ans2 = r;
					break;
				}
				if (upper(v, now)) break;
				for (int j = i; j <= r; j++) {
//					cout << "Asd " << i << ' ' << j << ' ' << get(i, j) << '\n';
					int cc = get(i, j);
					if (cc == v) {
						ans1 = i;
						ans2 = j;
						break;
					}
					if (lca(cc, v) != v) break;
				}
			}
			cout << ans1 << ' ' << ans2 << '\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();
	}
}

Compilation message

treearray.cpp: In function 'void build(long long int, long long int, long long int)':
treearray.cpp:13:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   13 | #define tm (tl + tr >> 1)
      |             ~~~^~~~
treearray.cpp:14:24: note: in expansion of macro 'tm'
   14 | #define ls v << 1, tl, tm
      |                        ^~
treearray.cpp:78:8: note: in expansion of macro 'ls'
   78 |  build(ls);
      |        ^~
treearray.cpp:13:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   13 | #define tm (tl + tr >> 1)
      |             ~~~^~~~
treearray.cpp:15:24: note: in expansion of macro 'tm'
   15 | #define rs v << 1 | 1, tm + 1, tr
      |                        ^~
treearray.cpp:79:8: note: in expansion of macro 'rs'
   79 |  build(rs);
      |        ^~
treearray.cpp: In function 'void upd(long long int, long long int, long long int, long long int, long long int)':
treearray.cpp:13:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   13 | #define tm (tl + tr >> 1)
      |             ~~~^~~~
treearray.cpp:89:13: note: in expansion of macro 'tm'
   89 |  if (pos <= tm) upd(pos, val, ls);
      |             ^~
treearray.cpp:13:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   13 | #define tm (tl + tr >> 1)
      |             ~~~^~~~
treearray.cpp:14:24: note: in expansion of macro 'tm'
   14 | #define ls v << 1, tl, tm
      |                        ^~
treearray.cpp:89:31: note: in expansion of macro 'ls'
   89 |  if (pos <= tm) upd(pos, val, ls);
      |                               ^~
treearray.cpp:13:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   13 | #define tm (tl + tr >> 1)
      |             ~~~^~~~
treearray.cpp:15:24: note: in expansion of macro 'tm'
   15 | #define rs v << 1 | 1, tm + 1, tr
      |                        ^~
treearray.cpp:90:21: note: in expansion of macro 'rs'
   90 |  else upd(pos, val, rs);
      |                     ^~
treearray.cpp: In function 'long long int get(long long int, long long int, long long int, long long int, long long int)':
treearray.cpp:13:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   13 | #define tm (tl + tr >> 1)
      |             ~~~^~~~
treearray.cpp:14:24: note: in expansion of macro 'tm'
   14 | #define ls v << 1, tl, tm
      |                        ^~
treearray.cpp:97:23: note: in expansion of macro 'ls'
   97 |  return lca(get(l, r, ls), get(l, r, rs));
      |                       ^~
treearray.cpp:13:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   13 | #define tm (tl + tr >> 1)
      |             ~~~^~~~
treearray.cpp:15:24: note: in expansion of macro 'tm'
   15 | #define rs v << 1 | 1, tm + 1, tr
      |                        ^~
treearray.cpp:97:38: note: in expansion of macro 'rs'
   97 |  return lca(get(l, r, ls), get(l, r, rs));
      |                                      ^~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12892 KB n=5
2 Correct 3 ms 13044 KB n=100
3 Correct 3 ms 12892 KB n=100
4 Correct 2 ms 12888 KB n=100
5 Correct 2 ms 12892 KB n=100
6 Correct 3 ms 12892 KB n=100
7 Correct 3 ms 12888 KB n=100
8 Correct 3 ms 13144 KB n=100
9 Correct 2 ms 12892 KB n=100
10 Correct 3 ms 12892 KB n=100
11 Correct 3 ms 12892 KB n=100
12 Correct 2 ms 12892 KB n=100
13 Correct 3 ms 12892 KB n=100
14 Correct 2 ms 12892 KB n=100
15 Correct 2 ms 12892 KB n=100
16 Correct 2 ms 12892 KB n=100
17 Correct 3 ms 12888 KB n=100
18 Correct 3 ms 12892 KB n=100
19 Correct 2 ms 12892 KB n=100
20 Correct 3 ms 12892 KB n=100
21 Correct 2 ms 12892 KB n=100
22 Correct 2 ms 12892 KB n=100
23 Correct 2 ms 12888 KB n=100
24 Correct 2 ms 12892 KB n=100
25 Correct 2 ms 12888 KB n=100
26 Correct 2 ms 12892 KB n=12
27 Correct 3 ms 12892 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12892 KB n=5
2 Correct 3 ms 13044 KB n=100
3 Correct 3 ms 12892 KB n=100
4 Correct 2 ms 12888 KB n=100
5 Correct 2 ms 12892 KB n=100
6 Correct 3 ms 12892 KB n=100
7 Correct 3 ms 12888 KB n=100
8 Correct 3 ms 13144 KB n=100
9 Correct 2 ms 12892 KB n=100
10 Correct 3 ms 12892 KB n=100
11 Correct 3 ms 12892 KB n=100
12 Correct 2 ms 12892 KB n=100
13 Correct 3 ms 12892 KB n=100
14 Correct 2 ms 12892 KB n=100
15 Correct 2 ms 12892 KB n=100
16 Correct 2 ms 12892 KB n=100
17 Correct 3 ms 12888 KB n=100
18 Correct 3 ms 12892 KB n=100
19 Correct 2 ms 12892 KB n=100
20 Correct 3 ms 12892 KB n=100
21 Correct 2 ms 12892 KB n=100
22 Correct 2 ms 12892 KB n=100
23 Correct 2 ms 12888 KB n=100
24 Correct 2 ms 12892 KB n=100
25 Correct 2 ms 12888 KB n=100
26 Correct 2 ms 12892 KB n=12
27 Correct 3 ms 12892 KB n=100
28 Correct 3 ms 12892 KB n=500
29 Correct 15 ms 12988 KB n=500
30 Correct 25 ms 12964 KB n=500
31 Correct 20 ms 12892 KB n=500
32 Correct 3 ms 12892 KB n=500
33 Correct 21 ms 12892 KB n=500
34 Correct 3 ms 12888 KB n=500
35 Correct 19 ms 12892 KB n=500
36 Correct 46 ms 12940 KB n=500
37 Correct 35 ms 12888 KB n=500
38 Correct 46 ms 12888 KB n=500
39 Correct 17 ms 12892 KB n=500
40 Correct 13 ms 12888 KB n=500
41 Correct 12 ms 12892 KB n=500
42 Correct 21 ms 12888 KB n=500
43 Correct 22 ms 12892 KB n=500
44 Correct 21 ms 12964 KB n=500
45 Correct 3 ms 12888 KB n=500
46 Correct 19 ms 12892 KB n=500
47 Correct 13 ms 12976 KB n=500
48 Correct 2 ms 12892 KB n=500
49 Correct 11 ms 12892 KB n=500
50 Correct 3 ms 12892 KB n=500
51 Correct 17 ms 12888 KB n=500
52 Correct 19 ms 12892 KB n=500
53 Correct 11 ms 12892 KB n=500
54 Correct 3 ms 12892 KB n=500
55 Correct 8 ms 12892 KB n=278
56 Correct 3 ms 12892 KB n=500
57 Correct 2 ms 12892 KB n=500
58 Correct 6 ms 12888 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12892 KB n=5
2 Correct 3 ms 13044 KB n=100
3 Correct 3 ms 12892 KB n=100
4 Correct 2 ms 12888 KB n=100
5 Correct 2 ms 12892 KB n=100
6 Correct 3 ms 12892 KB n=100
7 Correct 3 ms 12888 KB n=100
8 Correct 3 ms 13144 KB n=100
9 Correct 2 ms 12892 KB n=100
10 Correct 3 ms 12892 KB n=100
11 Correct 3 ms 12892 KB n=100
12 Correct 2 ms 12892 KB n=100
13 Correct 3 ms 12892 KB n=100
14 Correct 2 ms 12892 KB n=100
15 Correct 2 ms 12892 KB n=100
16 Correct 2 ms 12892 KB n=100
17 Correct 3 ms 12888 KB n=100
18 Correct 3 ms 12892 KB n=100
19 Correct 2 ms 12892 KB n=100
20 Correct 3 ms 12892 KB n=100
21 Correct 2 ms 12892 KB n=100
22 Correct 2 ms 12892 KB n=100
23 Correct 2 ms 12888 KB n=100
24 Correct 2 ms 12892 KB n=100
25 Correct 2 ms 12888 KB n=100
26 Correct 2 ms 12892 KB n=12
27 Correct 3 ms 12892 KB n=100
28 Correct 3 ms 12892 KB n=500
29 Correct 15 ms 12988 KB n=500
30 Correct 25 ms 12964 KB n=500
31 Correct 20 ms 12892 KB n=500
32 Correct 3 ms 12892 KB n=500
33 Correct 21 ms 12892 KB n=500
34 Correct 3 ms 12888 KB n=500
35 Correct 19 ms 12892 KB n=500
36 Correct 46 ms 12940 KB n=500
37 Correct 35 ms 12888 KB n=500
38 Correct 46 ms 12888 KB n=500
39 Correct 17 ms 12892 KB n=500
40 Correct 13 ms 12888 KB n=500
41 Correct 12 ms 12892 KB n=500
42 Correct 21 ms 12888 KB n=500
43 Correct 22 ms 12892 KB n=500
44 Correct 21 ms 12964 KB n=500
45 Correct 3 ms 12888 KB n=500
46 Correct 19 ms 12892 KB n=500
47 Correct 13 ms 12976 KB n=500
48 Correct 2 ms 12892 KB n=500
49 Correct 11 ms 12892 KB n=500
50 Correct 3 ms 12892 KB n=500
51 Correct 17 ms 12888 KB n=500
52 Correct 19 ms 12892 KB n=500
53 Correct 11 ms 12892 KB n=500
54 Correct 3 ms 12892 KB n=500
55 Correct 8 ms 12892 KB n=278
56 Correct 3 ms 12892 KB n=500
57 Correct 2 ms 12892 KB n=500
58 Correct 6 ms 12888 KB n=500
59 Correct 4 ms 12892 KB n=2000
60 Correct 374 ms 13076 KB n=2000
61 Correct 349 ms 13044 KB n=2000
62 Correct 502 ms 12892 KB n=2000
63 Correct 6 ms 12892 KB n=2000
64 Correct 432 ms 13048 KB n=2000
65 Correct 5 ms 12888 KB n=2000
66 Correct 469 ms 13060 KB n=2000
67 Correct 48 ms 12892 KB n=2000
68 Correct 396 ms 12888 KB n=2000
69 Correct 684 ms 12992 KB n=2000
70 Correct 685 ms 13008 KB n=2000
71 Correct 684 ms 13144 KB n=2000
72 Correct 191 ms 12888 KB n=2000
73 Correct 199 ms 13136 KB n=2000
74 Correct 4 ms 12892 KB n=1844
75 Correct 195 ms 13016 KB n=2000
76 Correct 392 ms 12892 KB n=2000
77 Correct 377 ms 12996 KB n=2000
78 Correct 403 ms 13140 KB n=2000
79 Correct 4 ms 12888 KB n=2000
80 Correct 258 ms 12892 KB n=2000
81 Correct 343 ms 12892 KB n=2000
82 Correct 3 ms 12888 KB n=2000
83 Correct 270 ms 13048 KB n=2000
84 Correct 16 ms 12888 KB n=2000
85 Correct 372 ms 13148 KB n=2000
86 Correct 214 ms 13016 KB n=2000
87 Correct 18 ms 12892 KB n=2000
88 Correct 5 ms 12888 KB n=2000
89 Correct 4 ms 12888 KB n=2000
90 Correct 4 ms 12892 KB n=2000
91 Correct 61 ms 13020 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12892 KB n=5
2 Correct 3 ms 13044 KB n=100
3 Correct 3 ms 12892 KB n=100
4 Correct 2 ms 12888 KB n=100
5 Correct 2 ms 12892 KB n=100
6 Correct 3 ms 12892 KB n=100
7 Correct 3 ms 12888 KB n=100
8 Correct 3 ms 13144 KB n=100
9 Correct 2 ms 12892 KB n=100
10 Correct 3 ms 12892 KB n=100
11 Correct 3 ms 12892 KB n=100
12 Correct 2 ms 12892 KB n=100
13 Correct 3 ms 12892 KB n=100
14 Correct 2 ms 12892 KB n=100
15 Correct 2 ms 12892 KB n=100
16 Correct 2 ms 12892 KB n=100
17 Correct 3 ms 12888 KB n=100
18 Correct 3 ms 12892 KB n=100
19 Correct 2 ms 12892 KB n=100
20 Correct 3 ms 12892 KB n=100
21 Correct 2 ms 12892 KB n=100
22 Correct 2 ms 12892 KB n=100
23 Correct 2 ms 12888 KB n=100
24 Correct 2 ms 12892 KB n=100
25 Correct 2 ms 12888 KB n=100
26 Correct 2 ms 12892 KB n=12
27 Correct 3 ms 12892 KB n=100
28 Correct 3 ms 12892 KB n=500
29 Correct 15 ms 12988 KB n=500
30 Correct 25 ms 12964 KB n=500
31 Correct 20 ms 12892 KB n=500
32 Correct 3 ms 12892 KB n=500
33 Correct 21 ms 12892 KB n=500
34 Correct 3 ms 12888 KB n=500
35 Correct 19 ms 12892 KB n=500
36 Correct 46 ms 12940 KB n=500
37 Correct 35 ms 12888 KB n=500
38 Correct 46 ms 12888 KB n=500
39 Correct 17 ms 12892 KB n=500
40 Correct 13 ms 12888 KB n=500
41 Correct 12 ms 12892 KB n=500
42 Correct 21 ms 12888 KB n=500
43 Correct 22 ms 12892 KB n=500
44 Correct 21 ms 12964 KB n=500
45 Correct 3 ms 12888 KB n=500
46 Correct 19 ms 12892 KB n=500
47 Correct 13 ms 12976 KB n=500
48 Correct 2 ms 12892 KB n=500
49 Correct 11 ms 12892 KB n=500
50 Correct 3 ms 12892 KB n=500
51 Correct 17 ms 12888 KB n=500
52 Correct 19 ms 12892 KB n=500
53 Correct 11 ms 12892 KB n=500
54 Correct 3 ms 12892 KB n=500
55 Correct 8 ms 12892 KB n=278
56 Correct 3 ms 12892 KB n=500
57 Correct 2 ms 12892 KB n=500
58 Correct 6 ms 12888 KB n=500
59 Correct 4 ms 12892 KB n=2000
60 Correct 374 ms 13076 KB n=2000
61 Correct 349 ms 13044 KB n=2000
62 Correct 502 ms 12892 KB n=2000
63 Correct 6 ms 12892 KB n=2000
64 Correct 432 ms 13048 KB n=2000
65 Correct 5 ms 12888 KB n=2000
66 Correct 469 ms 13060 KB n=2000
67 Correct 48 ms 12892 KB n=2000
68 Correct 396 ms 12888 KB n=2000
69 Correct 684 ms 12992 KB n=2000
70 Correct 685 ms 13008 KB n=2000
71 Correct 684 ms 13144 KB n=2000
72 Correct 191 ms 12888 KB n=2000
73 Correct 199 ms 13136 KB n=2000
74 Correct 4 ms 12892 KB n=1844
75 Correct 195 ms 13016 KB n=2000
76 Correct 392 ms 12892 KB n=2000
77 Correct 377 ms 12996 KB n=2000
78 Correct 403 ms 13140 KB n=2000
79 Correct 4 ms 12888 KB n=2000
80 Correct 258 ms 12892 KB n=2000
81 Correct 343 ms 12892 KB n=2000
82 Correct 3 ms 12888 KB n=2000
83 Correct 270 ms 13048 KB n=2000
84 Correct 16 ms 12888 KB n=2000
85 Correct 372 ms 13148 KB n=2000
86 Correct 214 ms 13016 KB n=2000
87 Correct 18 ms 12892 KB n=2000
88 Correct 5 ms 12888 KB n=2000
89 Correct 4 ms 12888 KB n=2000
90 Correct 4 ms 12892 KB n=2000
91 Correct 61 ms 13020 KB n=2000
92 Execution timed out 4100 ms 52364 KB Time limit exceeded
93 Halted 0 ms 0 KB -