Submission #175530

# Submission time Handle Problem Language Result Execution time Memory
175530 2020-01-07T07:53:24 Z VEGAnn Birthday gift (IZhO18_treearray) C++14
100 / 100
1303 ms 85044 KB
#include <bits/stdc++.h>
#define sz(x) ((int)x.size())
#define PB push_back
using namespace std;
const int N = 200100;
const int PW = 20;
set<int> st[N], pr[N];
set<int>::iterator it;
vector<int> g[N];
int tin[N], tout[N], up[N][PW], n, m, q, tt = 0, a[N];

void dfs(int v, int p){
    up[v][0] = p;
    for (int po = 1; po < PW; po++)
        up[v][po] = up[up[v][po - 1]][po - 1];

    tin[v] = tt++;

    for (int u : g[v]){
        if (u == p) continue;
        dfs(u, v);
    }

    tout[v] = tt++;
}

bool upper(int a, int b){
    return (tin[a] <= tin[b] && tout[a] >= tout[b]);
}

int lca(int a, int b){
    if (upper(a, b)) return a;
    if (upper(b, a)) return b;

    for (int po = PW - 1; po >= 0; po--)
        if (!upper(up[a][po], b))
            a = up[a][po];

    return up[a][0];
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);

    cin >> n >> m >> q;
    for (int i = 1; i < n; i++){
        int x, y; cin >> x >> y;
        x--; y--;
        g[x].PB(y);
        g[y].PB(x);
    }

    dfs(0, 0);

    for (int i = 0; i < m; i++){
        cin >> a[i];
        a[i]--;
        st[a[i]].insert(i);

        if (i > 0)
            pr[lca(a[i], a[i - 1])].insert(i);
    }

    for (; q; q--){
        int tp;
        cin >> tp;
        if (tp == 1){
            int ps, v; cin >> ps >> v;
            ps--; v--;
            st[a[ps]].erase(ps);
            if (ps > 0)
                pr[lca(a[ps], a[ps - 1])].erase(ps);
            if (ps + 1 < m)
                pr[lca(a[ps], a[ps + 1])].erase(ps + 1);

            a[ps] = v;

            st[a[ps]].insert(ps);
            if (ps > 0)
                pr[lca(a[ps], a[ps - 1])].insert(ps);
            if (ps + 1 < m)
                pr[lca(a[ps], a[ps + 1])].insert(ps + 1);
        } else {
            int l, r, v; cin >> l >> r >> v;
            l--; r--; v--;
            int x = -2, y = -2;
            if (sz(st[v])){
                it = st[v].lower_bound(l);
                if (it != st[v].end() && (*it) <= r){
                    y = x = (*it);
                }
            }

            if (x < 0 && sz(pr[v])){
                it = pr[v].upper_bound(l);
                if (it != pr[v].end() && (*it) <= r){
                    y = (*it);
                    x = y - 1;
                }
            }

            cout << x + 1 << " " << y + 1 << '\n';
        }
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23800 KB n=5
2 Correct 23 ms 23928 KB n=100
3 Correct 24 ms 23928 KB n=100
4 Correct 24 ms 23800 KB n=100
5 Correct 24 ms 23928 KB n=100
6 Correct 23 ms 23900 KB n=100
7 Correct 23 ms 23928 KB n=100
8 Correct 23 ms 23928 KB n=100
9 Correct 28 ms 23800 KB n=100
10 Correct 23 ms 23928 KB n=100
11 Correct 24 ms 23928 KB n=100
12 Correct 24 ms 23932 KB n=100
13 Correct 24 ms 23928 KB n=100
14 Correct 24 ms 23928 KB n=100
15 Correct 27 ms 23800 KB n=100
16 Correct 24 ms 23800 KB n=100
17 Correct 23 ms 23800 KB n=100
18 Correct 23 ms 23928 KB n=100
19 Correct 24 ms 23800 KB n=100
20 Correct 24 ms 23928 KB n=100
21 Correct 23 ms 23928 KB n=100
22 Correct 29 ms 23800 KB n=100
23 Correct 23 ms 23924 KB n=100
24 Correct 23 ms 23928 KB n=100
25 Correct 23 ms 23928 KB n=100
26 Correct 23 ms 23928 KB n=12
27 Correct 23 ms 23928 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23800 KB n=5
2 Correct 23 ms 23928 KB n=100
3 Correct 24 ms 23928 KB n=100
4 Correct 24 ms 23800 KB n=100
5 Correct 24 ms 23928 KB n=100
6 Correct 23 ms 23900 KB n=100
7 Correct 23 ms 23928 KB n=100
8 Correct 23 ms 23928 KB n=100
9 Correct 28 ms 23800 KB n=100
10 Correct 23 ms 23928 KB n=100
11 Correct 24 ms 23928 KB n=100
12 Correct 24 ms 23932 KB n=100
13 Correct 24 ms 23928 KB n=100
14 Correct 24 ms 23928 KB n=100
15 Correct 27 ms 23800 KB n=100
16 Correct 24 ms 23800 KB n=100
17 Correct 23 ms 23800 KB n=100
18 Correct 23 ms 23928 KB n=100
19 Correct 24 ms 23800 KB n=100
20 Correct 24 ms 23928 KB n=100
21 Correct 23 ms 23928 KB n=100
22 Correct 29 ms 23800 KB n=100
23 Correct 23 ms 23924 KB n=100
24 Correct 23 ms 23928 KB n=100
25 Correct 23 ms 23928 KB n=100
26 Correct 23 ms 23928 KB n=12
27 Correct 23 ms 23928 KB n=100
28 Correct 24 ms 24056 KB n=500
29 Correct 24 ms 24056 KB n=500
30 Correct 24 ms 23928 KB n=500
31 Correct 25 ms 23928 KB n=500
32 Correct 25 ms 24056 KB n=500
33 Correct 25 ms 24056 KB n=500
34 Correct 26 ms 23944 KB n=500
35 Correct 25 ms 24056 KB n=500
36 Correct 24 ms 24056 KB n=500
37 Correct 24 ms 24056 KB n=500
38 Correct 25 ms 24056 KB n=500
39 Correct 24 ms 24056 KB n=500
40 Correct 29 ms 24060 KB n=500
41 Correct 24 ms 24132 KB n=500
42 Correct 24 ms 24056 KB n=500
43 Correct 24 ms 24056 KB n=500
44 Correct 24 ms 24056 KB n=500
45 Correct 24 ms 23928 KB n=500
46 Correct 25 ms 24056 KB n=500
47 Correct 24 ms 23928 KB n=500
48 Correct 24 ms 23928 KB n=500
49 Correct 24 ms 24056 KB n=500
50 Correct 24 ms 24028 KB n=500
51 Correct 24 ms 24056 KB n=500
52 Correct 24 ms 24056 KB n=500
53 Correct 25 ms 24056 KB n=500
54 Correct 24 ms 23928 KB n=500
55 Correct 23 ms 23900 KB n=278
56 Correct 24 ms 24056 KB n=500
57 Correct 24 ms 24056 KB n=500
58 Correct 24 ms 24056 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23800 KB n=5
2 Correct 23 ms 23928 KB n=100
3 Correct 24 ms 23928 KB n=100
4 Correct 24 ms 23800 KB n=100
5 Correct 24 ms 23928 KB n=100
6 Correct 23 ms 23900 KB n=100
7 Correct 23 ms 23928 KB n=100
8 Correct 23 ms 23928 KB n=100
9 Correct 28 ms 23800 KB n=100
10 Correct 23 ms 23928 KB n=100
11 Correct 24 ms 23928 KB n=100
12 Correct 24 ms 23932 KB n=100
13 Correct 24 ms 23928 KB n=100
14 Correct 24 ms 23928 KB n=100
15 Correct 27 ms 23800 KB n=100
16 Correct 24 ms 23800 KB n=100
17 Correct 23 ms 23800 KB n=100
18 Correct 23 ms 23928 KB n=100
19 Correct 24 ms 23800 KB n=100
20 Correct 24 ms 23928 KB n=100
21 Correct 23 ms 23928 KB n=100
22 Correct 29 ms 23800 KB n=100
23 Correct 23 ms 23924 KB n=100
24 Correct 23 ms 23928 KB n=100
25 Correct 23 ms 23928 KB n=100
26 Correct 23 ms 23928 KB n=12
27 Correct 23 ms 23928 KB n=100
28 Correct 24 ms 24056 KB n=500
29 Correct 24 ms 24056 KB n=500
30 Correct 24 ms 23928 KB n=500
31 Correct 25 ms 23928 KB n=500
32 Correct 25 ms 24056 KB n=500
33 Correct 25 ms 24056 KB n=500
34 Correct 26 ms 23944 KB n=500
35 Correct 25 ms 24056 KB n=500
36 Correct 24 ms 24056 KB n=500
37 Correct 24 ms 24056 KB n=500
38 Correct 25 ms 24056 KB n=500
39 Correct 24 ms 24056 KB n=500
40 Correct 29 ms 24060 KB n=500
41 Correct 24 ms 24132 KB n=500
42 Correct 24 ms 24056 KB n=500
43 Correct 24 ms 24056 KB n=500
44 Correct 24 ms 24056 KB n=500
45 Correct 24 ms 23928 KB n=500
46 Correct 25 ms 24056 KB n=500
47 Correct 24 ms 23928 KB n=500
48 Correct 24 ms 23928 KB n=500
49 Correct 24 ms 24056 KB n=500
50 Correct 24 ms 24028 KB n=500
51 Correct 24 ms 24056 KB n=500
52 Correct 24 ms 24056 KB n=500
53 Correct 25 ms 24056 KB n=500
54 Correct 24 ms 23928 KB n=500
55 Correct 23 ms 23900 KB n=278
56 Correct 24 ms 24056 KB n=500
57 Correct 24 ms 24056 KB n=500
58 Correct 24 ms 24056 KB n=500
59 Correct 27 ms 24440 KB n=2000
60 Correct 27 ms 24440 KB n=2000
61 Correct 27 ms 24412 KB n=2000
62 Correct 28 ms 24456 KB n=2000
63 Correct 28 ms 24312 KB n=2000
64 Correct 34 ms 24312 KB n=2000
65 Correct 29 ms 24356 KB n=2000
66 Correct 27 ms 24440 KB n=2000
67 Correct 28 ms 24316 KB n=2000
68 Correct 28 ms 24440 KB n=2000
69 Correct 27 ms 24312 KB n=2000
70 Correct 27 ms 24316 KB n=2000
71 Correct 31 ms 24384 KB n=2000
72 Correct 27 ms 24312 KB n=2000
73 Correct 27 ms 24312 KB n=2000
74 Correct 27 ms 24312 KB n=1844
75 Correct 31 ms 24312 KB n=2000
76 Correct 28 ms 24312 KB n=2000
77 Correct 28 ms 24312 KB n=2000
78 Correct 27 ms 24440 KB n=2000
79 Correct 28 ms 24312 KB n=2000
80 Correct 26 ms 24440 KB n=2000
81 Correct 28 ms 24440 KB n=2000
82 Correct 28 ms 24312 KB n=2000
83 Correct 27 ms 24440 KB n=2000
84 Correct 28 ms 24440 KB n=2000
85 Correct 28 ms 24312 KB n=2000
86 Correct 28 ms 24312 KB n=2000
87 Correct 28 ms 24312 KB n=2000
88 Correct 27 ms 24440 KB n=2000
89 Correct 27 ms 24440 KB n=2000
90 Correct 27 ms 24440 KB n=2000
91 Correct 27 ms 24404 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23800 KB n=5
2 Correct 23 ms 23928 KB n=100
3 Correct 24 ms 23928 KB n=100
4 Correct 24 ms 23800 KB n=100
5 Correct 24 ms 23928 KB n=100
6 Correct 23 ms 23900 KB n=100
7 Correct 23 ms 23928 KB n=100
8 Correct 23 ms 23928 KB n=100
9 Correct 28 ms 23800 KB n=100
10 Correct 23 ms 23928 KB n=100
11 Correct 24 ms 23928 KB n=100
12 Correct 24 ms 23932 KB n=100
13 Correct 24 ms 23928 KB n=100
14 Correct 24 ms 23928 KB n=100
15 Correct 27 ms 23800 KB n=100
16 Correct 24 ms 23800 KB n=100
17 Correct 23 ms 23800 KB n=100
18 Correct 23 ms 23928 KB n=100
19 Correct 24 ms 23800 KB n=100
20 Correct 24 ms 23928 KB n=100
21 Correct 23 ms 23928 KB n=100
22 Correct 29 ms 23800 KB n=100
23 Correct 23 ms 23924 KB n=100
24 Correct 23 ms 23928 KB n=100
25 Correct 23 ms 23928 KB n=100
26 Correct 23 ms 23928 KB n=12
27 Correct 23 ms 23928 KB n=100
28 Correct 24 ms 24056 KB n=500
29 Correct 24 ms 24056 KB n=500
30 Correct 24 ms 23928 KB n=500
31 Correct 25 ms 23928 KB n=500
32 Correct 25 ms 24056 KB n=500
33 Correct 25 ms 24056 KB n=500
34 Correct 26 ms 23944 KB n=500
35 Correct 25 ms 24056 KB n=500
36 Correct 24 ms 24056 KB n=500
37 Correct 24 ms 24056 KB n=500
38 Correct 25 ms 24056 KB n=500
39 Correct 24 ms 24056 KB n=500
40 Correct 29 ms 24060 KB n=500
41 Correct 24 ms 24132 KB n=500
42 Correct 24 ms 24056 KB n=500
43 Correct 24 ms 24056 KB n=500
44 Correct 24 ms 24056 KB n=500
45 Correct 24 ms 23928 KB n=500
46 Correct 25 ms 24056 KB n=500
47 Correct 24 ms 23928 KB n=500
48 Correct 24 ms 23928 KB n=500
49 Correct 24 ms 24056 KB n=500
50 Correct 24 ms 24028 KB n=500
51 Correct 24 ms 24056 KB n=500
52 Correct 24 ms 24056 KB n=500
53 Correct 25 ms 24056 KB n=500
54 Correct 24 ms 23928 KB n=500
55 Correct 23 ms 23900 KB n=278
56 Correct 24 ms 24056 KB n=500
57 Correct 24 ms 24056 KB n=500
58 Correct 24 ms 24056 KB n=500
59 Correct 27 ms 24440 KB n=2000
60 Correct 27 ms 24440 KB n=2000
61 Correct 27 ms 24412 KB n=2000
62 Correct 28 ms 24456 KB n=2000
63 Correct 28 ms 24312 KB n=2000
64 Correct 34 ms 24312 KB n=2000
65 Correct 29 ms 24356 KB n=2000
66 Correct 27 ms 24440 KB n=2000
67 Correct 28 ms 24316 KB n=2000
68 Correct 28 ms 24440 KB n=2000
69 Correct 27 ms 24312 KB n=2000
70 Correct 27 ms 24316 KB n=2000
71 Correct 31 ms 24384 KB n=2000
72 Correct 27 ms 24312 KB n=2000
73 Correct 27 ms 24312 KB n=2000
74 Correct 27 ms 24312 KB n=1844
75 Correct 31 ms 24312 KB n=2000
76 Correct 28 ms 24312 KB n=2000
77 Correct 28 ms 24312 KB n=2000
78 Correct 27 ms 24440 KB n=2000
79 Correct 28 ms 24312 KB n=2000
80 Correct 26 ms 24440 KB n=2000
81 Correct 28 ms 24440 KB n=2000
82 Correct 28 ms 24312 KB n=2000
83 Correct 27 ms 24440 KB n=2000
84 Correct 28 ms 24440 KB n=2000
85 Correct 28 ms 24312 KB n=2000
86 Correct 28 ms 24312 KB n=2000
87 Correct 28 ms 24312 KB n=2000
88 Correct 27 ms 24440 KB n=2000
89 Correct 27 ms 24440 KB n=2000
90 Correct 27 ms 24440 KB n=2000
91 Correct 27 ms 24404 KB n=2000
92 Correct 914 ms 75504 KB n=200000
93 Correct 1131 ms 80504 KB n=200000
94 Correct 819 ms 83824 KB n=200000
95 Correct 889 ms 75292 KB n=200000
96 Correct 850 ms 75476 KB n=200000
97 Correct 1115 ms 79500 KB n=200000
98 Correct 922 ms 75460 KB n=200000
99 Correct 1058 ms 75564 KB n=200000
100 Correct 874 ms 75492 KB n=200000
101 Correct 725 ms 85044 KB n=200000
102 Correct 574 ms 76540 KB n=200000
103 Correct 559 ms 76408 KB n=200000
104 Correct 552 ms 76496 KB n=200000
105 Correct 572 ms 76828 KB n=200000
106 Correct 565 ms 76924 KB n=200000
107 Correct 626 ms 76964 KB n=200000
108 Correct 1034 ms 75504 KB n=200000
109 Correct 1059 ms 75436 KB n=200000
110 Correct 974 ms 75436 KB n=200000
111 Correct 900 ms 74740 KB n=200000
112 Correct 764 ms 84028 KB n=200000
113 Correct 1080 ms 79368 KB n=200000
114 Correct 942 ms 74760 KB n=200000
115 Correct 1303 ms 77212 KB n=200000
116 Correct 927 ms 75496 KB n=200000
117 Correct 752 ms 84216 KB n=200000
118 Correct 1220 ms 78296 KB n=200000
119 Correct 867 ms 75476 KB n=200000
120 Correct 667 ms 83924 KB n=200000
121 Correct 669 ms 84064 KB n=200000
122 Correct 696 ms 84412 KB n=200000
123 Correct 594 ms 76744 KB n=200000
124 Correct 240 ms 38940 KB n=25264