Submission #889441

# Submission time Handle Problem Language Result Execution time Memory
889441 2023-12-19T17:09:00 Z makrav Birthday gift (IZhO18_treearray) C++14
100 / 100
761 ms 98424 KB
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vei;
typedef vector<vei> vevei;

#define all(a) (a).begin(), (a).end()
#define sz(a) (int) a.size()
#define con cout << "NO\n"
#define coe cout << "YES\n";
#define str string
#define pb push_back
#define ff first
#define sc second
#define ss second
#define pii pair<int, int>
#define mxe max_element
#define mne min_element
#define stf shrink_to_fit
#define f(i, l, r) for (int i = (l); i < (r); i++)
#define double ld

struct LCA {
    int n;
    vector<vector<int>> g, up;
    vector<int> tin, tout;
    int timer = 0, l;

    LCA() = default;
    LCA(int n_, vector<vector<int>>& g_) {
        n = n_;
        g = g_;
        l = (int)log2(n) + 1;
        up.assign(n, vector<int>(l, 0));
        tin.assign(n, 0);
        tout.assign(n, 0);
        dfs(0, 0);
    }

    void dfs(int v, int p) {
        tin[v] = timer++;
        up[v][0] = p;
        f(i, 1, l) {
            up[v][i] = up[up[v][i - 1]][i - 1];
        }

        for (auto& u : g[v]) {
            if (u != p) {
                dfs(u, v);
            }
        }
        tout[v] = timer++;
    }

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

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

        for (int i = l - 1; i >= 0; i--) {
            if (!parent(up[a][i], b)) {
                a = up[a][i];
            }
        }
        return up[a][0];
    }
};

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

    int n, m, q; cin >> n >> m >> q;
    vector<vector<int>> g(n);
    f(i, 0, n - 1) {
        int u, v; cin >> u >> v;
        u--; v--;
        g[u].pb(v);
        g[v].pb(u);

    }
    LCA lc(n, g);

    vector<int> a(m);
    f(i, 0, m) { cin >> a[i]; a[i]--; }
    vector<int> lcas(m - 1);
    vector<set<int>> pos(n), pos2(n);
    f(i, 0, m - 1) {
        lcas[i] = lc.lca(a[i], a[i + 1]);
        pos[lcas[i]].insert(i);
        pos2[a[i]].insert(i);
    }
    pos2[a[m - 1]].insert(m - 1);
    while (q--) {
        int t; cin >> t;
        if (t == 1) {
            int p, v; cin >> p >> v;
            p--;
            v--;
            pos2[a[p]].erase(p);
            pos2[v].insert(p);

            if (p > 0) {
                pos[lcas[p - 1]].erase(p - 1);
                lcas[p - 1] = lc.lca(a[p - 1], v);
                pos[lcas[p - 1]].insert(p - 1);
            }
            if (p < m - 1) {
                pos[lcas[p]].erase(p);
                lcas[p] = lc.lca(v, a[p + 1]);
                pos[lcas[p]].insert(p);
            }

            a[p] = v;
        }
        else {
            int l, r, x; cin >> l >> r >> x;
            l--; r--; x--;
            auto it = pos2[x].lower_bound(l);
            if (it != pos2[x].end() && *it <= r) {
                cout << *it + 1 << ' ' << *it + 1 << '\n';
                continue;
            }
            auto it2 = pos[x].lower_bound(l);
            if (r - l + 1 >= 2 && it2 != pos[x].end() && *it2 <= r - 1) {
                cout << *it2 + 1 << ' ' << *it2 + 2 << '\n';
                continue;
            }
            cout << "-1 -1\n";
        }
    }


    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB n=5
