Submission #675060

# Submission time Handle Problem Language Result Execution time Memory
675060 2022-12-27T01:32:36 Z YENGOYAN Birthday gift (IZhO18_treearray) C++17
100 / 100
1028 ms 83208 KB
// eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee //
// 271828___182845__904523__53602__ //
// 87___47____13______52____66__24_ //
// 97___75____72______47____09___36 // 
// 999595_____74______96____69___67 // 
// 62___77____24______07____66__30_ // 
// 35___35____47______59____45713__ //
// eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee //
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_map>
#include <cmath>
#include <climits>
#include <algorithm>
#include <random>
#include <queue>
#include <deque>
#include <iomanip>
#include <string>
#include <tuple>
#include <bitset>
#include <chrono>
#include <ctime>
#include <fstream>
#include <stack>
#include <cstdio>

using namespace std;
using ll = long long;
const int N = 1e5 + 5;
const ll mod = 1e9 + 7, inf = 1e18;

vector<int> a, tin, tout;
map<pair<int, int>, int> p, s, lcas;
vector<vector<int>> gp, up;
int n, m, q, timer = 0;

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

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

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


pair<int, pair<int, int>> daq(int l, int r, int v) {
    if (l == r) {
        if (tin[a[l]] >= tin[v] && tout[a[l]] <= tout[v]) {
            p[{l, r}] = s[{l, r}] = 1;
            lcas[{l, r}] = a[l];
            return { a[l], {l, l} };
        }
        p[{l, r}] = s[{l, r}] = 0;
        lcas[{l, r}] = -1;
        return { -1, {-1, -1} };
    }
    int  m = (l + r) / 2;
    pair<int, pair<int, int>> L1 = daq(l, m, v), L2 = daq(m + 1, r, v);
    int ll1 = L1.first, ll2 = L2.first;
    if (ll1 == v) return L1;
    if (ll2 == v) return L2;
    int lst = s[{l, m}], fr = p[{m + 1, r}];
    int s1 = m - l + 1, s2 = r - m;
    int l1 = m - lst + 1, r1 = m + fr;
    if (lst == s1) p[{l, r}] = s1 + fr;
    else p[{l, r}] = p[{l, m}];

    if (fr == s2) s[{l, r}] = s2 + lst;
    else s[{l, r}] = s[{m + 1, r}];
    // lcas -> l1 ... r1
    // lcas[{l1, r1}] = lca(lcas[{l1, m}], lcas[{m + 1, r1}]);
    ll1 = lcas[{l1, m}], ll2 = lcas[{m + 1, r1}];
    if (ll1 != -1 && ll2 != -1) lcas[{l1, r1}] = lca(ll1, ll2);
    else if (ll1 != -1) lcas[{l1, r1}] = ll1;
    else if (ll2 != -1) lcas[{l1, r1}] = ll2;
    else lcas[{l1, r1}] = -1;
    return { lcas[{l1, r1}], {l1, r1} };
}

vector<set<pair<int, int>>> vst;


