Submission #543088

# Submission time Handle Problem Language Result Execution time Memory
543088 2022-03-29T08:50:25 Z Danilo21 Birthday gift (IZhO18_treearray) C++14
100 / 100
1111 ms 88348 KB
#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define en '\n'
#define sp ' '
#define tb '\t'
#define ri(n) int n; cin >> n
#define rl(n) ll n; cin >> n
#define rs(s) string s; cin >> s
#define rc(c) char c; cin >> c
#define rv(v) for (auto &x : v) cin >> x
#define pven(v) for (auto x : v) cout << x << en
#define pv(v) for (auto x : v) cout << x << sp; cout << en
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define yes cout << "YES" << en
#define no cout << "NO" << en
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define ssort(a, b) if (a < b) swap(a, b)
#define bitcnt(a) __builtin_popcountll(a)
#define bithigh(a) 63-__builtin_clzll(a)
#define lg bithigh
ll highpow(ll a) { return 1LL << (ll)lg(a); }

using namespace std;

class Lca{
private:
    int n;
    vector<vector<int> > g, par;
    vector<int> in, out, depth;

    int dfs(int s, int p = -1, int d = 0, int t = 0){
        par[s][0] = p;
        depth[s] = d;
        in[s] = t;
        for (int u : g[s])
            if (u^p)
                t = dfs(u, s, d+1, t+1);
        return out[s] = t;
    }

public:
    Lca(int n){
        this->n = n;
        g = vector<vector<int> >(n);
        par = vector<vector<int> >(n, vector<int>(lg(n)+1, -1));
        in = out = depth = vector<int>(n);
    }

    void edge(int u, int v){
        g[u].pb(v);
        g[v].pb(u);
    }

    void build(int root = 0){
        dfs(root);
        for (int d = 1; (1<<d) <= n; d++)
            for (int s = 0; s < n; s++)
                if (~par[s][d-1])
                    par[s][d] = par[par[s][d-1]][d-1];
    }

    bool Ancestor(int s, int p) const { return in[s] >= in[p] && out[s] <= out[p]; }

    int Par(int s, int d) const {
        if (!d) return s;
        return Par(par[s][lg(d)], d-highpow(d));
    }

    int lca(int u, int v) const {
        if (depth[u] > depth[v]) swap(u, v);
        if (Ancestor(v, u)) return u;
        v = Par(v, depth[v] - depth[u]);
        for (int d = lg(n); ~d; d--){
            if (par[u][d]^par[v][d]){
                u = par[u][d];
                v = par[v][d];
            }
        }
        return par[u][0];
    }
};

const ll LINF = 4e18;
const int mxN = 2e5+10, INF = 2e9, mod = (1 ? 1e9+7 : 998244353);
ll n, m, q, a[mxN];
set<int> idx[mxN][2];
Lca* lc;

void Solve(){

    cin >> n >> m >> q;
    lc = new Lca(n);
    for (int i = 1; i < n; i++){
        ri(u); ri(v);
        u--; v--;
        lc->edge(u, v);
    }
    lc->build();
    for (int i = 0; i < m; i++){
        cin >> a[i];
        a[i]--;
        idx[a[i]][0].insert(i);
    }
    for (int i = 0; i < m-1; i++){
        int s = lc->lca(a[i], a[i+1]);
        idx[s][1].insert(i);
    }
    while (q--){
        ri(t); t--;
        if (t){
            ri(l); ri(r); ri(s);
            l--; r--; s--;
            auto it1 = idx[s][0].lower_bound(l);
            auto it2 = idx[s][1].lower_bound(l);
            if (it1 != idx[s][0].end() && *it1 <= r) cout << *it1 + 1 << sp << *it1 + 1 << en;
            else if (it2 != idx[s][1].end() && *it2 < r) cout << *it2 + 1 << sp << *it2 + 2 << en;
            else cout << -1 << sp << -1 << en;
        }
        else{
            ri(i); ri(s);
            i--; s--;
            idx[a[i]][0].erase(i);
            idx[s][0].insert(i);
            if (i){
                idx[lc->lca(a[i], a[i-1])][1].erase(i-1);
                idx[lc->lca(s, a[i-1])][1].insert(i-1);
            }
            if (i < m-1){
                idx[lc->lca(a[i], a[i+1])][1].erase(i);
                idx[lc->lca(s, a[i+1])][1].insert(i);
            }
            a[i] = s;
        }
    }
}

