Submission #441559

# Submission time Handle Problem Language Result Execution time Memory
441559 2021-07-05T11:14:13 Z nishuz Birthday gift (IZhO18_treearray) C++17
100 / 100
2458 ms 98976 KB
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2")
 
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace __gnu_pbds;
using namespace std;
 
#define ll long long
#define ld long double
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define eb emplace_back
#define F first
#define S second
#define mp make_pair
#define random mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
 
template <typename T> using oset = tree <pair <T, T>, null_type, less <pair <T, T>>, rb_tree_tag, tree_order_statistics_node_update>;
 
template <typename Container>
void Print(Container& container, int starting) {
    auto Start = container.begin() + starting, End = container.end();
    while (Start != End) cout << *(Start++) << " ";
    cout << '\n';
}
 
template <typename T>
void print(T&& t) {cout << t << '\n';}
 
template <typename T, typename... Args>
void print(T&& t, Args&&... args) {
    cout << t << " ";
    print(forward<Args>(args)...);
}


const int mxn = 2e5+1, mxk = 19;

vector <int> adj[mxn], dep(mxn);
vector <vector <int>> par(mxn, vector <int> (mxk, -1));
map <int, set <int>> mm, mmm;
 
void dfs(int u, int p, int d)
{
    par[u][0] = p;
    dep[u] = d;
    for (int i = 1; i < mxk; ++i)
    {
        int t = par[u][i-1];
        if (t == -1) continue;
        par[u][i] = par[t][i-1];
    }
    for (int v : adj[u])
        if (v != p)
            dfs(v, u, d+1);
}
 
int kthpar(int n, int k)
{
    for (int i = 0; i < mxk; ++i)
        if (k & (1 << i))
            n = par[n][i];
    return n;
}
 
int lca(int a, int b)
{
    if (dep[a] > dep[b]) a = kthpar(a, dep[a]-dep[b]);
    else if (dep[b] > dep[a]) b = kthpar(b, dep[b]-dep[a]);
    if (a == b) return a;
    for (int i = mxk-1; i >= 0; --i)
    {
        if (par[a][i] != par[b][i])
        {
            a = par[a][i];
            b = par[b][i];
        }
    }
    return par[a][0];
}
 
inline void solve() {
    int n, m, q;
    cin >> n >> m >> q;
    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        cin >> u >> v;
        adj[u].eb(v);
        adj[v].eb(u);
    }
    vector <int> a(m + 1);
    for (int i = 1; i <= m; ++i) {
        cin >> a[i];
        mm[a[i]].insert(i);
    }
    dfs(1, 0, 0);
    for (int i = 1; i < m; ++i) {
        mmm[lca(a[i], a[i + 1])].insert(i);
    }
    for (int _ = 0; _ < q; _++) {
        int t;
        cin >> t;
        if (t == 1) {
            int pos, v;
            cin >> pos >> v;
            mm[a[pos]].erase(pos);
            if (pos != m) {
                mmm[lca(a[pos], a[pos + 1])].erase(pos);
                mmm[lca(v, a[pos + 1])].insert(pos);
            }
            if (pos != 1) {
                mmm[lca(a[pos - 1], a[pos])].erase(pos - 1);
                mmm[lca(a[pos - 1], v)].insert(pos - 1);
            }
            a[pos] = v;
            mm[v].insert(pos);
        } else {
            int l, r, v;
            cin >> l >> r >> v;
            if (l > r) swap(l, r);
            int hi = *mm[v].lower_bound(l);
            if (l <= hi && hi <= r) {
                print(hi, hi);
                goto END;
            }
            hi = *mmm[v].lower_bound(l);
            if (l <= hi && hi + 1 <= r) {
                print(hi, hi + 1);
                goto END;
            }
            print(-1, -1);
            END:;
        }
    }
}
 
