Submission #1058754

# Submission time Handle Problem Language Result Execution time Memory
1058754 2024-08-14T13:21:00 Z Beerus13 Birthday gift (IZhO18_treearray) C++14
100 / 100
496 ms 122452 KB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MIN_HIGH(x, t) (H[x] < H[t] ? (x) : (t))
#define Mask(i) (1 << i)
const int N = 2e5 + 5;

int n, m, q, cnt, a[N];
int H[N], P[N << 1][19], nodes[N << 1], pos[N];
vector<int> g[N];
set<int> sett[N], Pos[N];

void dfs(int u, int p = 0) {
	nodes[++cnt] = u;
	pos[u] = cnt;
	for (int v : g[u]) if(v != p) {
		H[v] = H[u] + 1;
		dfs(v, u);
		nodes[++cnt] = u;
	}
}

int lca(int u, int v) {
	int l = pos[u], r = pos[v];
	if (l > r) swap(l, r);
	int k = 31 - __builtin_clz(r - l + 1);
	return MIN_HIGH(P[l][k], P[r - Mask(k) + 1][k]);
}

void solve() {
    cin >> n >> m >> q;
    for(int i = 1, u, v; i < n; ++i) {
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs(1);
    for(int i = 1; i <= cnt; ++i) P[i][0] = nodes[i];
    for(int j = 1; j < 19; ++j) for(int i = 1; i <= cnt - Mask(j) + 1; ++i)
            P[i][j] = MIN_HIGH(P[i][j - 1], P[i + Mask(j - 1)][j - 1]);
    for(int i = 1; i <= m; ++i) {
        cin >> a[i];
        Pos[a[i]].insert(i);
        if(i > 1) sett[lca(a[i], a[i - 1])].insert(i);
    }
    for(int i = 1; i <= n; ++i) {
        Pos[i].insert(m + 1);
        sett[i].insert(m + 1);
    }
    while(q--) {
        int type, l, r, u; cin >> type >> l >> r;
        if(type == 1) {
            Pos[a[l]].erase(l);
            Pos[r].insert(l);
            if(l > 1) {
                sett[lca(a[l], a[l - 1])].erase(l);
                sett[lca(r, a[l - 1])].insert(l);
            }
            if(l < m) {
                sett[lca(a[l], a[l + 1])].erase(l + 1);
                sett[lca(r, a[l + 1])].insert(l + 1);
            }
            a[l] = r;
        }
        else {
            cin >> u;
            int res = *Pos[u].lower_bound(l);
            if(res <= r) cout << res << ' ' << res << '\n';
            else {
                res = *sett[u].upper_bound(l);
                if(res <= r) cout << res - 1 << ' ' << res << '\n';
                else cout << -1 << ' ' << -1 << '\n';
            }
        }
    }
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int test = 1;
    // cin >> test;
    while(test--) solve();
    return 0;
}

Compilation message

treearray.cpp: In function 'void solve()':
treearray.cpp:40:58: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   40 |             P[i][j] = MIN_HIGH(P[i][j - 1], P[i + Mask(j - 1)][j - 1]);
      |                                                        ~~^~~
treearray.cpp:4:34: note: in definition of macro 'MIN_HIGH'
    4 | #define MIN_HIGH(x, t) (H[x] < H[t] ? (x) : (t))
      |                                  ^
treearray.cpp:40:51: note: in expansion of macro 'Mask'
   40 |             P[i][j] = MIN_HIGH(P[i][j - 1], P[i + Mask(j - 1)][j - 1]);
      |                                                   ^~~~
treearray.cpp:40:58: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   40 |             P[i][j] = MIN_HIGH(P[i][j - 1], P[i + Mask(j - 1)][j - 1]);
      |                                                        ~~^~~
treearray.cpp:4:46: note: in definition of macro 'MIN_HIGH'
    4 | #define MIN_HIGH(x, t) (H[x] < H[t] ? (x) : (t))
      |                                              ^
treearray.cpp:40:51: note: in expansion of macro 'Mask'
   40 |             P[i][j] = MIN_HIGH(P[i][j - 1], P[i + Mask(j - 1)][j - 1]);
      |                                                   ^~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 27228 KB n=5
2 Correct 3 ms 27228 KB n=100
3 Correct 5 ms 27228 KB n=100
4 Correct 5 ms 27228 KB n=100
5 Correct 4 ms 27312 KB n=100
6 Correct 3 ms 27228 KB n=100
7 Correct 5 ms 27228 KB n=100
8 Correct 3 ms 27228 KB n=100
9 Correct 3 ms 27228 KB n=100
10 Correct 3 ms 27304 KB n=100
11 Correct 4 ms 27228 KB n=100
12 Correct 4 ms 27228 KB n=100
13 Correct 4 ms 27228 KB n=100
14 Correct 4 ms 27228 KB n=100
15 Correct 3 ms 27228 KB n=100
16 Correct 3 ms 27228 KB n=100
17 Correct 4 ms 27228 KB n=100
18 Correct 4 ms 27228 KB n=100
19 Correct 3 ms 27228 KB n=100
20 Correct 3 ms 27228 KB n=100
21 Correct 4 ms 27484 KB n=100
22 Correct 4 ms 27228 KB n=100
23 Correct 4 ms 27228 KB n=100
24 Correct 3 ms 27312 KB n=100
25 Correct 3 ms 27348 KB n=100
26 Correct 3 ms 27304 KB n=12
27 Correct 4 ms 27228 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 3 ms 27228 KB n=5
2 Correct 3 ms 27228 KB n=100
3 Correct 5 ms 27228 KB n=100
4 Correct 5 ms 27228 KB n=100
5 Correct 4 ms 27312 KB n=100
6 Correct 3 ms 27228 KB n=100
7 Correct 5 ms 27228 KB n=100
8 Correct 3 ms 27228 KB n=100
9 Correct 3 ms 27228 KB n=100
10 Correct 3 ms 27304 KB n=100
11 Correct 4 ms 27228 KB n=100
12 Correct 4 ms 27228 KB n=100
13 Correct 4 ms 27228 KB n=100
14 Correct 4 ms 27228 KB n=100
15 Correct 3 ms 27228 KB n=100
16 Correct 3 ms 27228 KB n=100
17 Correct 4 ms 27228 KB n=100
18 Correct 4 ms 27228 KB n=100
19 Correct 3 ms 27228 KB n=100
20 Correct 3 ms 27228 KB n=100
21 Correct 4 ms 27484 KB n=100
22 Correct 4 ms 27228 KB n=100
23 Correct 4 ms 27228 KB n=100
24 Correct 3 ms 27312 KB n=100
25 Correct 3 ms 27348 KB n=100
26 Correct 3 ms 27304 KB n=12
27 Correct 4 ms 27228 KB n=100
28 Correct 4 ms 27316 KB n=500
29 Correct 4 ms 27228 KB n=500
30 Correct 4 ms 27228 KB n=500
31 Correct 4 ms 27344 KB n=500
32 Correct 4 ms 27228 KB n=500
33 Correct 4 ms 27228 KB n=500
34 Correct 3 ms 27228 KB n=500
35 Correct 4 ms 27480 KB n=500
36 Correct 4 ms 27228 KB n=500
37 Correct 4 ms 27228 KB n=500
38 Correct 4 ms 27316 KB n=500
39 Correct 4 ms 27228 KB n=500
40 Correct 3 ms 27228 KB n=500
41 Correct 4 ms 27228 KB n=500
42 Correct 4 ms 27228 KB n=500
43 Correct 4 ms 27228 KB n=500
44 Correct 4 ms 27480 KB n=500
45 Correct 5 ms 27452 KB n=500
46 Correct 4 ms 27228 KB n=500
47 Correct 5 ms 27228 KB n=500
48 Correct 4 ms 27228 KB n=500
49 Correct 5 ms 27228 KB n=500
50 Correct 4 ms 27228 KB n=500
51 Correct 4 ms 27228 KB n=500
52 Correct 5 ms 27476 KB n=500
53 Correct 4 ms 27228 KB n=500
54 Correct 6 ms 27228 KB n=500
55 Correct 5 ms 27228 KB n=278
56 Correct 4 ms 27228 KB n=500
57 Correct 4 ms 27228 KB n=500
58 Correct 5 ms 27228 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 3 ms 27228 KB n=5
2 Correct 3 ms 27228 KB n=100
3 Correct 5 ms 27228 KB n=100
4 Correct 5 ms 27228 KB n=100
5 Correct 4 ms 27312 KB n=100
6 Correct 3 ms 27228 KB n=100
7 Correct 5 ms 27228 KB n=100
8 Correct 3 ms 27228 KB n=100
9 Correct 3 ms 27228 KB n=100
10 Correct 3 ms 27304 KB n=100
11 Correct 4 ms 27228 KB n=100
12 Correct 4 ms 27228 KB n=100
13 Correct 4 ms 27228 KB n=100
14 Correct 4 ms 27228 KB n=100
15 Correct 3 ms 27228 KB n=100
16 Correct 3 ms 27228 KB n=100
17 Correct 4 ms 27228 KB n=100
18 Correct 4 ms 27228 KB n=100
19 Correct 3 ms 27228 KB n=100
20 Correct 3 ms 27228 KB n=100
21 Correct 4 ms 27484 KB n=100
22 Correct 4 ms 27228 KB n=100
23 Correct 4 ms 27228 KB n=100
24 Correct 3 ms 27312 KB n=100
25 Correct 3 ms 27348 KB n=100
26 Correct 3 ms 27304 KB n=12
27 Correct 4 ms 27228 KB n=100
28 Correct 4 ms 27316 KB n=500
29 Correct 4 ms 27228 KB n=500
30 Correct 4 ms 27228 KB n=500
31 Correct 4 ms 27344 KB n=500
32 Correct 4 ms 27228 KB n=500
33 Correct 4 ms 27228 KB n=500
34 Correct 3 ms 27228 KB n=500
35 Correct 4 ms 27480 KB n=500
36 Correct 4 ms 27228 KB n=500
37 Correct 4 ms 27228 KB n=500
38 Correct 4 ms 27316 KB n=500
39 Correct 4 ms 27228 KB n=500
40 Correct 3 ms 27228 KB n=500
41 Correct 4 ms 27228 KB n=500
42 Correct 4 ms 27228 KB n=500
43 Correct 4 ms 27228 KB n=500
44 Correct 4 ms 27480 KB n=500
45 Correct 5 ms 27452 KB n=500
46 Correct 4 ms 27228 KB n=500
47 Correct 5 ms 27228 KB n=500
48 Correct 4 ms 27228 KB n=500
49 Correct 5 ms 27228 KB n=500
50 Correct 4 ms 27228 KB n=500
51 Correct 4 ms 27228 KB n=500
52 Correct 5 ms 27476 KB n=500
53 Correct 4 ms 27228 KB n=500
54 Correct 6 ms 27228 KB n=500
55 Correct 5 ms 27228 KB n=278
56 Correct 4 ms 27228 KB n=500
57 Correct 4 ms 27228 KB n=500
58 Correct 5 ms 27228 KB n=500
59 Correct 5 ms 27996 KB n=2000
60 Correct 6 ms 27740 KB n=2000
61 Correct 6 ms 27740 KB n=2000
62 Correct 6 ms 27836 KB n=2000
63 Correct 5 ms 27740 KB n=2000
64 Correct 5 ms 27740 KB n=2000
65 Correct 7 ms 27740 KB n=2000
66 Correct 6 ms 27740 KB n=2000
67 Correct 8 ms 27780 KB n=2000
68 Correct 7 ms 27740 KB n=2000
69 Correct 5 ms 27772 KB n=2000
70 Correct 7 ms 27740 KB n=2000
71 Correct 5 ms 27740 KB n=2000
72 Correct 5 ms 27740 KB n=2000
73 Correct 6 ms 27740 KB n=2000
74 Correct 5 ms 27740 KB n=1844
75 Correct 5 ms 27740 KB n=2000
76 Correct 5 ms 27740 KB n=2000
77 Correct 5 ms 27844 KB n=2000
78 Correct 6 ms 27728 KB n=2000
79 Correct 5 ms 27740 KB n=2000
80 Correct 5 ms 27740 KB n=2000
81 Correct 6 ms 27740 KB n=2000
82 Correct 5 ms 27740 KB n=2000
83 Correct 5 ms 27740 KB n=2000
84 Correct 5 ms 27740 KB n=2000
85 Correct 6 ms 27740 KB n=2000
86 Correct 5 ms 27740 KB n=2000
87 Correct 5 ms 27740 KB n=2000
88 Correct 5 ms 27740 KB n=2000
89 Correct 5 ms 27740 KB n=2000
90 Correct 5 ms 27836 KB n=2000
91 Correct 5 ms 27740 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 3 ms 27228 KB n=5
2 Correct 3 ms 27228 KB n=100
3 Correct 5 ms 27228 KB n=100
4 Correct 5 ms 27228 KB n=100
5 Correct 4 ms 27312 KB n=100
6 Correct 3 ms 27228 KB n=100
7 Correct 5 ms 27228 KB n=100
8 Correct 3 ms 27228 KB n=100
9 Correct 3 ms 27228 KB n=100
10 Correct 3 ms 27304 KB n=100
11 Correct 4 ms 27228 KB n=100
12 Correct 4 ms 27228 KB n=100
13 Correct 4 ms 27228 KB n=100
14 Correct 4 ms 27228 KB n=100
15 Correct 3 ms 27228 KB n=100
16 Correct 3 ms 27228 KB n=100
17 Correct 4 ms 27228 KB n=100
18 Correct 4 ms 27228 KB n=100
19 Correct 3 ms 27228 KB n=100
20 Correct 3 ms 27228 KB n=100
21 Correct 4 ms 27484 KB n=100
22 Correct 4 ms 27228 KB n=100
23 Correct 4 ms 27228 KB n=100
24 Correct 3 ms 27312 KB n=100
25 Correct 3 ms 27348 KB n=100
26 Correct 3 ms 27304 KB n=12
27 Correct 4 ms 27228 KB n=100
28 Correct 4 ms 27316 KB n=500
29 Correct 4 ms 27228 KB n=500
30 Correct 4 ms 27228 KB n=500
31 Correct 4 ms 27344 KB n=500
32 Correct 4 ms 27228 KB n=500
33 Correct 4 ms 27228 KB n=500
34 Correct 3 ms 27228 KB n=500
35 Correct 4 ms 27480 KB n=500
36 Correct 4 ms 27228 KB n=500
37 Correct 4 ms 27228 KB n=500
38 Correct 4 ms 27316 KB n=500
39 Correct 4 ms 27228 KB n=500
40 Correct 3 ms 27228 KB n=500
41 Correct 4 ms 27228 KB n=500
42 Correct 4 ms 27228 KB n=500
43 Correct 4 ms 27228 KB n=500
44 Correct 4 ms 27480 KB n=500
45 Correct 5 ms 27452 KB n=500
46 Correct 4 ms 27228 KB n=500
47 Correct 5 ms 27228 KB n=500
48 Correct 4 ms 27228 KB n=500
49 Correct 5 ms 27228 KB n=500
50 Correct 4 ms 27228 KB n=500
51 Correct 4 ms 27228 KB n=500
52 Correct 5 ms 27476 KB n=500
53 Correct 4 ms 27228 KB n=500
54 Correct 6 ms 27228 KB n=500
55 Correct 5 ms 27228 KB n=278
56 Correct 4 ms 27228 KB n=500
57 Correct 4 ms 27228 KB n=500
58 Correct 5 ms 27228 KB n=500
59 Correct 5 ms 27996 KB n=2000
60 Correct 6 ms 27740 KB n=2000
61 Correct 6 ms 27740 KB n=2000
62 Correct 6 ms 27836 KB n=2000
63 Correct 5 ms 27740 KB n=2000
64 Correct 5 ms 27740 KB n=2000
65 Correct 7 ms 27740 KB n=2000
66 Correct 6 ms 27740 KB n=2000
67 Correct 8 ms 27780 KB n=2000
68 Correct 7 ms 27740 KB n=2000
69 Correct 5 ms 27772 KB n=2000
70 Correct 7 ms 27740 KB n=2000
71 Correct 5 ms 27740 KB n=2000
72 Correct 5 ms 27740 KB n=2000
73 Correct 6 ms 27740 KB n=2000
74 Correct 5 ms 27740 KB n=1844
75 Correct 5 ms 27740 KB n=2000
76 Correct 5 ms 27740 KB n=2000
77 Correct 5 ms 27844 KB n=2000
78 Correct 6 ms 27728 KB n=2000
79 Correct 5 ms 27740 KB n=2000
80 Correct 5 ms 27740 KB n=2000
81 Correct 6 ms 27740 KB n=2000
82 Correct 5 ms 27740 KB n=2000
83 Correct 5 ms 27740 KB n=2000
84 Correct 5 ms 27740 KB n=2000
85 Correct 6 ms 27740 KB n=2000
86 Correct 5 ms 27740 KB n=2000
87 Correct 5 ms 27740 KB n=2000
88 Correct 5 ms 27740 KB n=2000
89 Correct 5 ms 27740 KB n=2000
90 Correct 5 ms 27836 KB n=2000
91 Correct 5 ms 27740 KB n=2000
92 Correct 388 ms 109908 KB n=200000
93 Correct 385 ms 116308 KB n=200000
94 Correct 426 ms 120592 KB n=200000
95 Correct 433 ms 109544 KB n=200000
96 Correct 405 ms 109768 KB n=200000
97 Correct 478 ms 115028 KB n=200000
98 Correct 459 ms 109628 KB n=200000
99 Correct 496 ms 109968 KB n=200000
100 Correct 446 ms 109668 KB n=200000
101 Correct 433 ms 122452 KB n=200000
102 Correct 302 ms 110932 KB n=200000
103 Correct 253 ms 110716 KB n=200000
104 Correct 236 ms 110932 KB n=200000
105 Correct 228 ms 111328 KB n=200000
106 Correct 234 ms 111188 KB n=200000
107 Correct 241 ms 111444 KB n=200000
108 Correct 390 ms 109652 KB n=200000
109 Correct 383 ms 109656 KB n=200000
110 Correct 377 ms 109652 KB n=200000
111 Correct 393 ms 109388 KB n=200000
112 Correct 371 ms 121172 KB n=200000
113 Correct 385 ms 115028 KB n=200000
114 Correct 391 ms 109256 KB n=200000
115 Correct 412 ms 112312 KB n=200000
116 Correct 380 ms 109768 KB n=200000
117 Correct 396 ms 121640 KB n=200000
118 Correct 406 ms 113532 KB n=200000
119 Correct 381 ms 109868 KB n=200000
120 Correct 341 ms 121452 KB n=200000
121 Correct 347 ms 121428 KB n=200000
122 Correct 344 ms 122000 KB n=200000
123 Correct 238 ms 110932 KB n=200000
124 Correct 90 ms 46932 KB n=25264