답안 #91027

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
91027 2018-12-25T15:58:49 Z rkocharyan Birthday gift (IZhO18_treearray) C++14
100 / 100
1355 ms 79292 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].upper_bound(l - 1);
                if(it != f[val].end() && *it <= r) {
                    ans = { *it, *it };
                }
            }
            if(!s[val].empty()) {
                auto it = s[val].upper_bound(l - 1);
                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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 23800 KB n=5
2 Correct 23 ms 23800 KB n=100
3 Correct 19 ms 23816 KB n=100
4 Correct 19 ms 23944 KB n=100
5 Correct 19 ms 24020 KB n=100
6 Correct 23 ms 24096 KB n=100
7 Correct 19 ms 24096 KB n=100
8 Correct 19 ms 24096 KB n=100
9 Correct 19 ms 24096 KB n=100
10 Correct 23 ms 24096 KB n=100
11 Correct 19 ms 24096 KB n=100
12 Correct 19 ms 24096 KB n=100
13 Correct 18 ms 24096 KB n=100
14 Correct 23 ms 24096 KB n=100
15 Correct 22 ms 24096 KB n=100
16 Correct 22 ms 24096 KB n=100
17 Correct 20 ms 24176 KB n=100
18 Correct 19 ms 24176 KB n=100
19 Correct 20 ms 24176 KB n=100
20 Correct 19 ms 24176 KB n=100
21 Correct 18 ms 24176 KB n=100
22 Correct 19 ms 24176 KB n=100
23 Correct 19 ms 24176 KB n=100
24 Correct 19 ms 24176 KB n=100
25 Correct 19 ms 24176 KB n=100
26 Correct 18 ms 24176 KB n=12
27 Correct 19 ms 24176 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 23800 KB n=5
2 Correct 23 ms 23800 KB n=100
3 Correct 19 ms 23816 KB n=100
4 Correct 19 ms 23944 KB n=100
5 Correct 19 ms 24020 KB n=100
6 Correct 23 ms 24096 KB n=100
7 Correct 19 ms 24096 KB n=100
8 Correct 19 ms 24096 KB n=100
9 Correct 19 ms 24096 KB n=100
10 Correct 23 ms 24096 KB n=100
11 Correct 19 ms 24096 KB n=100
12 Correct 19 ms 24096 KB n=100
13 Correct 18 ms 24096 KB n=100
14 Correct 23 ms 24096 KB n=100
15 Correct 22 ms 24096 KB n=100
16 Correct 22 ms 24096 KB n=100
17 Correct 20 ms 24176 KB n=100
18 Correct 19 ms 24176 KB n=100
19 Correct 20 ms 24176 KB n=100
20 Correct 19 ms 24176 KB n=100
21 Correct 18 ms 24176 KB n=100
22 Correct 19 ms 24176 KB n=100
23 Correct 19 ms 24176 KB n=100
24 Correct 19 ms 24176 KB n=100
25 Correct 19 ms 24176 KB n=100
26 Correct 18 ms 24176 KB n=12
27 Correct 19 ms 24176 KB n=100
28 Correct 24 ms 24176 KB n=500
29 Correct 24 ms 24176 KB n=500
30 Correct 25 ms 24244 KB n=500
31 Correct 22 ms 24244 KB n=500
32 Correct 24 ms 24244 KB n=500
33 Correct 24 ms 24244 KB n=500
34 Correct 23 ms 24244 KB n=500
35 Correct 23 ms 24244 KB n=500
36 Correct 23 ms 24244 KB n=500
37 Correct 23 ms 24244 KB n=500
38 Correct 24 ms 24244 KB n=500
39 Correct 21 ms 24244 KB n=500
40 Correct 19 ms 24244 KB n=500
41 Correct 19 ms 24244 KB n=500
42 Correct 19 ms 24244 KB n=500
43 Correct 23 ms 24348 KB n=500
44 Correct 23 ms 24348 KB n=500
45 Correct 22 ms 24348 KB n=500
46 Correct 22 ms 24348 KB n=500
47 Correct 25 ms 24348 KB n=500
48 Correct 19 ms 24348 KB n=500
49 Correct 23 ms 24348 KB n=500
50 Correct 23 ms 24348 KB n=500
51 Correct 20 ms 24348 KB n=500
52 Correct 20 ms 24348 KB n=500
53 Correct 20 ms 24348 KB n=500
54 Correct 20 ms 24348 KB n=500
55 Correct 19 ms 24348 KB n=278
56 Correct 38 ms 24348 KB n=500
57 Correct 20 ms 24348 KB n=500
58 Correct 20 ms 24348 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 23800 KB n=5
2 Correct 23 ms 23800 KB n=100
3 Correct 19 ms 23816 KB n=100
4 Correct 19 ms 23944 KB n=100
5 Correct 19 ms 24020 KB n=100
6 Correct 23 ms 24096 KB n=100
7 Correct 19 ms 24096 KB n=100
8 Correct 19 ms 24096 KB n=100
9 Correct 19 ms 24096 KB n=100
10 Correct 23 ms 24096 KB n=100
11 Correct 19 ms 24096 KB n=100
12 Correct 19 ms 24096 KB n=100
13 Correct 18 ms 24096 KB n=100
14 Correct 23 ms 24096 KB n=100
15 Correct 22 ms 24096 KB n=100
16 Correct 22 ms 24096 KB n=100
17 Correct 20 ms 24176 KB n=100
18 Correct 19 ms 24176 KB n=100
19 Correct 20 ms 24176 KB n=100
20 Correct 19 ms 24176 KB n=100
21 Correct 18 ms 24176 KB n=100
22 Correct 19 ms 24176 KB n=100
23 Correct 19 ms 24176 KB n=100
24 Correct 19 ms 24176 KB n=100
25 Correct 19 ms 24176 KB n=100
26 Correct 18 ms 24176 KB n=12
27 Correct 19 ms 24176 KB n=100
28 Correct 24 ms 24176 KB n=500
29 Correct 24 ms 24176 KB n=500
30 Correct 25 ms 24244 KB n=500
31 Correct 22 ms 24244 KB n=500
32 Correct 24 ms 24244 KB n=500
33 Correct 24 ms 24244 KB n=500
34 Correct 23 ms 24244 KB n=500
35 Correct 23 ms 24244 KB n=500
36 Correct 23 ms 24244 KB n=500
37 Correct 23 ms 24244 KB n=500
38 Correct 24 ms 24244 KB n=500
39 Correct 21 ms 24244 KB n=500
40 Correct 19 ms 24244 KB n=500
41 Correct 19 ms 24244 KB n=500
42 Correct 19 ms 24244 KB n=500
43 Correct 23 ms 24348 KB n=500
44 Correct 23 ms 24348 KB n=500
45 Correct 22 ms 24348 KB n=500
46 Correct 22 ms 24348 KB n=500
47 Correct 25 ms 24348 KB n=500
48 Correct 19 ms 24348 KB n=500
49 Correct 23 ms 24348 KB n=500
50 Correct 23 ms 24348 KB n=500
51 Correct 20 ms 24348 KB n=500
52 Correct 20 ms 24348 KB n=500
53 Correct 20 ms 24348 KB n=500
54 Correct 20 ms 24348 KB n=500
55 Correct 19 ms 24348 KB n=278
56 Correct 38 ms 24348 KB n=500
57 Correct 20 ms 24348 KB n=500
58 Correct 20 ms 24348 KB n=500
59 Correct 22 ms 24556 KB n=2000
60 Correct 22 ms 24640 KB n=2000
61 Correct 54 ms 24640 KB n=2000
62 Correct 24 ms 24640 KB n=2000
63 Correct 23 ms 24640 KB n=2000
64 Correct 23 ms 24644 KB n=2000
65 Correct 23 ms 24644 KB n=2000
66 Correct 25 ms 24644 KB n=2000
67 Correct 28 ms 24644 KB n=2000
68 Correct 27 ms 24644 KB n=2000
69 Correct 25 ms 24644 KB n=2000
70 Correct 26 ms 24660 KB n=2000
71 Correct 26 ms 24664 KB n=2000
72 Correct 26 ms 24664 KB n=2000
73 Correct 22 ms 24664 KB n=2000
74 Correct 23 ms 24664 KB n=1844
75 Correct 23 ms 24664 KB n=2000
76 Correct 23 ms 24664 KB n=2000
77 Correct 22 ms 24664 KB n=2000
78 Correct 23 ms 24664 KB n=2000
79 Correct 23 ms 24664 KB n=2000
80 Correct 22 ms 24664 KB n=2000
81 Correct 23 ms 24664 KB n=2000
82 Correct 23 ms 24664 KB n=2000
83 Correct 22 ms 24664 KB n=2000
84 Correct 22 ms 24664 KB n=2000
85 Correct 23 ms 24664 KB n=2000
86 Correct 23 ms 24664 KB n=2000
87 Correct 22 ms 24664 KB n=2000
88 Correct 22 ms 24664 KB n=2000
89 Correct 22 ms 24664 KB n=2000
90 Correct 25 ms 24664 KB n=2000
91 Correct 26 ms 24664 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 23800 KB n=5
2 Correct 23 ms 23800 KB n=100
3 Correct 19 ms 23816 KB n=100
4 Correct 19 ms 23944 KB n=100
5 Correct 19 ms 24020 KB n=100
6 Correct 23 ms 24096 KB n=100
7 Correct 19 ms 24096 KB n=100
8 Correct 19 ms 24096 KB n=100
9 Correct 19 ms 24096 KB n=100
10 Correct 23 ms 24096 KB n=100
11 Correct 19 ms 24096 KB n=100
12 Correct 19 ms 24096 KB n=100
13 Correct 18 ms 24096 KB n=100
14 Correct 23 ms 24096 KB n=100
15 Correct 22 ms 24096 KB n=100
16 Correct 22 ms 24096 KB n=100
17 Correct 20 ms 24176 KB n=100
18 Correct 19 ms 24176 KB n=100
19 Correct 20 ms 24176 KB n=100
20 Correct 19 ms 24176 KB n=100
21 Correct 18 ms 24176 KB n=100
22 Correct 19 ms 24176 KB n=100
23 Correct 19 ms 24176 KB n=100
24 Correct 19 ms 24176 KB n=100
25 Correct 19 ms 24176 KB n=100
26 Correct 18 ms 24176 KB n=12
27 Correct 19 ms 24176 KB n=100
28 Correct 24 ms 24176 KB n=500
29 Correct 24 ms 24176 KB n=500
30 Correct 25 ms 24244 KB n=500
31 Correct 22 ms 24244 KB n=500
32 Correct 24 ms 24244 KB n=500
33 Correct 24 ms 24244 KB n=500
34 Correct 23 ms 24244 KB n=500
35 Correct 23 ms 24244 KB n=500
36 Correct 23 ms 24244 KB n=500
37 Correct 23 ms 24244 KB n=500
38 Correct 24 ms 24244 KB n=500
39 Correct 21 ms 24244 KB n=500
40 Correct 19 ms 24244 KB n=500
41 Correct 19 ms 24244 KB n=500
42 Correct 19 ms 24244 KB n=500
43 Correct 23 ms 24348 KB n=500
44 Correct 23 ms 24348 KB n=500
45 Correct 22 ms 24348 KB n=500
46 Correct 22 ms 24348 KB n=500
47 Correct 25 ms 24348 KB n=500
48 Correct 19 ms 24348 KB n=500
49 Correct 23 ms 24348 KB n=500
50 Correct 23 ms 24348 KB n=500
51 Correct 20 ms 24348 KB n=500
52 Correct 20 ms 24348 KB n=500
53 Correct 20 ms 24348 KB n=500
54 Correct 20 ms 24348 KB n=500
55 Correct 19 ms 24348 KB n=278
56 Correct 38 ms 24348 KB n=500
57 Correct 20 ms 24348 KB n=500
58 Correct 20 ms 24348 KB n=500
59 Correct 22 ms 24556 KB n=2000
60 Correct 22 ms 24640 KB n=2000
61 Correct 54 ms 24640 KB n=2000
62 Correct 24 ms 24640 KB n=2000
63 Correct 23 ms 24640 KB n=2000
64 Correct 23 ms 24644 KB n=2000
65 Correct 23 ms 24644 KB n=2000
66 Correct 25 ms 24644 KB n=2000
67 Correct 28 ms 24644 KB n=2000
68 Correct 27 ms 24644 KB n=2000
69 Correct 25 ms 24644 KB n=2000
70 Correct 26 ms 24660 KB n=2000
71 Correct 26 ms 24664 KB n=2000
72 Correct 26 ms 24664 KB n=2000
73 Correct 22 ms 24664 KB n=2000
74 Correct 23 ms 24664 KB n=1844
75 Correct 23 ms 24664 KB n=2000
76 Correct 23 ms 24664 KB n=2000
77 Correct 22 ms 24664 KB n=2000
78 Correct 23 ms 24664 KB n=2000
79 Correct 23 ms 24664 KB n=2000
80 Correct 22 ms 24664 KB n=2000
81 Correct 23 ms 24664 KB n=2000
82 Correct 23 ms 24664 KB n=2000
83 Correct 22 ms 24664 KB n=2000
84 Correct 22 ms 24664 KB n=2000
85 Correct 23 ms 24664 KB n=2000
86 Correct 23 ms 24664 KB n=2000
87 Correct 22 ms 24664 KB n=2000
88 Correct 22 ms 24664 KB n=2000
89 Correct 22 ms 24664 KB n=2000
90 Correct 25 ms 24664 KB n=2000
91 Correct 26 ms 24664 KB n=2000
92 Correct 851 ms 70712 KB n=200000
93 Correct 889 ms 74392 KB n=200000
94 Correct 714 ms 77848 KB n=200000
95 Correct 1039 ms 77848 KB n=200000
96 Correct 917 ms 77848 KB n=200000
97 Correct 1115 ms 77848 KB n=200000
98 Correct 1040 ms 77848 KB n=200000
99 Correct 1160 ms 77848 KB n=200000
100 Correct 991 ms 77848 KB n=200000
101 Correct 787 ms 79140 KB n=200000
102 Correct 550 ms 79140 KB n=200000
103 Correct 541 ms 79140 KB n=200000
104 Correct 481 ms 79140 KB n=200000
105 Correct 559 ms 79140 KB n=200000
106 Correct 586 ms 79140 KB n=200000
107 Correct 545 ms 79140 KB n=200000
108 Correct 1080 ms 79140 KB n=200000
109 Correct 1054 ms 79140 KB n=200000
110 Correct 1008 ms 79140 KB n=200000
111 Correct 1047 ms 79140 KB n=200000
112 Correct 826 ms 79140 KB n=200000
113 Correct 1131 ms 79140 KB n=200000
114 Correct 832 ms 79140 KB n=200000
115 Correct 1355 ms 79140 KB n=200000
116 Correct 816 ms 79140 KB n=200000
117 Correct 842 ms 79140 KB n=200000
118 Correct 1330 ms 79140 KB n=200000
119 Correct 991 ms 79140 KB n=200000
120 Correct 643 ms 79140 KB n=200000
121 Correct 558 ms 79140 KB n=200000
122 Correct 556 ms 79292 KB n=200000
123 Correct 590 ms 79292 KB n=200000
124 Correct 257 ms 79292 KB n=25264