int main(){

    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0); cerr.tie(0);
    cout << setprecision(12) << fixed;
    cerr << setprecision(12) << fixed;
    cerr << "Started!" << endl;

    int t = 1;
    //cin >> t;
    while (t--)
        Solve();

    return 0;
}

Compilation message

treearray.cpp: In function 'long long int highpow(long long int)':
treearray.cpp:26:22: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   26 | #define bithigh(a) 63-__builtin_clzll(a)
      |                      ^
treearray.cpp:27:12: note: in expansion of macro 'bithigh'
   27 | #define lg bithigh
      |            ^~~~~~~
treearray.cpp:28:38: note: in expansion of macro 'lg'
   28 | ll highpow(ll a) { return 1LL << (ll)lg(a); }
      |                                      ^~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 19028 KB n=5
2 Correct 9 ms 19028 KB n=100
3 Correct 10 ms 19124 KB n=100
4 Correct 10 ms 19112 KB n=100
5 Correct 9 ms 19028 KB n=100
6 Correct 10 ms 19028 KB n=100
7 Correct 10 ms 19104 KB n=100
8 Correct 9 ms 19124 KB n=100
9 Correct 10 ms 19028 KB n=100
10 Correct 10 ms 19028 KB n=100
11 Correct 10 ms 19124 KB n=100
12 Correct 10 ms 19028 KB n=100
13 Correct 10 ms 19036 KB n=100
14 Correct 9 ms 19028 KB n=100
15 Correct 9 ms 19028 KB n=100
16 Correct 10 ms 19028 KB n=100
17 Correct 10 ms 19148 KB n=100
18 Correct 10 ms 19028 KB n=100
19 Correct 10 ms 19124 KB n=100
20 Correct 10 ms 19088 KB n=100
21 Correct 9 ms 19028 KB n=100
22 Correct 10 ms 19028 KB n=100
23 Correct 10 ms 19120 KB n=100
24 Correct 10 ms 19028 KB n=100
25 Correct 10 ms 19120 KB n=100
26 Correct 10 ms 19128 KB n=12
27 Correct 9 ms 19028 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 9 ms 19028 KB n=5
2 Correct 9 ms 19028 KB n=100
3 Correct 10 ms 19124 KB n=100
4 Correct 10 ms 19112 KB n=100
5 Correct 9 ms 19028 KB n=100
6 Correct 10 ms 19028 KB n=100
7 Correct 10 ms 19104 KB n=100
8 Correct 9 ms 19124 KB n=100
9 Correct 10 ms 19028 KB n=100
10 Correct 10 ms 19028 KB n=100
11 Correct 10 ms 19124 KB n=100
12 Correct 10 ms 19028 KB n=100
13 Correct 10 ms 19036 KB n=100
14 Correct 9 ms 19028 KB n=100
15 Correct 9 ms 19028 KB n=100
16 Correct 10 ms 19028 KB n=100
17 Correct 10 ms 19148 KB n=100
18 Correct 10 ms 19028 KB n=100
19 Correct 10 ms 19124 KB n=100
20 Correct 10 ms 19088 KB n=100
21 Correct 9 ms 19028 KB n=100
22 Correct 10 ms 19028 KB n=100
23 Correct 10 ms 19120 KB n=100
24 Correct 10 ms 19028 KB n=100
25 Correct 10 ms 19120 KB n=100
26 Correct 10 ms 19128 KB n=12
27 Correct 9 ms 19028 KB n=100
28 Correct 10 ms 19156 KB n=500
29 Correct 10 ms 19148 KB n=500
30 Correct 12 ms 19156 KB n=500
31 Correct 10 ms 19156 KB n=500
32 Correct 10 ms 19128 KB n=500
33 Correct 11 ms 19144 KB n=500
34 Correct 11 ms 19156 KB n=500
35 Correct 11 ms 19260 KB n=500
36 Correct 10 ms 19260 KB n=500
37 Correct 10 ms 19156 KB n=500
38 Correct 11 ms 19172 KB n=500
39 Correct 10 ms 19156 KB n=500
40 Correct 10 ms 19156 KB n=500
41 Correct 10 ms 19260 KB n=500
42 Correct 10 ms 19256 KB n=500
43 Correct 11 ms 19156 KB n=500
44 Correct 11 ms 19128 KB n=500
45 Correct 11 ms 19416 KB n=500
46 Correct 10 ms 19156 KB n=500
47 Correct 11 ms 19168 KB n=500
48 Correct 11 ms 19260 KB n=500
49 Correct 12 ms 19260 KB n=500
50 Correct 11 ms 19156 KB n=500
51 Correct 11 ms 19152 KB n=500
52 Correct 11 ms 19260 KB n=500
53 Correct 10 ms 19156 KB n=500
54 Correct 11 ms 19260 KB n=500
55 Correct 10 ms 19156 KB n=278
56 Correct 16 ms 19216 KB n=500
57 Correct 11 ms 19256 KB n=500
58 Correct 10 ms 19232 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 9 ms 19028 KB n=5
2 Correct 9 ms 19028 KB n=100
3 Correct 10 ms 19124 KB n=100
4 Correct 10 ms 19112 KB n=100
5 Correct 9 ms 19028 KB n=100
6 Correct 10 ms 19028 KB n=100
7 Correct 10 ms 19104 KB n=100
8 Correct 9 ms 19124 KB n=100
9 Correct 10 ms 19028 KB n=100
10 Correct 10 ms 19028 KB n=100
11 Correct 10 ms 19124 KB n=100
12 Correct 10 ms 19028 KB n=100
13 Correct 10 ms 19036 KB n=100
14 Correct 9 ms 19028 KB n=100
15 Correct 9 ms 19028 KB n=100
16 Correct 10 ms 19028 KB n=100
17 Correct 10 ms 19148 KB n=100
18 Correct 10 ms 19028 KB n=100
19 Correct 10 ms 19124 KB n=100
20 Correct 10 ms 19088 KB n=100
21 Correct 9 ms 19028 KB n=100
22 Correct 10 ms 19028 KB n=100
23 Correct 10 ms 19120 KB n=100
24 Correct 10 ms 19028 KB n=100
25 Correct 10 ms 19120 KB n=100
26 Correct 10 ms 19128 KB n=12
27 Correct 9 ms 19028 KB n=100
28 Correct 10 ms 19156 KB n=500
29 Correct 10 ms 19148 KB n=500
30 Correct 12 ms 19156 KB n=500
31 Correct 10 ms 19156 KB n=500
32 Correct 10 ms 19128 KB n=500
33 Correct 11 ms 19144 KB n=500
34 Correct 11 ms 19156 KB n=500
35 Correct 11 ms 19260 KB n=500
36 Correct 10 ms 19260 KB n=500
37 Correct 10 ms 19156 KB n=500
38 Correct 11 ms 19172 KB n=500
39 Correct 10 ms 19156 KB n=500
40 Correct 10 ms 19156 KB n=500
41 Correct 10 ms 19260 KB n=500
42 Correct 10 ms 19256 KB n=500
43 Correct 11 ms 19156 KB n=500
44 Correct 11 ms 19128 KB n=500
45 Correct 11 ms 19416 KB n=500
46 Correct 10 ms 19156 KB n=500
47 Correct 11 ms 19168 KB n=500
48 Correct 11 ms 19260 KB n=500
49 Correct 12 ms 19260 KB n=500
50 Correct 11 ms 19156 KB n=500
51 Correct 11 ms 19152 KB n=500
52 Correct 11 ms 19260 KB n=500
53 Correct 10 ms 19156 KB n=500
54 Correct 11 ms 19260 KB n=500
55 Correct 10 ms 19156 KB n=278
56 Correct 16 ms 19216 KB n=500
57 Correct 11 ms 19256 KB n=500
58 Correct 10 ms 19232 KB n=500
59 Correct 13 ms 19592 KB n=2000
60 Correct 11 ms 19668 KB n=2000
61 Correct 13 ms 19708 KB n=2000
62 Correct 13 ms 19644 KB n=2000
63 Correct 12 ms 19644 KB n=2000
64 Correct 13 ms 19712 KB n=2000
65 Correct 12 ms 19648 KB n=2000
66 Correct 13 ms 19640 KB n=2000
67 Correct 14 ms 19588 KB n=2000
68 Correct 13 ms 19676 KB n=2000
69 Correct 12 ms 19576 KB n=2000
70 Correct 13 ms 19668 KB n=2000
71 Correct 12 ms 19652 KB n=2000
72 Correct 12 ms 19624 KB n=2000
73 Correct 13 ms 19648 KB n=2000
74 Correct 12 ms 19540 KB n=1844
75 Correct 12 ms 19668 KB n=2000
76 Correct 13 ms 19644 KB n=2000
77 Correct 14 ms 19668 KB n=2000
78 Correct 13 ms 19668 KB n=2000
79 Correct 15 ms 19648 KB n=2000
80 Correct 14 ms 19668 KB n=2000
81 Correct 13 ms 19668 KB n=2000
82 Correct 14 ms 19652 KB n=2000
83 Correct 12 ms 19648 KB n=2000
84 Correct 13 ms 19668 KB n=2000
85 Correct 12 ms 19664 KB n=2000
86 Correct 15 ms 19648 KB n=2000
87 Correct 14 ms 19668 KB n=2000
88 Correct 12 ms 19720 KB n=2000
89 Correct 12 ms 19696 KB n=2000
90 Correct 14 ms 19648 KB n=2000
91 Correct 13 ms 19592 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 9 ms 19028 KB n=5
2 Correct 9 ms 19028 KB n=100
3 Correct 10 ms 19124 KB n=100
4 Correct 10 ms 19112 KB n=100
5 Correct 9 ms 19028 KB n=100
6 Correct 10 ms 19028 KB n=100
7 Correct 10 ms 19104 KB n=100
8 Correct 9 ms 19124 KB n=100
9 Correct 10 ms 19028 KB n=100
10 Correct 10 ms 19028 KB n=100
11 Correct 10 ms 19124 KB n=100
12 Correct 10 ms 19028 KB n=100
13 Correct 10 ms 19036 KB n=100
14 Correct 9 ms 19028 KB n=100
15 Correct 9 ms 19028 KB n=100
16 Correct 10 ms 19028 KB n=100
17 Correct 10 ms 19148 KB n=100
18 Correct 10 ms 19028 KB n=100
19 Correct 10 ms 19124 KB n=100
20 Correct 10 ms 19088 KB n=100
21 Correct 9 ms 19028 KB n=100
22 Correct 10 ms 19028 KB n=100
23 Correct 10 ms 19120 KB n=100
24 Correct 10 ms 19028 KB n=100
25 Correct 10 ms 19120 KB n=100
26 Correct 10 ms 19128 KB n=12
27 Correct 9 ms 19028 KB n=100
28 Correct 10 ms 19156 KB n=500
29 Correct 10 ms 19148 KB n=500
30 Correct 12 ms 19156 KB n=500
31 Correct 10 ms 19156 KB n=500
32 Correct 10 ms 19128 KB n=500
33 Correct 11 ms 19144 KB n=500
34 Correct 11 ms 19156 KB n=500
35 Correct 11 ms 19260 KB n=500
36 Correct 10 ms 19260 KB n=500
37 Correct 10 ms 19156 KB n=500
38 Correct 11 ms 19172 KB n=500
39 Correct 10 ms 19156 KB n=500
40 Correct 10 ms 19156 KB n=500
41 Correct 10 ms 19260 KB n=500
42 Correct 10 ms 19256 KB n=500
43 Correct 11 ms 19156 KB n=500
44 Correct 11 ms 19128 KB n=500
45 Correct 11 ms 19416 KB n=500
46 Correct 10 ms 19156 KB n=500
47 Correct 11 ms 19168 KB n=500
48 Correct 11 ms 19260 KB n=500
49 Correct 12 ms 19260 KB n=500
50 Correct 11 ms 19156 KB n=500
51 Correct 11 ms 19152 KB n=500
52 Correct 11 ms 19260 KB n=500
53 Correct 10 ms 19156 KB n=500
54 Correct 11 ms 19260 KB n=500
55 Correct 10 ms 19156 KB n=278
56 Correct 16 ms 19216 KB n=500
57 Correct 11 ms 19256 KB n=500
58 Correct 10 ms 19232 KB n=500
59 Correct 13 ms 19592 KB n=2000
60 Correct 11 ms 19668 KB n=2000
61 Correct 13 ms 19708 KB n=2000
62 Correct 13 ms 19644 KB n=2000
63 Correct 12 ms 19644 KB n=2000
64 Correct 13 ms 19712 KB n=2000
65 Correct 12 ms 19648 KB n=2000
66 Correct 13 ms 19640 KB n=2000
67 Correct 14 ms 19588 KB n=2000
68 Correct 13 ms 19676 KB n=2000
69 Correct 12 ms 19576 KB n=2000
70 Correct 13 ms 19668 KB n=2000
71 Correct 12 ms 19652 KB n=2000
72 Correct 12 ms 19624 KB n=2000
73 Correct 13 ms 19648 KB n=2000
74 Correct 12 ms 19540 KB n=1844
75 Correct 12 ms 19668 KB n=2000
76 Correct 13 ms 19644 KB n=2000
77 Correct 14 ms 19668 KB n=2000
78 Correct 13 ms 19668 KB n=2000
79 Correct 15 ms 19648 KB n=2000
80 Correct 14 ms 19668 KB n=2000
81 Correct 13 ms 19668 KB n=2000
82 Correct 14 ms 19652 KB n=2000
83 Correct 12 ms 19648 KB n=2000
84 Correct 13 ms 19668 KB n=2000
85 Correct 12 ms 19664 KB n=2000
86 Correct 15 ms 19648 KB n=2000
87 Correct 14 ms 19668 KB n=2000
88 Correct 12 ms 19720 KB n=2000
89 Correct 12 ms 19696 KB n=2000
90 Correct 14 ms 19648 KB n=2000
91 Correct 13 ms 19592 KB n=2000
92 Correct 631 ms 81568 KB n=200000
93 Correct 843 ms 85164 KB n=200000
94 Correct 591 ms 87364 KB n=200000
95 Correct 623 ms 81292 KB n=200000
96 Correct 652 ms 81428 KB n=200000
97 Correct 937 ms 84380 KB n=200000
98 Correct 626 ms 81488 KB n=200000
99 Correct 830 ms 81552 KB n=200000
100 Correct 623 ms 81292 KB n=200000
101 Correct 492 ms 88348 KB n=200000
102 Correct 398 ms 82636 KB n=200000
103 Correct 394 ms 82680 KB n=200000
104 Correct 425 ms 82620 KB n=200000
105 Correct 399 ms 83020 KB n=200000
106 Correct 418 ms 82960 KB n=200000
107 Correct 400 ms 83132 KB n=200000
108 Correct 772 ms 81672 KB n=200000
109 Correct 790 ms 81464 KB n=200000
110 Correct 786 ms 81484 KB n=200000
111 Correct 767 ms 80908 KB n=200000
112 Correct 563 ms 87600 KB n=200000
113 Correct 910 ms 84452 KB n=200000
114 Correct 699 ms 80960 KB n=200000
115 Correct 1111 ms 82948 KB n=200000
116 Correct 650 ms 81580 KB n=200000
117 Correct 529 ms 87788 KB n=200000
118 Correct 1018 ms 83716 KB n=200000
119 Correct 629 ms 81476 KB n=200000
120 Correct 503 ms 87268 KB n=200000
121 Correct 468 ms 87420 KB n=200000
122 Correct 521 ms 87584 KB n=200000
123 Correct 453 ms 82804 KB n=200000
124 Correct 177 ms 35404 KB n=25264