2 Correct 1 ms 348 KB n=100
3 Correct 0 ms 344 KB n=100
4 Correct 1 ms 348 KB n=100
5 Correct 1 ms 600 KB n=100
6 Correct 0 ms 428 KB n=100
7 Correct 0 ms 348 KB n=100
8 Correct 0 ms 348 KB n=100
9 Correct 0 ms 348 KB n=100
10 Correct 0 ms 348 KB n=100
11 Correct 0 ms 348 KB n=100
12 Correct 0 ms 348 KB n=100
13 Correct 0 ms 348 KB n=100
14 Correct 1 ms 348 KB n=100
15 Correct 0 ms 348 KB n=100
16 Correct 0 ms 348 KB n=100
17 Correct 0 ms 348 KB n=100
18 Correct 1 ms 344 KB n=100
19 Correct 0 ms 348 KB n=100
20 Correct 0 ms 348 KB n=100
21 Correct 0 ms 348 KB n=100
22 Correct 0 ms 348 KB n=100
23 Correct 0 ms 344 KB n=100
24 Correct 0 ms 348 KB n=100
25 Correct 0 ms 344 KB n=100
26 Correct 1 ms 348 KB n=12
27 Correct 0 ms 348 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB n=5
2 Correct 1 ms 348 KB n=100
3 Correct 0 ms 344 KB n=100
4 Correct 1 ms 348 KB n=100
5 Correct 1 ms 600 KB n=100
6 Correct 0 ms 428 KB n=100
7 Correct 0 ms 348 KB n=100
8 Correct 0 ms 348 KB n=100
9 Correct 0 ms 348 KB n=100
10 Correct 0 ms 348 KB n=100
11 Correct 0 ms 348 KB n=100
12 Correct 0 ms 348 KB n=100
13 Correct 0 ms 348 KB n=100
14 Correct 1 ms 348 KB n=100
15 Correct 0 ms 348 KB n=100
16 Correct 0 ms 348 KB n=100
17 Correct 0 ms 348 KB n=100
18 Correct 1 ms 344 KB n=100
19 Correct 0 ms 348 KB n=100
20 Correct 0 ms 348 KB n=100
21 Correct 0 ms 348 KB n=100
22 Correct 0 ms 348 KB n=100
23 Correct 0 ms 344 KB n=100
24 Correct 0 ms 348 KB n=100
25 Correct 0 ms 344 KB n=100
26 Correct 1 ms 348 KB n=12
27 Correct 0 ms 348 KB n=100
28 Correct 1 ms 604 KB n=500
29 Correct 1 ms 604 KB n=500
30 Correct 1 ms 604 KB n=500
31 Correct 1 ms 604 KB n=500
32 Correct 1 ms 604 KB n=500
33 Correct 1 ms 604 KB n=500
34 Correct 1 ms 604 KB n=500
35 Correct 1 ms 604 KB n=500
36 Correct 1 ms 604 KB n=500
37 Correct 1 ms 604 KB n=500
38 Correct 1 ms 600 KB n=500
39 Correct 1 ms 600 KB n=500
40 Correct 1 ms 856 KB n=500
41 Correct 1 ms 604 KB n=500
42 Correct 1 ms 600 KB n=500
43 Correct 1 ms 604 KB n=500
44 Correct 1 ms 604 KB n=500
45 Correct 1 ms 604 KB n=500
46 Correct 1 ms 604 KB n=500
47 Correct 1 ms 472 KB n=500
48 Correct 1 ms 604 KB n=500
49 Correct 1 ms 600 KB n=500
50 Correct 1 ms 604 KB n=500
51 Correct 1 ms 604 KB n=500
52 Correct 1 ms 604 KB n=500
53 Correct 1 ms 472 KB n=500
54 Correct 1 ms 604 KB n=500
55 Correct 1 ms 348 KB n=278
56 Correct 1 ms 604 KB n=500
57 Correct 1 ms 600 KB n=500
58 Correct 1 ms 604 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB n=5
2 Correct 1 ms 348 KB n=100
3 Correct 0 ms 344 KB n=100
4 Correct 1 ms 348 KB n=100
5 Correct 1 ms 600 KB n=100
6 Correct 0 ms 428 KB n=100
7 Correct 0 ms 348 KB n=100
8 Correct 0 ms 348 KB n=100
9 Correct 0 ms 348 KB n=100
10 Correct 0 ms 348 KB n=100
11 Correct 0 ms 348 KB n=100
12 Correct 0 ms 348 KB n=100
13 Correct 0 ms 348 KB n=100
14 Correct 1 ms 348 KB n=100
15 Correct 0 ms 348 KB n=100
16 Correct 0 ms 348 KB n=100
17 Correct 0 ms 348 KB n=100
18 Correct 1 ms 344 KB n=100
19 Correct 0 ms 348 KB n=100
20 Correct 0 ms 348 KB n=100
21 Correct 0 ms 348 KB n=100
22 Correct 0 ms 348 KB n=100
23 Correct 0 ms 344 KB n=100
24 Correct 0 ms 348 KB n=100
25 Correct 0 ms 344 KB n=100
26 Correct 1 ms 348 KB n=12
27 Correct 0 ms 348 KB n=100
28 Correct 1 ms 604 KB n=500
29 Correct 1 ms 604 KB n=500
30 Correct 1 ms 604 KB n=500
31 Correct 1 ms 604 KB n=500
32 Correct 1 ms 604 KB n=500
33 Correct 1 ms 604 KB n=500
34 Correct 1 ms 604 KB n=500
35 Correct 1 ms 604 KB n=500
36 Correct 1 ms 604 KB n=500
37 Correct 1 ms 604 KB n=500
38 Correct 1 ms 600 KB n=500
39 Correct 1 ms 600 KB n=500
40 Correct 1 ms 856 KB n=500
41 Correct 1 ms 604 KB n=500
42 Correct 1 ms 600 KB n=500
43 Correct 1 ms 604 KB n=500
44 Correct 1 ms 604 KB n=500
45 Correct 1 ms 604 KB n=500
46 Correct 1 ms 604 KB n=500
47 Correct 1 ms 472 KB n=500
48 Correct 1 ms 604 KB n=500
49 Correct 1 ms 600 KB n=500
50 Correct 1 ms 604 KB n=500
51 Correct 1 ms 604 KB n=500
52 Correct 1 ms 604 KB n=500
53 Correct 1 ms 472 KB n=500
54 Correct 1 ms 604 KB n=500
55 Correct 1 ms 348 KB n=278
56 Correct 1 ms 604 KB n=500
57 Correct 1 ms 600 KB n=500
58 Correct 1 ms 604 KB n=500
59 Correct 3 ms 1388 KB n=2000
60 Correct 2 ms 1116 KB n=2000
61 Correct 2 ms 1116 KB n=2000
62 Correct 3 ms 1112 KB n=2000
63 Correct 3 ms 1116 KB n=2000
64 Correct 3 ms 1168 KB n=2000
65 Correct 3 ms 1112 KB n=2000
66 Correct 2 ms 1112 KB n=2000
67 Correct 3 ms 1112 KB n=2000
68 Correct 3 ms 1116 KB n=2000
69 Correct 2 ms 1112 KB n=2000
70 Correct 3 ms 1372 KB n=2000
71 Correct 2 ms 1116 KB n=2000
72 Correct 2 ms 1116 KB n=2000
73 Correct 2 ms 1112 KB n=2000
74 Correct 2 ms 1112 KB n=1844
75 Correct 2 ms 1116 KB n=2000
76 Correct 3 ms 1116 KB n=2000
77 Correct 3 ms 1236 KB n=2000
78 Correct 3 ms 1116 KB n=2000
79 Correct 3 ms 1116 KB n=2000
80 Correct 4 ms 1128 KB n=2000
81 Correct 2 ms 1112 KB n=2000
82 Correct 3 ms 1116 KB n=2000
83 Correct 2 ms 1116 KB n=2000
84 Correct 3 ms 1116 KB n=2000
85 Correct 3 ms 1128 KB n=2000
86 Correct 3 ms 1116 KB n=2000
87 Correct 3 ms 1116 KB n=2000
88 Correct 2 ms 1368 KB n=2000
89 Correct 3 ms 1368 KB n=2000
90 Correct 2 ms 1112 KB n=2000
91 Correct 2 ms 1116 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB n=5
2 Correct 1 ms 348 KB n=100
3 Correct 0 ms 344 KB n=100
4 Correct 1 ms 348 KB n=100
5 Correct 1 ms 600 KB n=100
6 Correct 0 ms 428 KB n=100
7 Correct 0 ms 348 KB n=100
8 Correct 0 ms 348 KB n=100
9 Correct 0 ms 348 KB n=100
10 Correct 0 ms 348 KB n=100
11 Correct 0 ms 348 KB n=100
12 Correct 0 ms 348 KB n=100
13 Correct 0 ms 348 KB n=100
14 Correct 1 ms 348 KB n=100
15 Correct 0 ms 348 KB n=100
16 Correct 0 ms 348 KB n=100
17 Correct 0 ms 348 KB n=100
18 Correct 1 ms 344 KB n=100
19 Correct 0 ms 348 KB n=100
20 Correct 0 ms 348 KB n=100
21 Correct 0 ms 348 KB n=100
22 Correct 0 ms 348 KB n=100
23 Correct 0 ms 344 KB n=100
24 Correct 0 ms 348 KB n=100
25 Correct 0 ms 344 KB n=100
26 Correct 1 ms 348 KB n=12
27 Correct 0 ms 348 KB n=100
28 Correct 1 ms 604 KB n=500
29 Correct 1 ms 604 KB n=500
30 Correct 1 ms 604 KB n=500
31 Correct 1 ms 604 KB n=500
32 Correct 1 ms 604 KB n=500
33 Correct 1 ms 604 KB n=500
34 Correct 1 ms 604 KB n=500
35 Correct 1 ms 604 KB n=500
36 Correct 1 ms 604 KB n=500
37 Correct 1 ms 604 KB n=500
38 Correct 1 ms 600 KB n=500
39 Correct 1 ms 600 KB n=500
40 Correct 1 ms 856 KB n=500
41 Correct 1 ms 604 KB n=500
42 Correct 1 ms 600 KB n=500
43 Correct 1 ms 604 KB n=500
44 Correct 1 ms 604 KB n=500
45 Correct 1 ms 604 KB n=500
46 Correct 1 ms 604 KB n=500
47 Correct 1 ms 472 KB n=500
48 Correct 1 ms 604 KB n=500
49 Correct 1 ms 600 KB n=500
50 Correct 1 ms 604 KB n=500
51 Correct 1 ms 604 KB n=500
52 Correct 1 ms 604 KB n=500
53 Correct 1 ms 472 KB n=500
54 Correct 1 ms 604 KB n=500
55 Correct 1 ms 348 KB n=278
56 Correct 1 ms 604 KB n=500
57 Correct 1 ms 600 KB n=500
58 Correct 1 ms 604 KB n=500
59 Correct 3 ms 1388 KB n=2000
60 Correct 2 ms 1116 KB n=2000
61 Correct 2 ms 1116 KB n=2000
62 Correct 3 ms 1112 KB n=2000
63 Correct 3 ms 1116 KB n=2000
64 Correct 3 ms 1168 KB n=2000
65 Correct 3 ms 1112 KB n=2000
66 Correct 2 ms 1112 KB n=2000
67 Correct 3 ms 1112 KB n=2000
68 Correct 3 ms 1116 KB n=2000
69 Correct 2 ms 1112 KB n=2000
70 Correct 3 ms 1372 KB n=2000
71 Correct 2 ms 1116 KB n=2000
72 Correct 2 ms 1116 KB n=2000
73 Correct 2 ms 1112 KB n=2000
74 Correct 2 ms 1112 KB n=1844
75 Correct 2 ms 1116 KB n=2000
76 Correct 3 ms 1116 KB n=2000
77 Correct 3 ms 1236 KB n=2000
78 Correct 3 ms 1116 KB n=2000
79 Correct 3 ms 1116 KB n=2000
80 Correct 4 ms 1128 KB n=2000
81 Correct 2 ms 1112 KB n=2000
82 Correct 3 ms 1116 KB n=2000
83 Correct 2 ms 1116 KB n=2000
84 Correct 3 ms 1116 KB n=2000
85 Correct 3 ms 1128 KB n=2000
86 Correct 3 ms 1116 KB n=2000
87 Correct 3 ms 1116 KB n=2000
88 Correct 2 ms 1368 KB n=2000
89 Correct 3 ms 1368 KB n=2000
90 Correct 2 ms 1112 KB n=2000
91 Correct 2 ms 1116 KB n=2000
92 Correct 591 ms 92464 KB n=200000
93 Correct 619 ms 95236 KB n=200000
94 Correct 457 ms 97476 KB n=200000
95 Correct 612 ms 92368 KB n=200000
96 Correct 562 ms 92640 KB n=200000
97 Correct 655 ms 94732 KB n=200000
98 Correct 571 ms 92428 KB n=200000
99 Correct 679 ms 91992 KB n=200000
100 Correct 581 ms 92620 KB n=200000
101 Correct 397 ms 98424 KB n=200000
102 Correct 375 ms 92756 KB n=200000
103 Correct 368 ms 93008 KB n=200000
104 Correct 356 ms 92756 KB n=200000
105 Correct 349 ms 93148 KB n=200000
106 Correct 349 ms 93264 KB n=200000
107 Correct 358 ms 93492 KB n=200000
108 Correct 597 ms 92036 KB n=200000
109 Correct 593 ms 91704 KB n=200000
110 Correct 575 ms 92040 KB n=200000
111 Correct 571 ms 91996 KB n=200000
112 Correct 418 ms 97600 KB n=200000
113 Correct 641 ms 94736 KB n=200000
114 Correct 553 ms 91872 KB n=200000
115 Correct 761 ms 93160 KB n=200000
116 Correct 563 ms 92876 KB n=200000
117 Correct 399 ms 97656 KB n=200000
118 Correct 699 ms 93644 KB n=200000
119 Correct 551 ms 92628 KB n=200000
120 Correct 335 ms 97416 KB n=200000
121 Correct 349 ms 97140 KB n=200000
122 Correct 338 ms 97364 KB n=200000
123 Correct 377 ms 93312 KB n=200000
124 Correct 86 ms 20308 KB n=25264