int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int T = 1;
    // cin >> T;
    for (int test = 1; test <= T; ++test) {
        // cout << "Case #" << test << ": ";
        solve();
    }
}
# Verdict Execution time Memory Grader output
1 Correct 25 ms 29288 KB n=5
2 Correct 26 ms 29272 KB n=100
3 Correct 26 ms 29200 KB n=100
4 Correct 26 ms 29260 KB n=100
5 Correct 26 ms 29260 KB n=100
6 Correct 26 ms 29312 KB n=100
7 Correct 27 ms 29384 KB n=100
8 Correct 26 ms 29192 KB n=100
9 Correct 25 ms 29260 KB n=100
10 Correct 26 ms 29256 KB n=100
11 Correct 26 ms 29232 KB n=100
12 Correct 26 ms 29308 KB n=100
13 Correct 27 ms 29316 KB n=100
14 Correct 26 ms 29272 KB n=100
15 Correct 25 ms 29292 KB n=100
16 Correct 26 ms 29232 KB n=100
17 Correct 28 ms 29296 KB n=100
18 Correct 26 ms 29276 KB n=100
19 Correct 27 ms 29296 KB n=100
20 Correct 27 ms 29244 KB n=100
21 Correct 26 ms 29260 KB n=100
22 Correct 28 ms 29252 KB n=100
23 Correct 26 ms 29300 KB n=100
24 Correct 26 ms 29268 KB n=100
25 Correct 27 ms 29232 KB n=100
26 Correct 25 ms 29260 KB n=12
27 Correct 26 ms 29220 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 25 ms 29288 KB n=5
2 Correct 26 ms 29272 KB n=100
3 Correct 26 ms 29200 KB n=100
4 Correct 26 ms 29260 KB n=100
5 Correct 26 ms 29260 KB n=100
6 Correct 26 ms 29312 KB n=100
7 Correct 27 ms 29384 KB n=100
8 Correct 26 ms 29192 KB n=100
9 Correct 25 ms 29260 KB n=100
10 Correct 26 ms 29256 KB n=100
11 Correct 26 ms 29232 KB n=100
12 Correct 26 ms 29308 KB n=100
13 Correct 27 ms 29316 KB n=100
14 Correct 26 ms 29272 KB n=100
15 Correct 25 ms 29292 KB n=100
16 Correct 26 ms 29232 KB n=100
17 Correct 28 ms 29296 KB n=100
18 Correct 26 ms 29276 KB n=100
19 Correct 27 ms 29296 KB n=100
20 Correct 27 ms 29244 KB n=100
21 Correct 26 ms 29260 KB n=100
22 Correct 28 ms 29252 KB n=100
23 Correct 26 ms 29300 KB n=100
24 Correct 26 ms 29268 KB n=100
25 Correct 27 ms 29232 KB n=100
26 Correct 25 ms 29260 KB n=12
27 Correct 26 ms 29220 KB n=100
28 Correct 27 ms 29296 KB n=500
29 Correct 28 ms 29388 KB n=500
30 Correct 29 ms 29388 KB n=500
31 Correct 27 ms 29420 KB n=500
32 Correct 26 ms 29388 KB n=500
33 Correct 27 ms 29312 KB n=500
34 Correct 26 ms 29292 KB n=500
35 Correct 28 ms 29372 KB n=500
36 Correct 26 ms 29296 KB n=500
37 Correct 27 ms 29388 KB n=500
38 Correct 31 ms 29372 KB n=500
39 Correct 26 ms 29260 KB n=500
40 Correct 26 ms 29260 KB n=500
41 Correct 27 ms 29308 KB n=500
42 Correct 27 ms 29352 KB n=500
43 Correct 26 ms 29388 KB n=500
44 Correct 27 ms 29404 KB n=500
45 Correct 26 ms 29348 KB n=500
46 Correct 27 ms 29452 KB n=500
47 Correct 27 ms 29388 KB n=500
48 Correct 27 ms 29388 KB n=500
49 Correct 29 ms 29388 KB n=500
50 Correct 27 ms 29380 KB n=500
51 Correct 29 ms 29428 KB n=500
52 Correct 27 ms 29388 KB n=500
53 Correct 27 ms 29420 KB n=500
54 Correct 27 ms 29388 KB n=500
55 Correct 26 ms 29260 KB n=278
56 Correct 27 ms 29456 KB n=500
57 Correct 27 ms 29360 KB n=500
58 Correct 27 ms 29304 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 25 ms 29288 KB n=5
2 Correct 26 ms 29272 KB n=100
3 Correct 26 ms 29200 KB n=100
4 Correct 26 ms 29260 KB n=100
5 Correct 26 ms 29260 KB n=100
6 Correct 26 ms 29312 KB n=100
7 Correct 27 ms 29384 KB n=100
8 Correct 26 ms 29192 KB n=100
9 Correct 25 ms 29260 KB n=100
10 Correct 26 ms 29256 KB n=100
11 Correct 26 ms 29232 KB n=100
12 Correct 26 ms 29308 KB n=100
13 Correct 27 ms 29316 KB n=100
14 Correct 26 ms 29272 KB n=100
15 Correct 25 ms 29292 KB n=100
16 Correct 26 ms 29232 KB n=100
17 Correct 28 ms 29296 KB n=100
18 Correct 26 ms 29276 KB n=100
19 Correct 27 ms 29296 KB n=100
20 Correct 27 ms 29244 KB n=100
21 Correct 26 ms 29260 KB n=100
22 Correct 28 ms 29252 KB n=100
23 Correct 26 ms 29300 KB n=100
24 Correct 26 ms 29268 KB n=100
25 Correct 27 ms 29232 KB n=100
26 Correct 25 ms 29260 KB n=12
27 Correct 26 ms 29220 KB n=100
28 Correct 27 ms 29296 KB n=500
29 Correct 28 ms 29388 KB n=500
30 Correct 29 ms 29388 KB n=500
31 Correct 27 ms 29420 KB n=500
32 Correct 26 ms 29388 KB n=500
33 Correct 27 ms 29312 KB n=500
34 Correct 26 ms 29292 KB n=500
35 Correct 28 ms 29372 KB n=500
36 Correct 26 ms 29296 KB n=500
37 Correct 27 ms 29388 KB n=500
38 Correct 31 ms 29372 KB n=500
39 Correct 26 ms 29260 KB n=500
40 Correct 26 ms 29260 KB n=500
41 Correct 27 ms 29308 KB n=500
42 Correct 27 ms 29352 KB n=500
43 Correct 26 ms 29388 KB n=500
44 Correct 27 ms 29404 KB n=500
45 Correct 26 ms 29348 KB n=500
46 Correct 27 ms 29452 KB n=500
47 Correct 27 ms 29388 KB n=500
48 Correct 27 ms 29388 KB n=500
49 Correct 29 ms 29388 KB n=500
50 Correct 27 ms 29380 KB n=500
51 Correct 29 ms 29428 KB n=500
52 Correct 27 ms 29388 KB n=500
53 Correct 27 ms 29420 KB n=500
54 Correct 27 ms 29388 KB n=500
55 Correct 26 ms 29260 KB n=278
56 Correct 27 ms 29456 KB n=500
57 Correct 27 ms 29360 KB n=500
58 Correct 27 ms 29304 KB n=500
59 Correct 31 ms 29564 KB n=2000
60 Correct 30 ms 29920 KB n=2000
61 Correct 30 ms 29856 KB n=2000
62 Correct 31 ms 29748 KB n=2000
63 Correct 29 ms 29644 KB n=2000
64 Correct 30 ms 29788 KB n=2000
65 Correct 30 ms 29676 KB n=2000
66 Correct 31 ms 29884 KB n=2000
67 Correct 30 ms 29800 KB n=2000
68 Correct 30 ms 29772 KB n=2000
69 Correct 28 ms 29588 KB n=2000
70 Correct 28 ms 29580 KB n=2000
71 Correct 28 ms 29568 KB n=2000
72 Correct 28 ms 29644 KB n=2000
73 Correct 28 ms 29664 KB n=2000
74 Correct 29 ms 29600 KB n=1844
75 Correct 29 ms 29588 KB n=2000
76 Correct 29 ms 29644 KB n=2000
77 Correct 30 ms 29664 KB n=2000
78 Correct 31 ms 29696 KB n=2000
79 Correct 32 ms 29660 KB n=2000
80 Correct 31 ms 29764 KB n=2000
81 Correct 31 ms 29700 KB n=2000
82 Correct 29 ms 29680 KB n=2000
83 Correct 30 ms 29804 KB n=2000
84 Correct 30 ms 29664 KB n=2000
85 Correct 31 ms 29764 KB n=2000
86 Correct 30 ms 29816 KB n=2000
87 Correct 31 ms 29752 KB n=2000
88 Correct 29 ms 29824 KB n=2000
89 Correct 32 ms 29964 KB n=2000
90 Correct 31 ms 29900 KB n=2000
91 Correct 28 ms 29644 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 25 ms 29288 KB n=5
2 Correct 26 ms 29272 KB n=100
3 Correct 26 ms 29200 KB n=100
4 Correct 26 ms 29260 KB n=100
5 Correct 26 ms 29260 KB n=100
6 Correct 26 ms 29312 KB n=100
7 Correct 27 ms 29384 KB n=100
8 Correct 26 ms 29192 KB n=100
9 Correct 25 ms 29260 KB n=100
10 Correct 26 ms 29256 KB n=100
11 Correct 26 ms 29232 KB n=100
12 Correct 26 ms 29308 KB n=100
13 Correct 27 ms 29316 KB n=100
14 Correct 26 ms 29272 KB n=100
15 Correct 25 ms 29292 KB n=100
16 Correct 26 ms 29232 KB n=100
17 Correct 28 ms 29296 KB n=100
18 Correct 26 ms 29276 KB n=100
19 Correct 27 ms 29296 KB n=100
20 Correct 27 ms 29244 KB n=100
21 Correct 26 ms 29260 KB n=100
22 Correct 28 ms 29252 KB n=100
23 Correct 26 ms 29300 KB n=100
24 Correct 26 ms 29268 KB n=100
25 Correct 27 ms 29232 KB n=100
26 Correct 25 ms 29260 KB n=12
27 Correct 26 ms 29220 KB n=100
28 Correct 27 ms 29296 KB n=500
29 Correct 28 ms 29388 KB n=500
30 Correct 29 ms 29388 KB n=500
31 Correct 27 ms 29420 KB n=500
32 Correct 26 ms 29388 KB n=500
33 Correct 27 ms 29312 KB n=500
34 Correct 26 ms 29292 KB n=500
35 Correct 28 ms 29372 KB n=500
36 Correct 26 ms 29296 KB n=500
37 Correct 27 ms 29388 KB n=500
38 Correct 31 ms 29372 KB n=500
39 Correct 26 ms 29260 KB n=500
40 Correct 26 ms 29260 KB n=500
41 Correct 27 ms 29308 KB n=500
42 Correct 27 ms 29352 KB n=500
43 Correct 26 ms 29388 KB n=500
44 Correct 27 ms 29404 KB n=500
45 Correct 26 ms 29348 KB n=500
46 Correct 27 ms 29452 KB n=500
47 Correct 27 ms 29388 KB n=500
48 Correct 27 ms 29388 KB n=500
49 Correct 29 ms 29388 KB n=500
50 Correct 27 ms 29380 KB n=500
51 Correct 29 ms 29428 KB n=500
52 Correct 27 ms 29388 KB n=500
53 Correct 27 ms 29420 KB n=500
54 Correct 27 ms 29388 KB n=500
55 Correct 26 ms 29260 KB n=278
56 Correct 27 ms 29456 KB n=500
57 Correct 27 ms 29360 KB n=500
58 Correct 27 ms 29304 KB n=500
59 Correct 31 ms 29564 KB n=2000
60 Correct 30 ms 29920 KB n=2000
61 Correct 30 ms 29856 KB n=2000
62 Correct 31 ms 29748 KB n=2000
63 Correct 29 ms 29644 KB n=2000
64 Correct 30 ms 29788 KB n=2000
65 Correct 30 ms 29676 KB n=2000
66 Correct 31 ms 29884 KB n=2000
67 Correct 30 ms 29800 KB n=2000
68 Correct 30 ms 29772 KB n=2000
69 Correct 28 ms 29588 KB n=2000
70 Correct 28 ms 29580 KB n=2000
71 Correct 28 ms 29568 KB n=2000
72 Correct 28 ms 29644 KB n=2000
73 Correct 28 ms 29664 KB n=2000
74 Correct 29 ms 29600 KB n=1844
75 Correct 29 ms 29588 KB n=2000
76 Correct 29 ms 29644 KB n=2000
77 Correct 30 ms 29664 KB n=2000
78 Correct 31 ms 29696 KB n=2000
79 Correct 32 ms 29660 KB n=2000
80 Correct 31 ms 29764 KB n=2000
81 Correct 31 ms 29700 KB n=2000
82 Correct 29 ms 29680 KB n=2000
83 Correct 30 ms 29804 KB n=2000
84 Correct 30 ms 29664 KB n=2000
85 Correct 31 ms 29764 KB n=2000
86 Correct 30 ms 29816 KB n=2000
87 Correct 31 ms 29752 KB n=2000
88 Correct 29 ms 29824 KB n=2000
89 Correct 32 ms 29964 KB n=2000
90 Correct 31 ms 29900 KB n=2000
91 Correct 28 ms 29644 KB n=2000
92 Correct 1237 ms 71740 KB n=200000
93 Correct 2397 ms 84656 KB n=200000
94 Correct 2349 ms 93524 KB n=200000
95 Correct 1222 ms 72764 KB n=200000
96 Correct 1164 ms 72816 KB n=200000
97 Correct 2458 ms 83368 KB n=200000
98 Correct 1200 ms 73052 KB n=200000
99 Correct 1499 ms 72468 KB n=200000
100 Correct 1211 ms 72948 KB n=200000
101 Correct 2351 ms 95952 KB n=200000
102 Correct 646 ms 71056 KB n=200000
103 Correct 679 ms 71280 KB n=200000
104 Correct 683 ms 71144 KB n=200000
105 Correct 668 ms 70788 KB n=200000
106 Correct 673 ms 70840 KB n=200000
107 Correct 669 ms 70712 KB n=200000
108 Correct 1388 ms 72536 KB n=200000
109 Correct 1401 ms 72564 KB n=200000
110 Correct 1386 ms 72724 KB n=200000
111 Correct 1167 ms 70180 KB n=200000
112 Correct 2342 ms 94112 KB n=200000
113 Correct 2435 ms 83008 KB n=200000
114 Correct 1105 ms 70000 KB n=200000
115 Correct 2316 ms 76736 KB n=200000
116 Correct 1414 ms 79808 KB n=200000
117 Correct 2435 ms 98976 KB n=200000
118 Correct 2420 ms 85900 KB n=200000
119 Correct 1374 ms 79776 KB n=200000
120 Correct 2282 ms 95828 KB n=200000
121 Correct 2283 ms 95860 KB n=200000
122 Correct 2223 ms 96312 KB n=200000
123 Correct 726 ms 70468 KB n=200000
124 Correct 433 ms 46004 KB n=25264