Submission #91022

# Submission time Handle Problem Language Result Execution time Memory
91022 2018-12-25T15:48:37 Z rkocharyan Birthday gift (IZhO18_treearray) C++14
100 / 100
1197 ms 105272 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 7;

int n, m, q;
vector <int> g[N];
int up[N][22], tin[N], tout[N];
int timer, l = 1;
set <int> f[N], s[N];

void dfs(int v, int p) {
    tin[v] = ++timer;
    up[v][0] = p;
    for(int i = 1; i <= l; i++) {
        up[v][i] = up[up[v][i - 1]][i - 1];
    }
    for(int u : g[v]) {
        if(u == p) continue;
        dfs(u, v);
    }
    tout[v] = ++timer;
}

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

int lca(int u, int v) {
    if(upper(u, v)) return u;
    if(upper(v, u)) return v;
    for(int i = l; i >= 0; i--) {
        if(!upper(up[u][i], v)) {
            u = up[u][i];
        }
    }
    return up[u][0];
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m >> q;
    for(int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    while((1 << l) <= n) l++;
    dfs(1, 1);
    vector <int> a(m);
    for(int i = 0; i < m; i++) {
        cin >> a[i];
        f[a[i]].insert(i);
    }
    for(int i = 0; i < m - 1; i++) {
        s[lca(a[i], a[i + 1])].insert(i);
    }
    for(; q; --q) {
        int t;
        cin >> t;
        if(t == 1) {
            int pos, val;
            cin >> pos >> val;
            pos--;
            f[a[pos]].erase(pos);
            f[val].insert(pos);
            if(pos > 0) s[lca(a[pos], a[pos - 1])].erase(pos - 1);
            if(pos < m - 1) s[lca(a[pos], a[pos + 1])].erase(pos);
            a[pos] = val;
            if(pos > 0) s[lca(a[pos], a[pos - 1])].insert(pos - 1);
            if(pos < m - 1) s[lca(a[pos], a[pos + 1])].insert(pos);
        }
        else {
            int l, r, val;
            cin >> l >> r >> val;
            l--, r--;
            if(l > r) swap(l, r);
            pair <int, int> ans = { -1, -1 };
            if(!f[val].empty()) {
                auto it = f[val].lower_bound(l);
                if(it != f[val].end() && *it <= r) {
                    ans = { *it, *it };
                }
            }
            if(!s[val].empty()) {
                auto it = s[val].lower_bound(l);
                if(it != s[val].end() && *it + 1 <= r) {
                    ans = { *it, *it + 1 };
                }
            }
            if(ans.first != -1) {
                ans.first++;
                ans.second++;
            }
            cout << ans.first << ' ' << ans.second << '\n';
        }
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23800 KB n=5
2 Correct 23 ms 23948 KB n=100
3 Correct 23 ms 23948 KB n=100
4 Correct 22 ms 23948 KB n=100
5 Correct 23 ms 23952 KB n=100
6 Correct 22 ms 23996 KB n=100
7 Correct 22 ms 24156 KB n=100
8 Correct 22 ms 24156 KB n=100
9 Correct 22 ms 24156 KB n=100
10 Correct 22 ms 24156 KB n=100
11 Correct 23 ms 24156 KB n=100
12 Correct 22 ms 24156 KB n=100
13 Correct 22 ms 24156 KB n=100
14 Correct 23 ms 24156 KB n=100
15 Correct 22 ms 24156 KB n=100
16 Correct 22 ms 24156 KB n=100
17 Correct 22 ms 24156 KB n=100
18 Correct 22 ms 24156 KB n=100
19 Correct 23 ms 24156 KB n=100
20 Correct 23 ms 24156 KB n=100
21 Correct 23 ms 24156 KB n=100
22 Correct 23 ms 24156 KB n=100
23 Correct 23 ms 24156 KB n=100
24 Correct 22 ms 24156 KB n=100
25 Correct 21 ms 24156 KB n=100
26 Correct 21 ms 24156 KB n=12
27 Correct 21 ms 24156 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23800 KB n=5
2 Correct 23 ms 23948 KB n=100
3 Correct 23 ms 23948 KB n=100
4 Correct 22 ms 23948 KB n=100
5 Correct 23 ms 23952 KB n=100
6 Correct 22 ms 23996 KB n=100
7 Correct 22 ms 24156 KB n=100
8 Correct 22 ms 24156 KB n=100
9 Correct 22 ms 24156 KB n=100
10 Correct 22 ms 24156 KB n=100
11 Correct 23 ms 24156 KB n=100
12 Correct 22 ms 24156 KB n=100
13 Correct 22 ms 24156 KB n=100
14 Correct 23 ms 24156 KB n=100
15 Correct 22 ms 24156 KB n=100
16 Correct 22 ms 24156 KB n=100
17 Correct 22 ms 24156 KB n=100
18 Correct 22 ms 24156 KB n=100
19 Correct 23 ms 24156 KB n=100
20 Correct 23 ms 24156 KB n=100
21 Correct 23 ms 24156 KB n=100
22 Correct 23 ms 24156 KB n=100
23 Correct 23 ms 24156 KB n=100
24 Correct 22 ms 24156 KB n=100
25 Correct 21 ms 24156 KB n=100
26 Correct 21 ms 24156 KB n=12
27 Correct 21 ms 24156 KB n=100
28 Correct 23 ms 24172 KB n=500
29 Correct 23 ms 24172 KB n=500
30 Correct 23 ms 24172 KB n=500
31 Correct 22 ms 24172 KB n=500
32 Correct 23 ms 24224 KB n=500
33 Correct 23 ms 24224 KB n=500
34 Correct 23 ms 24224 KB n=500
35 Correct 23 ms 24224 KB n=500
36 Correct 23 ms 24224 KB n=500
37 Correct 20 ms 24224 KB n=500
38 Correct 23 ms 24224 KB n=500
39 Correct 23 ms 24232 KB n=500
40 Correct 23 ms 24232 KB n=500
41 Correct 23 ms 24232 KB n=500
42 Correct 20 ms 24232 KB n=500
43 Correct 24 ms 24232 KB n=500
44 Correct 20 ms 24232 KB n=500
45 Correct 23 ms 24232 KB n=500
46 Correct 23 ms 24232 KB n=500
47 Correct 24 ms 24232 KB n=500
48 Correct 23 ms 24232 KB n=500
49 Correct 23 ms 24232 KB n=500
50 Correct 20 ms 24232 KB n=500
51 Correct 20 ms 24232 KB n=500
52 Correct 20 ms 24232 KB n=500
53 Correct 22 ms 24232 KB n=500
54 Correct 23 ms 24232 KB n=500
55 Correct 22 ms 24232 KB n=278
56 Correct 24 ms 24232 KB n=500
57 Correct 23 ms 24232 KB n=500
58 Correct 23 ms 24232 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23800 KB n=5
2 Correct 23 ms 23948 KB n=100
3 Correct 23 ms 23948 KB n=100
4 Correct 22 ms 23948 KB n=100
5 Correct 23 ms 23952 KB n=100
6 Correct 22 ms 23996 KB n=100
7 Correct 22 ms 24156 KB n=100
8 Correct 22 ms 24156 KB n=100
9 Correct 22 ms 24156 KB n=100
10 Correct 22 ms 24156 KB n=100
11 Correct 23 ms 24156 KB n=100
12 Correct 22 ms 24156 KB n=100
13 Correct 22 ms 24156 KB n=100
14 Correct 23 ms 24156 KB n=100
15 Correct 22 ms 24156 KB n=100
16 Correct 22 ms 24156 KB n=100
17 Correct 22 ms 24156 KB n=100
18 Correct 22 ms 24156 KB n=100
19 Correct 23 ms 24156 KB n=100
20 Correct 23 ms 24156 KB n=100
21 Correct 23 ms 24156 KB n=100
22 Correct 23 ms 24156 KB n=100
23 Correct 23 ms 24156 KB n=100
24 Correct 22 ms 24156 KB n=100
25 Correct 21 ms 24156 KB n=100
26 Correct 21 ms 24156 KB n=12
27 Correct 21 ms 24156 KB n=100
28 Correct 23 ms 24172 KB n=500
29 Correct 23 ms 24172 KB n=500
30 Correct 23 ms 24172 KB n=500
31 Correct 22 ms 24172 KB n=500
32 Correct 23 ms 24224 KB n=500
33 Correct 23 ms 24224 KB n=500
34 Correct 23 ms 24224 KB n=500
35 Correct 23 ms 24224 KB n=500
36 Correct 23 ms 24224 KB n=500
37 Correct 20 ms 24224 KB n=500
38 Correct 23 ms 24224 KB n=500
39 Correct 23 ms 24232 KB n=500
40 Correct 23 ms 24232 KB n=500
41 Correct 23 ms 24232 KB n=500
42 Correct 20 ms 24232 KB n=500
43 Correct 24 ms 24232 KB n=500
44 Correct 20 ms 24232 KB n=500
45 Correct 23 ms 24232 KB n=500
46 Correct 23 ms 24232 KB n=500
47 Correct 24 ms 24232 KB n=500
48 Correct 23 ms 24232 KB n=500
49 Correct 23 ms 24232 KB n=500
50 Correct 20 ms 24232 KB n=500
51 Correct 20 ms 24232 KB n=500
52 Correct 20 ms 24232 KB n=500
53 Correct 22 ms 24232 KB n=500
54 Correct 23 ms 24232 KB n=500
55 Correct 22 ms 24232 KB n=278
56 Correct 24 ms 24232 KB n=500
57 Correct 23 ms 24232 KB n=500
58 Correct 23 ms 24232 KB n=500
59 Correct 27 ms 24700 KB n=2000
60 Correct 25 ms 24700 KB n=2000
61 Correct 25 ms 24700 KB n=2000
62 Correct 26 ms 24700 KB n=2000
63 Correct 26 ms 24700 KB n=2000
64 Correct 26 ms 24700 KB n=2000
65 Correct 26 ms 24700 KB n=2000
66 Correct 26 ms 24700 KB n=2000
67 Correct 27 ms 24700 KB n=2000
68 Correct 27 ms 24700 KB n=2000
69 Correct 27 ms 24700 KB n=2000
70 Correct 26 ms 24700 KB n=2000
71 Correct 25 ms 24700 KB n=2000
72 Correct 25 ms 24700 KB n=2000
73 Correct 26 ms 24700 KB n=2000
74 Correct 26 ms 24700 KB n=1844
75 Correct 22 ms 24700 KB n=2000
76 Correct 26 ms 24700 KB n=2000
77 Correct 27 ms 24700 KB n=2000
78 Correct 27 ms 24700 KB n=2000
79 Correct 27 ms 24700 KB n=2000
80 Correct 25 ms 24700 KB n=2000
81 Correct 25 ms 24700 KB n=2000
82 Correct 26 ms 24700 KB n=2000
83 Correct 27 ms 24700 KB n=2000
84 Correct 27 ms 24700 KB n=2000
85 Correct 25 ms 24700 KB n=2000
86 Correct 26 ms 24700 KB n=2000
87 Correct 25 ms 24700 KB n=2000
88 Correct 25 ms 24700 KB n=2000
89 Correct 25 ms 24700 KB n=2000
90 Correct 26 ms 24772 KB n=2000
91 Correct 25 ms 24772 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23800 KB n=5
2 Correct 23 ms 23948 KB n=100
3 Correct 23 ms 23948 KB n=100
4 Correct 22 ms 23948 KB n=100
5 Correct 23 ms 23952 KB n=100
6 Correct 22 ms 23996 KB n=100
7 Correct 22 ms 24156 KB n=100
8 Correct 22 ms 24156 KB n=100
9 Correct 22 ms 24156 KB n=100
10 Correct 22 ms 24156 KB n=100
11 Correct 23 ms 24156 KB n=100
12 Correct 22 ms 24156 KB n=100
13 Correct 22 ms 24156 KB n=100
14 Correct 23 ms 24156 KB n=100
15 Correct 22 ms 24156 KB n=100
16 Correct 22 ms 24156 KB n=100
17 Correct 22 ms 24156 KB n=100
18 Correct 22 ms 24156 KB n=100
19 Correct 23 ms 24156 KB n=100
20 Correct 23 ms 24156 KB n=100
21 Correct 23 ms 24156 KB n=100
22 Correct 23 ms 24156 KB n=100
23 Correct 23 ms 24156 KB n=100
24 Correct 22 ms 24156 KB n=100
25 Correct 21 ms 24156 KB n=100
26 Correct 21 ms 24156 KB n=12
27 Correct 21 ms 24156 KB n=100
28 Correct 23 ms 24172 KB n=500
29 Correct 23 ms 24172 KB n=500
30 Correct 23 ms 24172 KB n=500
31 Correct 22 ms 24172 KB n=500
32 Correct 23 ms 24224 KB n=500
33 Correct 23 ms 24224 KB n=500
34 Correct 23 ms 24224 KB n=500
35 Correct 23 ms 24224 KB n=500
36 Correct 23 ms 24224 KB n=500
37 Correct 20 ms 24224 KB n=500
38 Correct 23 ms 24224 KB n=500
39 Correct 23 ms 24232 KB n=500
40 Correct 23 ms 24232 KB n=500
41 Correct 23 ms 24232 KB n=500
42 Correct 20 ms 24232 KB n=500
43 Correct 24 ms 24232 KB n=500
44 Correct 20 ms 24232 KB n=500
45 Correct 23 ms 24232 KB n=500
46 Correct 23 ms 24232 KB n=500
47 Correct 24 ms 24232 KB n=500
48 Correct 23 ms 24232 KB n=500
49 Correct 23 ms 24232 KB n=500
50 Correct 20 ms 24232 KB n=500
51 Correct 20 ms 24232 KB n=500
52 Correct 20 ms 24232 KB n=500
53 Correct 22 ms 24232 KB n=500
54 Correct 23 ms 24232 KB n=500
55 Correct 22 ms 24232 KB n=278
56 Correct 24 ms 24232 KB n=500
57 Correct 23 ms 24232 KB n=500
58 Correct 23 ms 24232 KB n=500
59 Correct 27 ms 24700 KB n=2000
60 Correct 25 ms 24700 KB n=2000
61 Correct 25 ms 24700 KB n=2000
62 Correct 26 ms 24700 KB n=2000
63 Correct 26 ms 24700 KB n=2000
64 Correct 26 ms 24700 KB n=2000
65 Correct 26 ms 24700 KB n=2000
66 Correct 26 ms 24700 KB n=2000
67 Correct 27 ms 24700 KB n=2000
68 Correct 27 ms 24700 KB n=2000
69 Correct 27 ms 24700 KB n=2000
70 Correct 26 ms 24700 KB n=2000
71 Correct 25 ms 24700 KB n=2000
72 Correct 25 ms 24700 KB n=2000
73 Correct 26 ms 24700 KB n=2000
74 Correct 26 ms 24700 KB n=1844
75 Correct 22 ms 24700 KB n=2000
76 Correct 26 ms 24700 KB n=2000
77 Correct 27 ms 24700 KB n=2000
78 Correct 27 ms 24700 KB n=2000
79 Correct 27 ms 24700 KB n=2000
80 Correct 25 ms 24700 KB n=2000
81 Correct 25 ms 24700 KB n=2000
82 Correct 26 ms 24700 KB n=2000
83 Correct 27 ms 24700 KB n=2000
84 Correct 27 ms 24700 KB n=2000
85 Correct 25 ms 24700 KB n=2000
86 Correct 26 ms 24700 KB n=2000
87 Correct 25 ms 24700 KB n=2000
88 Correct 25 ms 24700 KB n=2000
89 Correct 25 ms 24700 KB n=2000
90 Correct 26 ms 24772 KB n=2000
91 Correct 25 ms 24772 KB n=2000
92 Correct 1025 ms 71148 KB n=200000
93 Correct 1143 ms 77872 KB n=200000
94 Correct 666 ms 81100 KB n=200000
95 Correct 925 ms 81100 KB n=200000
96 Correct 1026 ms 81100 KB n=200000
97 Correct 1197 ms 81100 KB n=200000
98 Correct 795 ms 81100 KB n=200000
99 Correct 945 ms 81100 KB n=200000
100 Correct 905 ms 81100 KB n=200000
101 Correct 748 ms 89308 KB n=200000
102 Correct 507 ms 89308 KB n=200000
103 Correct 532 ms 89308 KB n=200000
104 Correct 530 ms 89308 KB n=200000
105 Correct 567 ms 89308 KB n=200000
106 Correct 550 ms 89308 KB n=200000
107 Correct 542 ms 89308 KB n=200000
108 Correct 1029 ms 89308 KB n=200000
109 Correct 1024 ms 89308 KB n=200000
110 Correct 953 ms 89308 KB n=200000
111 Correct 841 ms 89308 KB n=200000
112 Correct 720 ms 97580 KB n=200000
113 Correct 955 ms 97580 KB n=200000
114 Correct 903 ms 97580 KB n=200000
115 Correct 1138 ms 97580 KB n=200000
116 Correct 843 ms 97580 KB n=200000
117 Correct 626 ms 102188 KB n=200000
118 Correct 1142 ms 102188 KB n=200000
119 Correct 759 ms 102188 KB n=200000
120 Correct 621 ms 104620 KB n=200000
121 Correct 730 ms 104760 KB n=200000
122 Correct 609 ms 105272 KB n=200000
123 Correct 563 ms 105272 KB n=200000
124 Correct 203 ms 105272 KB n=25264