Submission #446689

# Submission time Handle Problem Language Result Execution time Memory
446689 2021-07-23T06:40:12 Z dxz05 Birthday gift (IZhO18_treearray) C++14
100 / 100
1325 ms 87864 KB
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

using namespace std;

void debug_out() { cerr << endl; }

template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << "[" << H << "]";
    debug_out(T...);
}

#ifdef dddxxz
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define SZ(s) ((int)s.size())
#define all(x) (x).begin(), (x).end()
#define revall(x) (x).rbegin(), (x).rend()

clock_t startTime;

double getCurrentTime() {
    return (double) (clock() - startTime) / CLOCKS_PER_SEC;
}

#define MP make_pair

typedef long long ll;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const double eps = 0.00001;
const int MOD = 1e9 + 7;
const int INF = 1000000101;
const long long LLINF = 1223372000000000555;
const int N = 2e5 + 3e2;
const int M = 222;

vector<int> g[N];

int tin[N], tout[N], up[N][20], timer = 0;

void dfs(int v, int p){
    tin[v] = ++timer;
    up[v][0] = p;

    for (int i = 1; i < 20; i++){
        up[v][i] = up[up[v][i - 1]][i - 1];
    }

    for (int u : g[v]){
        if (u != p) dfs(u, v);
    }

    tout[v] = ++timer;
}

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

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

    for (int i = 19; i >= 0; i--){
        if (!upper(up[a][i], b)) a = up[a][i];
    }

    return up[a][0];
}

int a[N];

set<int> positions[N];

inline void add(int i){
    int x = lca(a[i], a[i + 1]);
    positions[x].insert(i);
}

inline void del(int i){
    int x = lca(a[i], a[i + 1]);
    positions[x].erase(i);
}

set<int> pos[N];

void solve(int TC) {
    int n, m, q;
    cin >> n >> m >> q;

    for (int i = 1; i < n; i++){
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }

    dfs(1, 1);

    for (int i = 1; i <= m; i++){
        cin >> a[i];
        pos[a[i]].insert(i);
    }

    for (int i = 1; i < m; i++) add(i);

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

        if (t == 1){
            int i, v;
            cin >> i >> v;

            if (i > 1) del(i - 1);
            if (i < m) del(i);

            pos[a[i]].erase(i);
            pos[v].insert(i);

            a[i] = v;

            if (i > 1) add(i - 1);
            if (i < m) add(i);
        } else {
            int l, r, v;
            cin >> l >> r >> v;

            auto it = positions[v].lower_bound(l);
            auto it2 = pos[v].lower_bound(l);
            if (it != positions[v].end() && *it < r){
                int x = *it;
                cout << x << ' ' << x + 1 << endl;
            } else if (it2 != pos[v].end() && *it2 <= r){
                int x = *it2;
                cout << x << ' ' << x << endl;
            } else cout << -1 << ' ' << -1 << endl;

        }
    }

}

