Submission #314218

# Submission time Handle Problem Language Result Execution time Memory
314218 2020-10-19T06:13:04 Z Trickster Birthday gift (IZhO18_treearray) C++14
100 / 100
2182 ms 82680 KB
#include <bits/stdc++.h>
#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
#include <map>

using namespace std;

#define N 200010
#define ff first
#define ss second
#define ll long long
#define pb push_back
#define mod 1000000007
#define pii pair <int, int>
#define sz(a) int(a.size())
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
ll bigmod(ll a,ll e) {if(e==0)return 1;ll x=bigmod(a*a%mod,e>>1);return e&1?x*a%mod:x;}

set <pii> S[N];
int n, m, q, a, b;
vector <int> E[N];
int L[N], Pr[N][30], v[N];

void dfs(int nd, int pr)
{
    L[nd] = L[pr] + 1;
    Pr[nd][0] = pr;

    for(auto i: E[nd]) if(i != pr) dfs(i, nd);
}

int lca(int a, int b)
{
    if(L[a] < L[b]) swap(a, b);

    for(int i = 20; i >= 0; i--) 
        if(L[a] - (1<<i) >= L[b])
            a = Pr[a][i];

    if(a == b) return a;

    for(int i = 20; i >= 0; i--)
        if(Pr[a][i] != Pr[b][i])
            a = Pr[a][i], b = Pr[b][i];

    return Pr[a][0];
}

int main()
{
    cin >> n >> m >> q;

    for(int i = 1; i < n; i++) {
        cin >> a >> b;

        E[a].pb(b);
        E[b].pb(a);
    }

    dfs(1, 0);

    for(int i = 1; (1<<i) <= n; i++)
        for(int h = 1; h <= n; h++)
            Pr[h][i] = Pr[Pr[h][i-1]][i-1];

    for(int i = 1; i <= m; i++) cin >> v[i];

    for(int i = 1; i < m; i++) S[lca(v[i], v[i+1])].insert({i, 1});

    for(int i = 1; i <= m; i++) S[v[i]].insert({i, 0});

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

        if(tp == 1) {
            int pos, x;
            cin >> pos >> x;

            if(pos != 1) {
                int Lca = lca(v[pos-1], v[pos]);
                S[Lca].erase({pos-1, 1});

                Lca = lca(v[pos-1], x);
                S[Lca].insert({pos-1, 1});
            }
            if(pos != n) {
                int Lca = lca(v[pos+1], v[pos]);
                S[Lca].erase({pos, 1});

                Lca = lca(v[pos+1], x);
                S[Lca].insert({pos, 1});
            }

            S[v[pos]].erase({pos, 0});
            v[pos] = x;
            S[v[pos]].insert({pos, 0});
        } else {
            int l, r, x;
            cin >> l >> r >> x;

            pii in = *S[x].lower_bound({l, 0});

            if(in.ff+in.ss <= r && in.ff >= l) cout << in.ff << " " << in.ff+in.ss << "\n";
            else cout << "-1 -1\n";
        }
    }
}
/*
5 4 4
1 2
3 1
3 4
5 3
4 5 2 3
2 1 3 1
1 3 5
2 3 4 5
2 1 3 1
*/

Compilation message

treearray.cpp:23: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
   23 | #pragma GCC optimization ("O3")
      | 
