답안 #338039

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
338039 2020-12-22T10:17:36 Z arujbansal Birthday gift (IZhO18_treearray) C++17
100 / 100
1545 ms 79084 KB
#include <bits/stdc++.h>

#define rng_init mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
using namespace std;
using ll = long long;

const int MAXN = 2e5 + 5, K = 20;
int n, m, q, timer;
int a[MAXN], tin[2 * MAXN], tout[2 * MAXN], par[MAXN][K];
vector<int> g[MAXN];

void dfs(int u, int p) {
    tin[u] = timer++;

    for (int j = 1; j < K; j++)
        par[u][j] = par[par[u][j - 1]][j - 1];

    for (const auto &v : g[u]) {
        if (v == p) continue;

        par[v][0] = u;
        dfs(v, u);
    }

    tout[u] = timer++;
}

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

int query(int u, int v) {
    if (isAnc(u, v)) return u;
    if (isAnc(v, u)) return v;

    for (int j = K - 1; j >= 0; j--)
        if (!isAnc(par[u][j], v))
            u = par[u][j];

    return par[u][0];
}

void solve() {
    cin >> n >> m >> q;

    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        u--, v--;

        g[u].push_back(v);
        g[v].push_back(u);
    }

    timer = 0;
    dfs(0, -1);

    set<pair<int, int>> individual;
    vector<set<int>> lca(n + 1);    

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

        individual.emplace(a[i], i);
    }

    for (int i = 0; i < m - 1; i++) {
        lca[query(a[i], a[i + 1])].insert(i);
    }

    while (q--) {
        int type;
        cin >> type;

        if (type == 1) {
            int pos, val;
            cin >> pos >> val;
            pos--, val--;

            individual.erase(make_pair(a[pos], pos));
            if (pos > 0) lca[query(a[pos], a[pos - 1])].erase(pos - 1);
            if (pos < m - 1) lca[query(a[pos], a[pos + 1])].erase(pos);

            a[pos] = val;

            individual.emplace(a[pos], pos);
            if (pos > 0) lca[query(a[pos], a[pos - 1])].insert(pos - 1);
            if (pos < m - 1) lca[query(a[pos], a[pos + 1])].insert(pos);
        } else {
            int l, r, val;
            cin >> l >> r >> val;
            l--, r--, val--;

            auto single = individual.lower_bound(make_pair(val, l));
            if (single != individual.end()) {
                int idx = (*single).second;

                if ((*single).first == val && l <= idx && idx <= r) {
                    cout << idx + 1 << " " << idx + 1 << "\n";
                    continue;
                }
            }

            auto adjacent = lca[val].lower_bound(l);
            if (adjacent == lca[val].end() || *adjacent >= r) {
                cout << "-1 -1\n";
                continue;
            }

            cout << *adjacent + 1 << " " << *adjacent + 2 << "\n";
        }
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int tc = 1;
    // cin >> tc;
    while (tc--) solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB n=5
2 Correct 4 ms 5132 KB n=100
3 Correct 4 ms 5100 KB n=100
4 Correct 4 ms 5120 KB n=100
5 Correct 4 ms 5112 KB n=100
6 Correct 4 ms 5100 KB n=100
7 Correct 4 ms 5100 KB n=100
8 Correct 4 ms 5100 KB n=100
9 Correct 4 ms 5100 KB n=100
10 Correct 4 ms 5100 KB n=100
11 Correct 4 ms 5100 KB n=100
12 Correct 4 ms 5100 KB n=100
13 Correct 4 ms 5100 KB n=100
14 Correct 4 ms 5100 KB n=100
15 Correct 4 ms 5100 KB n=100
16 Correct 4 ms 5100 KB n=100
17 Correct 4 ms 5100 KB n=100
18 Correct 4 ms 5100 KB n=100
19 Correct 4 ms 5120 KB n=100
20 Correct 4 ms 5100 KB n=100
21 Correct 4 ms 5100 KB n=100
22 Correct 4 ms 5100 KB n=100
23 Correct 4 ms 5100 KB n=100
24 Correct 4 ms 5100 KB n=100
25 Correct 4 ms 5100 KB n=100
26 Correct 4 ms 5100 KB n=12
27 Correct 4 ms 5100 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB n=5
2 Correct 4 ms 5132 KB n=100
3 Correct 4 ms 5100 KB n=100
4 Correct 4 ms 5120 KB n=100
5 Correct 4 ms 5112 KB n=100
6 Correct 4 ms 5100 KB n=100
7 Correct 4 ms 5100 KB n=100
8 Correct 4 ms 5100 KB n=100
9 Correct 4 ms 5100 KB n=100
10 Correct 4 ms 5100 KB n=100
11 Correct 4 ms 5100 KB n=100
12 Correct 4 ms 5100 KB n=100
13 Correct 4 ms 5100 KB n=100
14 Correct 4 ms 5100 KB n=100
15 Correct 4 ms 5100 KB n=100
16 Correct 4 ms 5100 KB n=100
17 Correct 4 ms 5100 KB n=100
18 Correct 4 ms 5100 KB n=100
19 Correct 4 ms 5120 KB n=100
20 Correct 4 ms 5100 KB n=100
21 Correct 4 ms 5100 KB n=100
22 Correct 4 ms 5100 KB n=100
23 Correct 4 ms 5100 KB n=100
24 Correct 4 ms 5100 KB n=100
25 Correct 4 ms 5100 KB n=100
26 Correct 4 ms 5100 KB n=12
27 Correct 4 ms 5100 KB n=100
28 Correct 5 ms 5228 KB n=500
29 Correct 4 ms 5228 KB n=500
30 Correct 5 ms 5228 KB n=500
31 Correct 5 ms 5228 KB n=500
32 Correct 6 ms 5228 KB n=500
33 Correct 5 ms 5228 KB n=500
34 Correct 5 ms 5228 KB n=500
35 Correct 5 ms 5228 KB n=500
36 Correct 4 ms 5228 KB n=500
37 Correct 4 ms 5228 KB n=500
38 Correct 4 ms 5228 KB n=500
39 Correct 5 ms 5228 KB n=500
40 Correct 4 ms 5228 KB n=500
41 Correct 4 ms 5228 KB n=500
42 Correct 5 ms 5228 KB n=500
43 Correct 5 ms 5228 KB n=500
44 Correct 5 ms 5356 KB n=500
45 Correct 5 ms 5228 KB n=500
46 Correct 4 ms 5228 KB n=500
47 Correct 5 ms 5248 KB n=500
48 Correct 5 ms 5228 KB n=500
49 Correct 5 ms 5228 KB n=500
50 Correct 5 ms 5228 KB n=500
51 Correct 5 ms 5228 KB n=500
52 Correct 5 ms 5228 KB n=500
53 Correct 5 ms 5228 KB n=500
54 Correct 5 ms 5248 KB n=500
55 Correct 4 ms 5100 KB n=278
56 Correct 4 ms 5228 KB n=500
57 Correct 5 ms 5228 KB n=500
58 Correct 4 ms 5228 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB n=5
2 Correct 4 ms 5132 KB n=100
3 Correct 4 ms 5100 KB n=100
4 Correct 4 ms 5120 KB n=100
5 Correct 4 ms 5112 KB n=100
6 Correct 4 ms 5100 KB n=100
7 Correct 4 ms 5100 KB n=100
8 Correct 4 ms 5100 KB n=100
9 Correct 4 ms 5100 KB n=100
10 Correct 4 ms 5100 KB n=100
11 Correct 4 ms 5100 KB n=100
12 Correct 4 ms 5100 KB n=100
13 Correct 4 ms 5100 KB n=100
14 Correct 4 ms 5100 KB n=100
15 Correct 4 ms 5100 KB n=100
16 Correct 4 ms 5100 KB n=100
17 Correct 4 ms 5100 KB n=100
18 Correct 4 ms 5100 KB n=100
19 Correct 4 ms 5120 KB n=100
20 Correct 4 ms 5100 KB n=100
21 Correct 4 ms 5100 KB n=100
22 Correct 4 ms 5100 KB n=100
23 Correct 4 ms 5100 KB n=100
24 Correct 4 ms 5100 KB n=100
25 Correct 4 ms 5100 KB n=100
26 Correct 4 ms 5100 KB n=12
27 Correct 4 ms 5100 KB n=100
28 Correct 5 ms 5228 KB n=500
29 Correct 4 ms 5228 KB n=500
30 Correct 5 ms 5228 KB n=500
31 Correct 5 ms 5228 KB n=500
32 Correct 6 ms 5228 KB n=500
33 Correct 5 ms 5228 KB n=500
34 Correct 5 ms 5228 KB n=500
35 Correct 5 ms 5228 KB n=500
36 Correct 4 ms 5228 KB n=500
37 Correct 4 ms 5228 KB n=500
38 Correct 4 ms 5228 KB n=500
39 Correct 5 ms 5228 KB n=500
40 Correct 4 ms 5228 KB n=500
41 Correct 4 ms 5228 KB n=500
42 Correct 5 ms 5228 KB n=500
43 Correct 5 ms 5228 KB n=500
44 Correct 5 ms 5356 KB n=500
45 Correct 5 ms 5228 KB n=500
46 Correct 4 ms 5228 KB n=500
47 Correct 5 ms 5248 KB n=500
48 Correct 5 ms 5228 KB n=500
49 Correct 5 ms 5228 KB n=500
50 Correct 5 ms 5228 KB n=500
51 Correct 5 ms 5228 KB n=500
52 Correct 5 ms 5228 KB n=500
53 Correct 5 ms 5228 KB n=500
54 Correct 5 ms 5248 KB n=500
55 Correct 4 ms 5100 KB n=278
56 Correct 4 ms 5228 KB n=500
57 Correct 5 ms 5228 KB n=500
58 Correct 4 ms 5228 KB n=500
59 Correct 8 ms 5612 KB n=2000
60 Correct 7 ms 5740 KB n=2000
61 Correct 8 ms 5740 KB n=2000
62 Correct 8 ms 5740 KB n=2000
63 Correct 9 ms 5612 KB n=2000
64 Correct 8 ms 5740 KB n=2000
65 Correct 9 ms 5612 KB n=2000
66 Correct 7 ms 5740 KB n=2000
67 Correct 9 ms 5612 KB n=2000
68 Correct 8 ms 5740 KB n=2000
69 Correct 7 ms 5612 KB n=2000
70 Correct 7 ms 5612 KB n=2000
71 Correct 7 ms 5740 KB n=2000
72 Correct 7 ms 5612 KB n=2000
73 Correct 9 ms 5612 KB n=2000
74 Correct 8 ms 5612 KB n=1844
75 Correct 7 ms 5612 KB n=2000
76 Correct 8 ms 5632 KB n=2000
77 Correct 8 ms 5612 KB n=2000
78 Correct 8 ms 5612 KB n=2000
79 Correct 8 ms 5612 KB n=2000
80 Correct 7 ms 5740 KB n=2000
81 Correct 8 ms 5740 KB n=2000
82 Correct 8 ms 5612 KB n=2000
83 Correct 7 ms 5740 KB n=2000
84 Correct 8 ms 5612 KB n=2000
85 Correct 8 ms 5740 KB n=2000
86 Correct 8 ms 5740 KB n=2000
87 Correct 9 ms 5612 KB n=2000
88 Correct 8 ms 5740 KB n=2000
89 Correct 7 ms 5740 KB n=2000
90 Correct 7 ms 5868 KB n=2000
91 Correct 7 ms 5612 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB n=5
2 Correct 4 ms 5132 KB n=100
3 Correct 4 ms 5100 KB n=100
4 Correct 4 ms 5120 KB n=100
5 Correct 4 ms 5112 KB n=100
6 Correct 4 ms 5100 KB n=100
7 Correct 4 ms 5100 KB n=100
8 Correct 4 ms 5100 KB n=100
9 Correct 4 ms 5100 KB n=100
10 Correct 4 ms 5100 KB n=100
11 Correct 4 ms 5100 KB n=100
12 Correct 4 ms 5100 KB n=100
13 Correct 4 ms 5100 KB n=100
14 Correct 4 ms 5100 KB n=100
15 Correct 4 ms 5100 KB n=100
16 Correct 4 ms 5100 KB n=100
17 Correct 4 ms 5100 KB n=100
18 Correct 4 ms 5100 KB n=100
19 Correct 4 ms 5120 KB n=100
20 Correct 4 ms 5100 KB n=100
21 Correct 4 ms 5100 KB n=100
22 Correct 4 ms 5100 KB n=100
23 Correct 4 ms 5100 KB n=100
24 Correct 4 ms 5100 KB n=100
25 Correct 4 ms 5100 KB n=100
26 Correct 4 ms 5100 KB n=12
27 Correct 4 ms 5100 KB n=100
28 Correct 5 ms 5228 KB n=500
29 Correct 4 ms 5228 KB n=500
30 Correct 5 ms 5228 KB n=500
31 Correct 5 ms 5228 KB n=500
32 Correct 6 ms 5228 KB n=500
33 Correct 5 ms 5228 KB n=500
34 Correct 5 ms 5228 KB n=500
35 Correct 5 ms 5228 KB n=500
36 Correct 4 ms 5228 KB n=500
37 Correct 4 ms 5228 KB n=500
38 Correct 4 ms 5228 KB n=500
39 Correct 5 ms 5228 KB n=500
40 Correct 4 ms 5228 KB n=500
41 Correct 4 ms 5228 KB n=500
42 Correct 5 ms 5228 KB n=500
43 Correct 5 ms 5228 KB n=500
44 Correct 5 ms 5356 KB n=500
45 Correct 5 ms 5228 KB n=500
46 Correct 4 ms 5228 KB n=500
47 Correct 5 ms 5248 KB n=500
48 Correct 5 ms 5228 KB n=500
49 Correct 5 ms 5228 KB n=500
50 Correct 5 ms 5228 KB n=500
51 Correct 5 ms 5228 KB n=500
52 Correct 5 ms 5228 KB n=500
53 Correct 5 ms 5228 KB n=500
54 Correct 5 ms 5248 KB n=500
55 Correct 4 ms 5100 KB n=278
56 Correct 4 ms 5228 KB n=500
57 Correct 5 ms 5228 KB n=500
58 Correct 4 ms 5228 KB n=500
59 Correct 8 ms 5612 KB n=2000
60 Correct 7 ms 5740 KB n=2000
61 Correct 8 ms 5740 KB n=2000
62 Correct 8 ms 5740 KB n=2000
63 Correct 9 ms 5612 KB n=2000
64 Correct 8 ms 5740 KB n=2000
65 Correct 9 ms 5612 KB n=2000
66 Correct 7 ms 5740 KB n=2000
67 Correct 9 ms 5612 KB n=2000
68 Correct 8 ms 5740 KB n=2000
69 Correct 7 ms 5612 KB n=2000
70 Correct 7 ms 5612 KB n=2000
71 Correct 7 ms 5740 KB n=2000
72 Correct 7 ms 5612 KB n=2000
73 Correct 9 ms 5612 KB n=2000
74 Correct 8 ms 5612 KB n=1844
75 Correct 7 ms 5612 KB n=2000
76 Correct 8 ms 5632 KB n=2000
77 Correct 8 ms 5612 KB n=2000
78 Correct 8 ms 5612 KB n=2000
79 Correct 8 ms 5612 KB n=2000
80 Correct 7 ms 5740 KB n=2000
81 Correct 8 ms 5740 KB n=2000
82 Correct 8 ms 5612 KB n=2000
83 Correct 7 ms 5740 KB n=2000
84 Correct 8 ms 5612 KB n=2000
85 Correct 8 ms 5740 KB n=2000
86 Correct 8 ms 5740 KB n=2000
87 Correct 9 ms 5612 KB n=2000
88 Correct 8 ms 5740 KB n=2000
89 Correct 7 ms 5740 KB n=2000
90 Correct 7 ms 5868 KB n=2000
91 Correct 7 ms 5612 KB n=2000
92 Correct 1083 ms 66312 KB n=200000
93 Correct 1265 ms 72556 KB n=200000
94 Correct 967 ms 76908 KB n=200000
95 Correct 1073 ms 65888 KB n=200000
96 Correct 1014 ms 66040 KB n=200000
97 Correct 1313 ms 71448 KB n=200000
98 Correct 1056 ms 66068 KB n=200000
99 Correct 1215 ms 66304 KB n=200000
100 Correct 1052 ms 66148 KB n=200000
101 Correct 892 ms 79084 KB n=200000
102 Correct 552 ms 67180 KB n=200000
103 Correct 557 ms 67052 KB n=200000
104 Correct 548 ms 67180 KB n=200000
105 Correct 564 ms 67564 KB n=200000
106 Correct 545 ms 67436 KB n=200000
107 Correct 567 ms 67564 KB n=200000
108 Correct 1151 ms 66108 KB n=200000
109 Correct 1134 ms 65900 KB n=200000
110 Correct 1125 ms 66088 KB n=200000
111 Correct 962 ms 65488 KB n=200000
112 Correct 919 ms 77472 KB n=200000
113 Correct 1272 ms 71404 KB n=200000
114 Correct 975 ms 65632 KB n=200000
115 Correct 1545 ms 68412 KB n=200000
116 Correct 1045 ms 66164 KB n=200000
117 Correct 906 ms 77804 KB n=200000
118 Correct 1389 ms 70028 KB n=200000
119 Correct 1029 ms 66272 KB n=200000
120 Correct 800 ms 77684 KB n=200000
121 Correct 756 ms 77548 KB n=200000
122 Correct 769 ms 77884 KB n=200000
123 Correct 554 ms 67308 KB n=200000
124 Correct 242 ms 21612 KB n=25264