int main() {
    startTime = clock();
    cin.tie(nullptr); cout.tie(nullptr);
    ios_base::sync_with_stdio(false);

    bool llololcal = false;
#ifdef dddxxz
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    llololcal = true;
#endif

    int TC = 1;
    //cin >> TC;

    for (int test = 1; test <= TC; test++) {
        //debug(test);
        //cout << "Case #" << test << ": ";
        solve(test);
    }

    if (llololcal) cerr << endl << "Time: " << int(getCurrentTime() * 1000) << " ms" << endl;

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 19 ms 23756 KB n=5
2 Correct 16 ms 23864 KB n=100
3 Correct 16 ms 23756 KB n=100
4 Correct 18 ms 23884 KB n=100
5 Correct 16 ms 23756 KB n=100
6 Correct 17 ms 23756 KB n=100
7 Correct 16 ms 23876 KB n=100
8 Correct 19 ms 23884 KB n=100
9 Correct 17 ms 23884 KB n=100
10 Correct 17 ms 23756 KB n=100
11 Correct 19 ms 23756 KB n=100
12 Correct 18 ms 23884 KB n=100
13 Correct 16 ms 23884 KB n=100
14 Correct 16 ms 23756 KB n=100
15 Correct 19 ms 23840 KB n=100
16 Correct 17 ms 23776 KB n=100
17 Correct 16 ms 23868 KB n=100
18 Correct 17 ms 23768 KB n=100
19 Correct 18 ms 23756 KB n=100
20 Correct 16 ms 23812 KB n=100
21 Correct 16 ms 23848 KB n=100
22 Correct 16 ms 23836 KB n=100
23 Correct 17 ms 23884 KB n=100
24 Correct 16 ms 23884 KB n=100
25 Correct 16 ms 23856 KB n=100
26 Correct 17 ms 23800 KB n=12
27 Correct 16 ms 23844 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 19 ms 23756 KB n=5
2 Correct 16 ms 23864 KB n=100
3 Correct 16 ms 23756 KB n=100
4 Correct 18 ms 23884 KB n=100
5 Correct 16 ms 23756 KB n=100
6 Correct 17 ms 23756 KB n=100
7 Correct 16 ms 23876 KB n=100
8 Correct 19 ms 23884 KB n=100
9 Correct 17 ms 23884 KB n=100
10 Correct 17 ms 23756 KB n=100
11 Correct 19 ms 23756 KB n=100
12 Correct 18 ms 23884 KB n=100
13 Correct 16 ms 23884 KB n=100
14 Correct 16 ms 23756 KB n=100
15 Correct 19 ms 23840 KB n=100
16 Correct 17 ms 23776 KB n=100
17 Correct 16 ms 23868 KB n=100
18 Correct 17 ms 23768 KB n=100
19 Correct 18 ms 23756 KB n=100
20 Correct 16 ms 23812 KB n=100
21 Correct 16 ms 23848 KB n=100
22 Correct 16 ms 23836 KB n=100
23 Correct 17 ms 23884 KB n=100
24 Correct 16 ms 23884 KB n=100
25 Correct 16 ms 23856 KB n=100
26 Correct 17 ms 23800 KB n=12
27 Correct 16 ms 23844 KB n=100
28 Correct 17 ms 24012 KB n=500
29 Correct 17 ms 23992 KB n=500
30 Correct 17 ms 23884 KB n=500
31 Correct 17 ms 23992 KB n=500
32 Correct 17 ms 23884 KB n=500
33 Correct 17 ms 23956 KB n=500
34 Correct 18 ms 23884 KB n=500
35 Correct 17 ms 23988 KB n=500
36 Correct 17 ms 23916 KB n=500
37 Correct 18 ms 23980 KB n=500
38 Correct 17 ms 23884 KB n=500
39 Correct 19 ms 23884 KB n=500
40 Correct 18 ms 23856 KB n=500
41 Correct 18 ms 23976 KB n=500
42 Correct 17 ms 24012 KB n=500
43 Correct 18 ms 23884 KB n=500
44 Correct 17 ms 23908 KB n=500
45 Correct 17 ms 23884 KB n=500
46 Correct 17 ms 24012 KB n=500
47 Correct 17 ms 23992 KB n=500
48 Correct 17 ms 23884 KB n=500
49 Correct 17 ms 23980 KB n=500
50 Correct 18 ms 23884 KB n=500
51 Correct 17 ms 23988 KB n=500
52 Correct 17 ms 23896 KB n=500
53 Correct 17 ms 23884 KB n=500
54 Correct 17 ms 23980 KB n=500
55 Correct 17 ms 23884 KB n=278
56 Correct 16 ms 23920 KB n=500
57 Correct 17 ms 24012 KB n=500
58 Correct 17 ms 23884 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 19 ms 23756 KB n=5
2 Correct 16 ms 23864 KB n=100
3 Correct 16 ms 23756 KB n=100
4 Correct 18 ms 23884 KB n=100
5 Correct 16 ms 23756 KB n=100
6 Correct 17 ms 23756 KB n=100
7 Correct 16 ms 23876 KB n=100
8 Correct 19 ms 23884 KB n=100
9 Correct 17 ms 23884 KB n=100
10 Correct 17 ms 23756 KB n=100
11 Correct 19 ms 23756 KB n=100
12 Correct 18 ms 23884 KB n=100
13 Correct 16 ms 23884 KB n=100
14 Correct 16 ms 23756 KB n=100
15 Correct 19 ms 23840 KB n=100
16 Correct 17 ms 23776 KB n=100
17 Correct 16 ms 23868 KB n=100
18 Correct 17 ms 23768 KB n=100
19 Correct 18 ms 23756 KB n=100
20 Correct 16 ms 23812 KB n=100
21 Correct 16 ms 23848 KB n=100
22 Correct 16 ms 23836 KB n=100
23 Correct 17 ms 23884 KB n=100
24 Correct 16 ms 23884 KB n=100
25 Correct 16 ms 23856 KB n=100
26 Correct 17 ms 23800 KB n=12
27 Correct 16 ms 23844 KB n=100
28 Correct 17 ms 24012 KB n=500
29 Correct 17 ms 23992 KB n=500
30 Correct 17 ms 23884 KB n=500
31 Correct 17 ms 23992 KB n=500
32 Correct 17 ms 23884 KB n=500
33 Correct 17 ms 23956 KB n=500
34 Correct 18 ms 23884 KB n=500
35 Correct 17 ms 23988 KB n=500
36 Correct 17 ms 23916 KB n=500
37 Correct 18 ms 23980 KB n=500
38 Correct 17 ms 23884 KB n=500
39 Correct 19 ms 23884 KB n=500
40 Correct 18 ms 23856 KB n=500
41 Correct 18 ms 23976 KB n=500
42 Correct 17 ms 24012 KB n=500
43 Correct 18 ms 23884 KB n=500
44 Correct 17 ms 23908 KB n=500
45 Correct 17 ms 23884 KB n=500
46 Correct 17 ms 24012 KB n=500
47 Correct 17 ms 23992 KB n=500
48 Correct 17 ms 23884 KB n=500
49 Correct 17 ms 23980 KB n=500
50 Correct 18 ms 23884 KB n=500
51 Correct 17 ms 23988 KB n=500
52 Correct 17 ms 23896 KB n=500
53 Correct 17 ms 23884 KB n=500
54 Correct 17 ms 23980 KB n=500
55 Correct 17 ms 23884 KB n=278
56 Correct 16 ms 23920 KB n=500
57 Correct 17 ms 24012 KB n=500
58 Correct 17 ms 23884 KB n=500
59 Correct 22 ms 24268 KB n=2000
60 Correct 21 ms 24460 KB n=2000
61 Correct 21 ms 24336 KB n=2000
62 Correct 21 ms 24292 KB n=2000
63 Correct 21 ms 24244 KB n=2000
64 Correct 20 ms 24396 KB n=2000
65 Correct 25 ms 24268 KB n=2000
66 Correct 20 ms 24356 KB n=2000
67 Correct 22 ms 24268 KB n=2000
68 Correct 21 ms 24396 KB n=2000
69 Correct 22 ms 24344 KB n=2000
70 Correct 21 ms 24248 KB n=2000
71 Correct 22 ms 24268 KB n=2000
72 Correct 22 ms 24308 KB n=2000
73 Correct 22 ms 24244 KB n=2000
74 Correct 21 ms 24336 KB n=1844
75 Correct 22 ms 24248 KB n=2000
76 Correct 21 ms 24252 KB n=2000
77 Correct 21 ms 24300 KB n=2000
78 Correct 23 ms 24264 KB n=2000
79 Correct 21 ms 24244 KB n=2000
80 Correct 21 ms 24396 KB n=2000
81 Correct 21 ms 24380 KB n=2000
82 Correct 22 ms 24268 KB n=2000
83 Correct 22 ms 24384 KB n=2000
84 Correct 21 ms 24332 KB n=2000
85 Correct 21 ms 24384 KB n=2000
86 Correct 21 ms 24396 KB n=2000
87 Correct 21 ms 24268 KB n=2000
88 Correct 20 ms 24452 KB n=2000
89 Correct 20 ms 24396 KB n=2000
90 Correct 21 ms 24396 KB n=2000
91 Correct 22 ms 24268 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 19 ms 23756 KB n=5
2 Correct 16 ms 23864 KB n=100
3 Correct 16 ms 23756 KB n=100
4 Correct 18 ms 23884 KB n=100
5 Correct 16 ms 23756 KB n=100
6 Correct 17 ms 23756 KB n=100
7 Correct 16 ms 23876 KB n=100
8 Correct 19 ms 23884 KB n=100
9 Correct 17 ms 23884 KB n=100
10 Correct 17 ms 23756 KB n=100
11 Correct 19 ms 23756 KB n=100
12 Correct 18 ms 23884 KB n=100
13 Correct 16 ms 23884 KB n=100
14 Correct 16 ms 23756 KB n=100
15 Correct 19 ms 23840 KB n=100
16 Correct 17 ms 23776 KB n=100
17 Correct 16 ms 23868 KB n=100
18 Correct 17 ms 23768 KB n=100
19 Correct 18 ms 23756 KB n=100
20 Correct 16 ms 23812 KB n=100
21 Correct 16 ms 23848 KB n=100
22 Correct 16 ms 23836 KB n=100
23 Correct 17 ms 23884 KB n=100
24 Correct 16 ms 23884 KB n=100
25 Correct 16 ms 23856 KB n=100
26 Correct 17 ms 23800 KB n=12
27 Correct 16 ms 23844 KB n=100
28 Correct 17 ms 24012 KB n=500
29 Correct 17 ms 23992 KB n=500
30 Correct 17 ms 23884 KB n=500
31 Correct 17 ms 23992 KB n=500
32 Correct 17 ms 23884 KB n=500
33 Correct 17 ms 23956 KB n=500
34 Correct 18 ms 23884 KB n=500
35 Correct 17 ms 23988 KB n=500
36 Correct 17 ms 23916 KB n=500
37 Correct 18 ms 23980 KB n=500
38 Correct 17 ms 23884 KB n=500
39 Correct 19 ms 23884 KB n=500
40 Correct 18 ms 23856 KB n=500
41 Correct 18 ms 23976 KB n=500
42 Correct 17 ms 24012 KB n=500
43 Correct 18 ms 23884 KB n=500
44 Correct 17 ms 23908 KB n=500
45 Correct 17 ms 23884 KB n=500
46 Correct 17 ms 24012 KB n=500
47 Correct 17 ms 23992 KB n=500
48 Correct 17 ms 23884 KB n=500
49 Correct 17 ms 23980 KB n=500
50 Correct 18 ms 23884 KB n=500
51 Correct 17 ms 23988 KB n=500
52 Correct 17 ms 23896 KB n=500
53 Correct 17 ms 23884 KB n=500
54 Correct 17 ms 23980 KB n=500
55 Correct 17 ms 23884 KB n=278
56 Correct 16 ms 23920 KB n=500
57 Correct 17 ms 24012 KB n=500
58 Correct 17 ms 23884 KB n=500
59 Correct 22 ms 24268 KB n=2000
60 Correct 21 ms 24460 KB n=2000
61 Correct 21 ms 24336 KB n=2000
62 Correct 21 ms 24292 KB n=2000
63 Correct 21 ms 24244 KB n=2000
64 Correct 20 ms 24396 KB n=2000
65 Correct 25 ms 24268 KB n=2000
66 Correct 20 ms 24356 KB n=2000
67 Correct 22 ms 24268 KB n=2000
68 Correct 21 ms 24396 KB n=2000
69 Correct 22 ms 24344 KB n=2000
70 Correct 21 ms 24248 KB n=2000
71 Correct 22 ms 24268 KB n=2000
72 Correct 22 ms 24308 KB n=2000
73 Correct 22 ms 24244 KB n=2000
74 Correct 21 ms 24336 KB n=1844
75 Correct 22 ms 24248 KB n=2000
76 Correct 21 ms 24252 KB n=2000
77 Correct 21 ms 24300 KB n=2000
78 Correct 23 ms 24264 KB n=2000
79 Correct 21 ms 24244 KB n=2000
80 Correct 21 ms 24396 KB n=2000
81 Correct 21 ms 24380 KB n=2000
82 Correct 22 ms 24268 KB n=2000
83 Correct 22 ms 24384 KB n=2000
84 Correct 21 ms 24332 KB n=2000
85 Correct 21 ms 24384 KB n=2000
86 Correct 21 ms 24396 KB n=2000
87 Correct 21 ms 24268 KB n=2000
88 Correct 20 ms 24452 KB n=2000
89 Correct 20 ms 24396 KB n=2000
90 Correct 21 ms 24396 KB n=2000
91 Correct 22 ms 24268 KB n=2000
92 Correct 1040 ms 75336 KB n=200000
93 Correct 1107 ms 81788 KB n=200000
94 Correct 866 ms 86252 KB n=200000
95 Correct 1008 ms 75336 KB n=200000
96 Correct 1026 ms 75132 KB n=200000
97 Correct 1215 ms 80604 KB n=200000
98 Correct 1020 ms 75092 KB n=200000
99 Correct 1181 ms 75332 KB n=200000
100 Correct 1020 ms 75212 KB n=200000
101 Correct 784 ms 87864 KB n=200000
102 Correct 780 ms 76292 KB n=200000
103 Correct 864 ms 76368 KB n=200000
104 Correct 782 ms 76388 KB n=200000
105 Correct 787 ms 76720 KB n=200000
106 Correct 787 ms 76740 KB n=200000
107 Correct 785 ms 76868 KB n=200000
108 Correct 1029 ms 75332 KB n=200000
109 Correct 1016 ms 75328 KB n=200000
110 Correct 1030 ms 75400 KB n=200000
111 Correct 1074 ms 74720 KB n=200000
112 Correct 827 ms 86660 KB n=200000
113 Correct 1138 ms 80648 KB n=200000
114 Correct 1060 ms 74968 KB n=200000
115 Correct 1325 ms 77708 KB n=200000
116 Correct 1004 ms 75464 KB n=200000
117 Correct 790 ms 87200 KB n=200000
118 Correct 1206 ms 79044 KB n=200000
119 Correct 958 ms 75348 KB n=200000
120 Correct 744 ms 86908 KB n=200000
121 Correct 723 ms 86920 KB n=200000
122 Correct 736 ms 87228 KB n=200000
123 Correct 793 ms 76624 KB n=200000
124 Correct 240 ms 39184 KB n=25264