treearray.cpp:24: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
   24 | #pragma GCC optimization ("unroll-loops")
      |
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14464 KB n=5
2 Correct 10 ms 14464 KB n=100
3 Correct 10 ms 14464 KB n=100
4 Correct 12 ms 14464 KB n=100
5 Correct 10 ms 14464 KB n=100
6 Correct 10 ms 14464 KB n=100
7 Correct 10 ms 14464 KB n=100
8 Correct 10 ms 14464 KB n=100
9 Correct 10 ms 14464 KB n=100
10 Correct 10 ms 14464 KB n=100
11 Correct 10 ms 14464 KB n=100
12 Correct 10 ms 14464 KB n=100
13 Correct 10 ms 14464 KB n=100
14 Correct 10 ms 14464 KB n=100
15 Correct 10 ms 14464 KB n=100
16 Correct 10 ms 14464 KB n=100
17 Correct 10 ms 14464 KB n=100
18 Correct 10 ms 14464 KB n=100
19 Correct 10 ms 14464 KB n=100
20 Correct 10 ms 14464 KB n=100
21 Correct 10 ms 14464 KB n=100
22 Correct 10 ms 14464 KB n=100
23 Correct 10 ms 14464 KB n=100
24 Correct 10 ms 14464 KB n=100
25 Correct 10 ms 14464 KB n=100
26 Correct 10 ms 14464 KB n=12
27 Correct 10 ms 14464 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14464 KB n=5
2 Correct 10 ms 14464 KB n=100
3 Correct 10 ms 14464 KB n=100
4 Correct 12 ms 14464 KB n=100
5 Correct 10 ms 14464 KB n=100
6 Correct 10 ms 14464 KB n=100
7 Correct 10 ms 14464 KB n=100
8 Correct 10 ms 14464 KB n=100
9 Correct 10 ms 14464 KB n=100
10 Correct 10 ms 14464 KB n=100
11 Correct 10 ms 14464 KB n=100
12 Correct 10 ms 14464 KB n=100
13 Correct 10 ms 14464 KB n=100
14 Correct 10 ms 14464 KB n=100
15 Correct 10 ms 14464 KB n=100
16 Correct 10 ms 14464 KB n=100
17 Correct 10 ms 14464 KB n=100
18 Correct 10 ms 14464 KB n=100
19 Correct 10 ms 14464 KB n=100
20 Correct 10 ms 14464 KB n=100
21 Correct 10 ms 14464 KB n=100
22 Correct 10 ms 14464 KB n=100
23 Correct 10 ms 14464 KB n=100
24 Correct 10 ms 14464 KB n=100
25 Correct 10 ms 14464 KB n=100
26 Correct 10 ms 14464 KB n=12
27 Correct 10 ms 14464 KB n=100
28 Correct 12 ms 14592 KB n=500
29 Correct 12 ms 14592 KB n=500
30 Correct 12 ms 14592 KB n=500
31 Correct 12 ms 14592 KB n=500
32 Correct 12 ms 14592 KB n=500
33 Correct 12 ms 14592 KB n=500
34 Correct 12 ms 14592 KB n=500
35 Correct 12 ms 14592 KB n=500
36 Correct 12 ms 14592 KB n=500
37 Correct 12 ms 14592 KB n=500
38 Correct 12 ms 14592 KB n=500
39 Correct 12 ms 14592 KB n=500
40 Correct 13 ms 14592 KB n=500
41 Correct 12 ms 14592 KB n=500
42 Correct 13 ms 14592 KB n=500
43 Correct 12 ms 14592 KB n=500
44 Correct 12 ms 14592 KB n=500
45 Correct 12 ms 14592 KB n=500
46 Correct 12 ms 14592 KB n=500
47 Correct 12 ms 14592 KB n=500
48 Correct 12 ms 14592 KB n=500
49 Correct 12 ms 14592 KB n=500
50 Correct 12 ms 14592 KB n=500
51 Correct 12 ms 14592 KB n=500
52 Correct 12 ms 14592 KB n=500
53 Correct 12 ms 14592 KB n=500
54 Correct 12 ms 14592 KB n=500
55 Correct 11 ms 14464 KB n=278
56 Correct 12 ms 14592 KB n=500
57 Correct 12 ms 14592 KB n=500
58 Correct 13 ms 14592 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14464 KB n=5
2 Correct 10 ms 14464 KB n=100
3 Correct 10 ms 14464 KB n=100
4 Correct 12 ms 14464 KB n=100
5 Correct 10 ms 14464 KB n=100
6 Correct 10 ms 14464 KB n=100
7 Correct 10 ms 14464 KB n=100
8 Correct 10 ms 14464 KB n=100
9 Correct 10 ms 14464 KB n=100
10 Correct 10 ms 14464 KB n=100
11 Correct 10 ms 14464 KB n=100
12 Correct 10 ms 14464 KB n=100
13 Correct 10 ms 14464 KB n=100
14 Correct 10 ms 14464 KB n=100
15 Correct 10 ms 14464 KB n=100
16 Correct 10 ms 14464 KB n=100
17 Correct 10 ms 14464 KB n=100
18 Correct 10 ms 14464 KB n=100
19 Correct 10 ms 14464 KB n=100
20 Correct 10 ms 14464 KB n=100
21 Correct 10 ms 14464 KB n=100
22 Correct 10 ms 14464 KB n=100
23 Correct 10 ms 14464 KB n=100
24 Correct 10 ms 14464 KB n=100
25 Correct 10 ms 14464 KB n=100
26 Correct 10 ms 14464 KB n=12
27 Correct 10 ms 14464 KB n=100
28 Correct 12 ms 14592 KB n=500
29 Correct 12 ms 14592 KB n=500
30 Correct 12 ms 14592 KB n=500
31 Correct 12 ms 14592 KB n=500
32 Correct 12 ms 14592 KB n=500
33 Correct 12 ms 14592 KB n=500
34 Correct 12 ms 14592 KB n=500
35 Correct 12 ms 14592 KB n=500
36 Correct 12 ms 14592 KB n=500
37 Correct 12 ms 14592 KB n=500
38 Correct 12 ms 14592 KB n=500
39 Correct 12 ms 14592 KB n=500
40 Correct 13 ms 14592 KB n=500
41 Correct 12 ms 14592 KB n=500
42 Correct 13 ms 14592 KB n=500
43 Correct 12 ms 14592 KB n=500
44 Correct 12 ms 14592 KB n=500
45 Correct 12 ms 14592 KB n=500
46 Correct 12 ms 14592 KB n=500
47 Correct 12 ms 14592 KB n=500
48 Correct 12 ms 14592 KB n=500
49 Correct 12 ms 14592 KB n=500
50 Correct 12 ms 14592 KB n=500
51 Correct 12 ms 14592 KB n=500
52 Correct 12 ms 14592 KB n=500
53 Correct 12 ms 14592 KB n=500
54 Correct 12 ms 14592 KB n=500
55 Correct 11 ms 14464 KB n=278
56 Correct 12 ms 14592 KB n=500
57 Correct 12 ms 14592 KB n=500
58 Correct 13 ms 14592 KB n=500
59 Correct 20 ms 14976 KB n=2000
60 Correct 21 ms 15096 KB n=2000
61 Correct 21 ms 15104 KB n=2000
62 Correct 21 ms 14976 KB n=2000
63 Correct 20 ms 14976 KB n=2000
64 Correct 20 ms 14976 KB n=2000
65 Correct 20 ms 14976 KB n=2000
66 Correct 21 ms 15104 KB n=2000
67 Correct 21 ms 14976 KB n=2000
68 Correct 20 ms 14976 KB n=2000
69 Correct 21 ms 14976 KB n=2000
70 Correct 21 ms 14976 KB n=2000
71 Correct 22 ms 14976 KB n=2000
72 Correct 22 ms 15052 KB n=2000
73 Correct 21 ms 14976 KB n=2000
74 Correct 18 ms 14976 KB n=1844
75 Correct 21 ms 14976 KB n=2000
76 Correct 20 ms 14976 KB n=2000
77 Correct 20 ms 14976 KB n=2000
78 Correct 20 ms 14976 KB n=2000
79 Correct 20 ms 14976 KB n=2000
80 Correct 21 ms 15104 KB n=2000
81 Correct 20 ms 14976 KB n=2000
82 Correct 20 ms 15104 KB n=2000
83 Correct 21 ms 15104 KB n=2000
84 Correct 19 ms 14976 KB n=2000
85 Correct 20 ms 14976 KB n=2000
86 Correct 20 ms 15048 KB n=2000
87 Correct 20 ms 14976 KB n=2000
88 Correct 19 ms 15104 KB n=2000
89 Correct 20 ms 15104 KB n=2000
90 Correct 20 ms 15104 KB n=2000
91 Correct 21 ms 14976 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14464 KB n=5
2 Correct 10 ms 14464 KB n=100
3 Correct 10 ms 14464 KB n=100
4 Correct 12 ms 14464 KB n=100
5 Correct 10 ms 14464 KB n=100
6 Correct 10 ms 14464 KB n=100
7 Correct 10 ms 14464 KB n=100
8 Correct 10 ms 14464 KB n=100
9 Correct 10 ms 14464 KB n=100
10 Correct 10 ms 14464 KB n=100
11 Correct 10 ms 14464 KB n=100
12 Correct 10 ms 14464 KB n=100
13 Correct 10 ms 14464 KB n=100
14 Correct 10 ms 14464 KB n=100
15 Correct 10 ms 14464 KB n=100
16 Correct 10 ms 14464 KB n=100
17 Correct 10 ms 14464 KB n=100
18 Correct 10 ms 14464 KB n=100
19 Correct 10 ms 14464 KB n=100
20 Correct 10 ms 14464 KB n=100
21 Correct 10 ms 14464 KB n=100
22 Correct 10 ms 14464 KB n=100
23 Correct 10 ms 14464 KB n=100
24 Correct 10 ms 14464 KB n=100
25 Correct 10 ms 14464 KB n=100
26 Correct 10 ms 14464 KB n=12
27 Correct 10 ms 14464 KB n=100
28 Correct 12 ms 14592 KB n=500
29 Correct 12 ms 14592 KB n=500
30 Correct 12 ms 14592 KB n=500
31 Correct 12 ms 14592 KB n=500
32 Correct 12 ms 14592 KB n=500
33 Correct 12 ms 14592 KB n=500
34 Correct 12 ms 14592 KB n=500
35 Correct 12 ms 14592 KB n=500
36 Correct 12 ms 14592 KB n=500
37 Correct 12 ms 14592 KB n=500
38 Correct 12 ms 14592 KB n=500
39 Correct 12 ms 14592 KB n=500
40 Correct 13 ms 14592 KB n=500
41 Correct 12 ms 14592 KB n=500
42 Correct 13 ms 14592 KB n=500
43 Correct 12 ms 14592 KB n=500
44 Correct 12 ms 14592 KB n=500
45 Correct 12 ms 14592 KB n=500
46 Correct 12 ms 14592 KB n=500
47 Correct 12 ms 14592 KB n=500
48 Correct 12 ms 14592 KB n=500
49 Correct 12 ms 14592 KB n=500
50 Correct 12 ms 14592 KB n=500
51 Correct 12 ms 14592 KB n=500
52 Correct 12 ms 14592 KB n=500
53 Correct 12 ms 14592 KB n=500
54 Correct 12 ms 14592 KB n=500
55 Correct 11 ms 14464 KB n=278
56 Correct 12 ms 14592 KB n=500
57 Correct 12 ms 14592 KB n=500
58 Correct 13 ms 14592 KB n=500
59 Correct 20 ms 14976 KB n=2000
60 Correct 21 ms 15096 KB n=2000
61 Correct 21 ms 15104 KB n=2000
62 Correct 21 ms 14976 KB n=2000
63 Correct 20 ms 14976 KB n=2000
64 Correct 20 ms 14976 KB n=2000
65 Correct 20 ms 14976 KB n=2000
66 Correct 21 ms 15104 KB n=2000
67 Correct 21 ms 14976 KB n=2000
68 Correct 20 ms 14976 KB n=2000
69 Correct 21 ms 14976 KB n=2000
70 Correct 21 ms 14976 KB n=2000
71 Correct 22 ms 14976 KB n=2000
72 Correct 22 ms 15052 KB n=2000
73 Correct 21 ms 14976 KB n=2000
74 Correct 18 ms 14976 KB n=1844
75 Correct 21 ms 14976 KB n=2000
76 Correct 20 ms 14976 KB n=2000
77 Correct 20 ms 14976 KB n=2000
78 Correct 20 ms 14976 KB n=2000
79 Correct 20 ms 14976 KB n=2000
80 Correct 21 ms 15104 KB n=2000
81 Correct 20 ms 14976 KB n=2000
82 Correct 20 ms 15104 KB n=2000
83 Correct 21 ms 15104 KB n=2000
84 Correct 19 ms 14976 KB n=2000
85 Correct 20 ms 14976 KB n=2000
86 Correct 20 ms 15048 KB n=2000
87 Correct 20 ms 14976 KB n=2000
88 Correct 19 ms 15104 KB n=2000
89 Correct 20 ms 15104 KB n=2000
90 Correct 20 ms 15104 KB n=2000
91 Correct 21 ms 14976 KB n=2000
92 Correct 1606 ms 73072 KB n=200000
93 Correct 2092 ms 78164 KB n=200000
94 Correct 2023 ms 81340 KB n=200000
95 Correct 1572 ms 72940 KB n=200000
96 Correct 1584 ms 73072 KB n=200000
97 Correct 2104 ms 77088 KB n=200000
98 Correct 1598 ms 72940 KB n=200000
99 Correct 1872 ms 72952 KB n=200000
100 Correct 1600 ms 73192 KB n=200000
101 Correct 2023 ms 82680 KB n=200000
102 Correct 1486 ms 74444 KB n=200000
103 Correct 1490 ms 74168 KB n=200000
104 Correct 1483 ms 74104 KB n=200000
105 Correct 1528 ms 74488 KB n=200000
106 Correct 1516 ms 74488 KB n=200000
107 Correct 1537 ms 74360 KB n=200000
108 Correct 1777 ms 72952 KB n=200000
109 Correct 1770 ms 72952 KB n=200000
110 Correct 1760 ms 73152 KB n=200000
111 Correct 1563 ms 72444 KB n=200000
112 Correct 2001 ms 81912 KB n=200000
113 Correct 2078 ms 77176 KB n=200000
114 Correct 1615 ms 72428 KB n=200000
115 Correct 2182 ms 74956 KB n=200000
116 Correct 1554 ms 73016 KB n=200000
117 Correct 1998 ms 82168 KB n=200000
118 Correct 2109 ms 76024 KB n=200000
119 Correct 1535 ms 73156 KB n=200000
120 Correct 1904 ms 81784 KB n=200000
121 Correct 1903 ms 81528 KB n=200000
122 Correct 1902 ms 82004 KB n=200000
123 Correct 1527 ms 74488 KB n=200000
124 Correct 513 ms 30328 KB n=25264