void solve() {
    cin >> n >> m >> q;
    tin = tout = vector<int>(n);
    a = vector<int>(m);
    gp = vector<vector<int>>(n);
    up = vector<vector<int>>(n, vector<int>(20));
    vst = vector<set<pair<int, int>>>(n);
    for (int i = 1; i < n; ++i) {
        int u, v; cin >> u >> v;
        gp[--u].push_back(--v);
        gp[v].push_back(u);
    }
    for (int i = 0; i < m; ++i) cin >> a[i], --a[i];
    dfs(0, 0);
    for (int i = 0; i < m; ++i) {
        vst[a[i]].insert({ i, i });
        if (i < m - 1) vst[lca(a[i], a[i + 1])].insert({ i, i + 1 });
    }
    //for (auto x : vst[0]) cout <<"! " << x.first << " " << x.second << endl;
    while (q--) {
        int type; cin >> type;
        if (type == 2) {
            int l, r, v; cin >> l >> r >> v;
            --l, --r, --v;
            auto it = vst[v].lower_bound({ l, l });
            if (it == vst[v].end()) {
                cout << "-1 -1\n";
                continue;
            }
            pair<int, int> p = *it;
            if (p.second <= r) cout << p.first + 1 << ' ' << p.second + 1 << "\n";
            else cout << "-1 -1\n";
        }
        else {
            int i, val; cin >> i >> val;
            --i, --val;
            int v = a[i];
            vst[v].erase({ i, i });
            if (i) vst[lca(a[i], a[i - 1])].erase({ i - 1, i });
            if (i < m - 1) vst[lca(a[i], a[i + 1])].erase({ i, i + 1 });
            if(i) vst[lca(val, a[i - 1])].insert({ i - 1, i });
            if(i < m - 1) vst[lca(val, a[i + 1])].insert({ i, i + 1 });
            vst[val].insert({ i, i });
            a[i] = val;
        }
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    //int _; cin >> _; while (_--)
    solve();
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB n=5
2 Correct 1 ms 340 KB n=100
3 Correct 1 ms 340 KB n=100
4 Correct 1 ms 340 KB n=100
5 Correct 0 ms 340 KB n=100
6 Correct 1 ms 340 KB n=100
7 Correct 1 ms 340 KB n=100
8 Correct 1 ms 340 KB n=100
9 Correct 0 ms 340 KB n=100
10 Correct 0 ms 340 KB n=100
11 Correct 1 ms 340 KB n=100
12 Correct 1 ms 340 KB n=100
13 Correct 1 ms 340 KB n=100
14 Correct 0 ms 340 KB n=100
15 Correct 1 ms 340 KB n=100
16 Correct 1 ms 340 KB n=100
17 Correct 1 ms 340 KB n=100
18 Correct 1 ms 340 KB n=100
19 Correct 1 ms 340 KB n=100
20 Correct 1 ms 340 KB n=100
21 Correct 0 ms 340 KB n=100
22 Correct 0 ms 340 KB n=100
23 Correct 1 ms 340 KB n=100
24 Correct 1 ms 460 KB n=100
25 Correct 0 ms 340 KB n=100
26 Correct 1 ms 212 KB n=12
27 Correct 1 ms 340 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB n=5
2 Correct 1 ms 340 KB n=100
3 Correct 1 ms 340 KB n=100
4 Correct 1 ms 340 KB n=100
5 Correct 0 ms 340 KB n=100
6 Correct 1 ms 340 KB n=100
7 Correct 1 ms 340 KB n=100
8 Correct 1 ms 340 KB n=100
9 Correct 0 ms 340 KB n=100
10 Correct 0 ms 340 KB n=100
11 Correct 1 ms 340 KB n=100
12 Correct 1 ms 340 KB n=100
13 Correct 1 ms 340 KB n=100
14 Correct 0 ms 340 KB n=100
15 Correct 1 ms 340 KB n=100
16 Correct 1 ms 340 KB n=100
17 Correct 1 ms 340 KB n=100
18 Correct 1 ms 340 KB n=100
19 Correct 1 ms 340 KB n=100
20 Correct 1 ms 340 KB n=100
21 Correct 0 ms 340 KB n=100
22 Correct 0 ms 340 KB n=100
23 Correct 1 ms 340 KB n=100
24 Correct 1 ms 460 KB n=100
25 Correct 0 ms 340 KB n=100
26 Correct 1 ms 212 KB n=12
27 Correct 1 ms 340 KB n=100
28 Correct 1 ms 468 KB n=500
29 Correct 1 ms 468 KB n=500
30 Correct 1 ms 468 KB n=500
31 Correct 1 ms 468 KB n=500
32 Correct 1 ms 468 KB n=500
33 Correct 1 ms 500 KB n=500
34 Correct 1 ms 468 KB n=500
35 Correct 1 ms 468 KB n=500
36 Correct 1 ms 500 KB n=500
37 Correct 1 ms 456 KB n=500
38 Correct 1 ms 468 KB n=500
39 Correct 1 ms 456 KB n=500
40 Correct 1 ms 456 KB n=500
41 Correct 1 ms 468 KB n=500
42 Correct 1 ms 468 KB n=500
43 Correct 1 ms 448 KB n=500
44 Correct 1 ms 468 KB n=500
45 Correct 1 ms 468 KB n=500
46 Correct 1 ms 468 KB n=500
47 Correct 1 ms 460 KB n=500
48 Correct 1 ms 468 KB n=500
49 Correct 1 ms 468 KB n=500
50 Correct 1 ms 468 KB n=500
51 Correct 1 ms 468 KB n=500
52 Correct 1 ms 456 KB n=500
53 Correct 1 ms 460 KB n=500
54 Correct 1 ms 496 KB n=500
55 Correct 1 ms 340 KB n=278
56 Correct 2 ms 468 KB n=500
57 Correct 1 ms 468 KB n=500
58 Correct 1 ms 468 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB n=5
2 Correct 1 ms 340 KB n=100
3 Correct 1 ms 340 KB n=100
4 Correct 1 ms 340 KB n=100
5 Correct 0 ms 340 KB n=100
6 Correct 1 ms 340 KB n=100
7 Correct 1 ms 340 KB n=100
8 Correct 1 ms 340 KB n=100
9 Correct 0 ms 340 KB n=100
10 Correct 0 ms 340 KB n=100
11 Correct 1 ms 340 KB n=100
12 Correct 1 ms 340 KB n=100
13 Correct 1 ms 340 KB n=100
14 Correct 0 ms 340 KB n=100
15 Correct 1 ms 340 KB n=100
16 Correct 1 ms 340 KB n=100
17 Correct 1 ms 340 KB n=100
18 Correct 1 ms 340 KB n=100
19 Correct 1 ms 340 KB n=100
20 Correct 1 ms 340 KB n=100
21 Correct 0 ms 340 KB n=100
22 Correct 0 ms 340 KB n=100
23 Correct 1 ms 340 KB n=100
24 Correct 1 ms 460 KB n=100
25 Correct 0 ms 340 KB n=100
26 Correct 1 ms 212 KB n=12
27 Correct 1 ms 340 KB n=100
28 Correct 1 ms 468 KB n=500
29 Correct 1 ms 468 KB n=500
30 Correct 1 ms 468 KB n=500
31 Correct 1 ms 468 KB n=500
32 Correct 1 ms 468 KB n=500
33 Correct 1 ms 500 KB n=500
34 Correct 1 ms 468 KB n=500
35 Correct 1 ms 468 KB n=500
36 Correct 1 ms 500 KB n=500
37 Correct 1 ms 456 KB n=500
38 Correct 1 ms 468 KB n=500
39 Correct 1 ms 456 KB n=500
40 Correct 1 ms 456 KB n=500
41 Correct 1 ms 468 KB n=500
42 Correct 1 ms 468 KB n=500
43 Correct 1 ms 448 KB n=500
44 Correct 1 ms 468 KB n=500
45 Correct 1 ms 468 KB n=500
46 Correct 1 ms 468 KB n=500
47 Correct 1 ms 460 KB n=500
48 Correct 1 ms 468 KB n=500
49 Correct 1 ms 468 KB n=500
50 Correct 1 ms 468 KB n=500
51 Correct 1 ms 468 KB n=500
52 Correct 1 ms 456 KB n=500
53 Correct 1 ms 460 KB n=500
54 Correct 1 ms 496 KB n=500
55 Correct 1 ms 340 KB n=278
56 Correct 2 ms 468 KB n=500
57 Correct 1 ms 468 KB n=500
58 Correct 1 ms 468 KB n=500
59 Correct 3 ms 980 KB n=2000
60 Correct 3 ms 1072 KB n=2000
61 Correct 3 ms 972 KB n=2000
62 Correct 3 ms 980 KB n=2000
63 Correct 4 ms 980 KB n=2000
64 Correct 3 ms 980 KB n=2000
65 Correct 3 ms 980 KB n=2000
66 Correct 4 ms 980 KB n=2000
67 Correct 4 ms 936 KB n=2000
68 Correct 4 ms 980 KB n=2000
69 Correct 3 ms 972 KB n=2000
70 Correct 3 ms 980 KB n=2000
71 Correct 3 ms 980 KB n=2000
72 Correct 3 ms 968 KB n=2000
73 Correct 3 ms 980 KB n=2000
74 Correct 3 ms 968 KB n=1844
75 Correct 2 ms 980 KB n=2000
76 Correct 3 ms 936 KB n=2000
77 Correct 3 ms 980 KB n=2000
78 Correct 3 ms 980 KB n=2000
79 Correct 5 ms 1036 KB n=2000
80 Correct 3 ms 1108 KB n=2000
81 Correct 3 ms 972 KB n=2000
82 Correct 3 ms 980 KB n=2000
83 Correct 3 ms 980 KB n=2000
84 Correct 3 ms 980 KB n=2000
85 Correct 3 ms 980 KB n=2000
86 Correct 4 ms 980 KB n=2000
87 Correct 4 ms 980 KB n=2000
88 Correct 3 ms 1100 KB n=2000
89 Correct 3 ms 1108 KB n=2000
90 Correct 3 ms 1108 KB n=2000
91 Correct 3 ms 980 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB n=5
2 Correct 1 ms 340 KB n=100
3 Correct 1 ms 340 KB n=100
4 Correct 1 ms 340 KB n=100
5 Correct 0 ms 340 KB n=100
6 Correct 1 ms 340 KB n=100
7 Correct 1 ms 340 KB n=100
8 Correct 1 ms 340 KB n=100
9 Correct 0 ms 340 KB n=100
10 Correct 0 ms 340 KB n=100
11 Correct 1 ms 340 KB n=100
12 Correct 1 ms 340 KB n=100
13 Correct 1 ms 340 KB n=100
14 Correct 0 ms 340 KB n=100
15 Correct 1 ms 340 KB n=100
16 Correct 1 ms 340 KB n=100
17 Correct 1 ms 340 KB n=100
18 Correct 1 ms 340 KB n=100
19 Correct 1 ms 340 KB n=100
20 Correct 1 ms 340 KB n=100
21 Correct 0 ms 340 KB n=100
22 Correct 0 ms 340 KB n=100
23 Correct 1 ms 340 KB n=100
24 Correct 1 ms 460 KB n=100
25 Correct 0 ms 340 KB n=100
26 Correct 1 ms 212 KB n=12
27 Correct 1 ms 340 KB n=100
28 Correct 1 ms 468 KB n=500
29 Correct 1 ms 468 KB n=500
30 Correct 1 ms 468 KB n=500
31 Correct 1 ms 468 KB n=500
32 Correct 1 ms 468 KB n=500
33 Correct 1 ms 500 KB n=500
34 Correct 1 ms 468 KB n=500
35 Correct 1 ms 468 KB n=500
36 Correct 1 ms 500 KB n=500
37 Correct 1 ms 456 KB n=500
38 Correct 1 ms 468 KB n=500
39 Correct 1 ms 456 KB n=500
40 Correct 1 ms 456 KB n=500
41 Correct 1 ms 468 KB n=500
42 Correct 1 ms 468 KB n=500
43 Correct 1 ms 448 KB n=500
44 Correct 1 ms 468 KB n=500
45 Correct 1 ms 468 KB n=500
46 Correct 1 ms 468 KB n=500
47 Correct 1 ms 460 KB n=500
48 Correct 1 ms 468 KB n=500
49 Correct 1 ms 468 KB n=500
50 Correct 1 ms 468 KB n=500
51 Correct 1 ms 468 KB n=500
52 Correct 1 ms 456 KB n=500
53 Correct 1 ms 460 KB n=500
54 Correct 1 ms 496 KB n=500
55 Correct 1 ms 340 KB n=278
56 Correct 2 ms 468 KB n=500
57 Correct 1 ms 468 KB n=500
58 Correct 1 ms 468 KB n=500
59 Correct 3 ms 980 KB n=2000
60 Correct 3 ms 1072 KB n=2000
61 Correct 3 ms 972 KB n=2000
62 Correct 3 ms 980 KB n=2000
63 Correct 4 ms 980 KB n=2000
64 Correct 3 ms 980 KB n=2000
65 Correct 3 ms 980 KB n=2000
66 Correct 4 ms 980 KB n=2000
67 Correct 4 ms 936 KB n=2000
68 Correct 4 ms 980 KB n=2000
69 Correct 3 ms 972 KB n=2000
70 Correct 3 ms 980 KB n=2000
71 Correct 3 ms 980 KB n=2000
72 Correct 3 ms 968 KB n=2000
73 Correct 3 ms 980 KB n=2000
74 Correct 3 ms 968 KB n=1844
75 Correct 2 ms 980 KB n=2000
76 Correct 3 ms 936 KB n=2000
77 Correct 3 ms 980 KB n=2000
78 Correct 3 ms 980 KB n=2000
79 Correct 5 ms 1036 KB n=2000
80 Correct 3 ms 1108 KB n=2000
81 Correct 3 ms 972 KB n=2000
82 Correct 3 ms 980 KB n=2000
83 Correct 3 ms 980 KB n=2000
84 Correct 3 ms 980 KB n=2000
85 Correct 3 ms 980 KB n=2000
86 Correct 4 ms 980 KB n=2000
87 Correct 4 ms 980 KB n=2000
88 Correct 3 ms 1100 KB n=2000
89 Correct 3 ms 1108 KB n=2000
90 Correct 3 ms 1108 KB n=2000
91 Correct 3 ms 980 KB n=2000
92 Correct 725 ms 73724 KB n=200000
93 Correct 952 ms 78540 KB n=200000
94 Correct 579 ms 82032 KB n=200000
95 Correct 687 ms 73468 KB n=200000
96 Correct 705 ms 73568 KB n=200000
97 Correct 835 ms 77864 KB n=200000
98 Correct 702 ms 73600 KB n=200000
99 Correct 851 ms 73724 KB n=200000
100 Correct 733 ms 73540 KB n=200000
101 Correct 458 ms 83208 KB n=200000
102 Correct 422 ms 74700 KB n=200000
103 Correct 416 ms 74696 KB n=200000
104 Correct 441 ms 74896 KB n=200000
105 Correct 449 ms 75316 KB n=200000
106 Correct 454 ms 75108 KB n=200000
107 Correct 427 ms 75112 KB n=200000
108 Correct 725 ms 73676 KB n=200000
109 Correct 727 ms 73660 KB n=200000
110 Correct 713 ms 73684 KB n=200000
111 Correct 705 ms 73096 KB n=200000
112 Correct 514 ms 82332 KB n=200000
113 Correct 801 ms 77816 KB n=200000
114 Correct 691 ms 73036 KB n=200000
115 Correct 1028 ms 75660 KB n=200000
116 Correct 667 ms 73664 KB n=200000
117 Correct 471 ms 82576 KB n=200000
118 Correct 893 ms 76656 KB n=200000
119 Correct 637 ms 73752 KB n=200000
120 Correct 410 ms 82176 KB n=200000
121 Correct 408 ms 82292 KB n=200000
122 Correct 403 ms 82668 KB n=200000
123 Correct 435 ms 74940 KB n=200000
124 Correct 135 ms 18000 KB n=25264