Submission #1097837

# Submission time Handle Problem Language Result Execution time Memory
1097837 2024-10-08T10:05:12 Z TrinhKhanhDung Birthday gift (IZhO18_treearray) C++14
56 / 100
660 ms 85584 KB
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(1e9 + 7)

using namespace std;

template<class T1, class T2>
    bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}

template<class T1, class T2>
    bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}

template<class T1, class T2>
    void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}

template<class T1, class T2>
    void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}

template<class T>
    void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}

const int MAX = 2e5 + 10, LOG = 18;

int N, M, Q;
vector<int> adj[MAX];
int h[MAX], d[MAX][LOG], a[MAX];

void dfs(int u, int p){
    for(int v: adj[u]) if(v != p){
        h[v] = h[u] + 1;
        d[v][0] = u;
        for(int i=1; i<LOG; i++){
            d[v][i] = d[d[v][i - 1]][i - 1];
        }
        dfs(v, u);
    }
}

int lca(int u, int v){
    if(h[u] < h[v]) swap(u, v);
    int delta = h[u] - h[v];
    for(int i=0; i<LOG; i++) if(BIT(delta, i)){
        u = d[u][i];
    }
    if(u == v) return u;
    for(int i=LOG-1; i>=0; i--) if(d[u][i] != d[v][i]){
        u = d[u][i];
        v = d[v][i];
    }
    return d[u][0];
}

set<int> st[2][MAX];

void solve(){
    cin >> N >> M >> Q;
    for(int i=1; i<N; i++){
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    for(int i=1; i<=M; i++){
        cin >> a[i];
    }
    dfs(1, 0);
    for(int i=2; i<=M; i++){
        st[1][lca(a[i - 1], a[i])].insert(i);
    }
    for(int i=1; i<=M; i++){
        st[0][a[i]].insert(i);
    }
    while(Q--){
        int t; cin >> t;

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

            st[0][a[i]].erase(i);
            if(i > 1) st[1][lca(a[i - 1], a[i])].erase(i);
            if(i < N) st[1][lca(a[i], a[i + 1])].erase(i + 1);

            a[i] = u;
            st[0][a[i]].insert(i);
            if(i > 1) st[1][lca(a[i - 1], a[i])].insert(i);
            if(i < N) st[1][lca(a[i], a[i + 1])].insert(i + 1);
        }

        if(t == 2){
            int l, r, u;
            cin >> l >> r >> u;
            auto it = st[0][u].lower_bound(l);
            if(it != st[0][u].end() && *it <= r){
                cout << *it << ' ' << *it << '\n';
                continue;
            }

            it = st[1][u].upper_bound(l);
            if(it != st[1][u].end() && *it <= r){
                cout << *it - 1 << ' ' << *it << '\n';
                continue;
            }

            cout << -1 << ' ' << -1 << '\n';
        }
    }
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
//    freopen("deggraph.inp", "r", stdin);
//    freopen("deggraph.out", "w", stdout);

    int t = 1;
//    cin >> t;
    while(t--){
        solve();
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23896 KB n=5
2 Correct 11 ms 23900 KB n=100
3 Correct 9 ms 23900 KB n=100
4 Correct 9 ms 23900 KB n=100
5 Correct 12 ms 23900 KB n=100
6 Correct 10 ms 23900 KB n=100
7 Correct 12 ms 23900 KB n=100
8 Correct 11 ms 23900 KB n=100
9 Correct 11 ms 23900 KB n=100
10 Correct 11 ms 23896 KB n=100
11 Correct 10 ms 23900 KB n=100
12 Correct 11 ms 23900 KB n=100
13 Correct 11 ms 23952 KB n=100
14 Correct 10 ms 23900 KB n=100
15 Correct 10 ms 23912 KB n=100
16 Correct 12 ms 23948 KB n=100
17 Correct 10 ms 23900 KB n=100
18 Correct 9 ms 23760 KB n=100
19 Correct 11 ms 23900 KB n=100
20 Correct 9 ms 23912 KB n=100
21 Correct 11 ms 23900 KB n=100
22 Correct 11 ms 23900 KB n=100
23 Correct 12 ms 23900 KB n=100
24 Correct 10 ms 23896 KB n=100
25 Correct 12 ms 23896 KB n=100
26 Correct 10 ms 23900 KB n=12
27 Correct 11 ms 23848 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23896 KB n=5
2 Correct 11 ms 23900 KB n=100
3 Correct 9 ms 23900 KB n=100
4 Correct 9 ms 23900 KB n=100
5 Correct 12 ms 23900 KB n=100
6 Correct 10 ms 23900 KB n=100
7 Correct 12 ms 23900 KB n=100
8 Correct 11 ms 23900 KB n=100
9 Correct 11 ms 23900 KB n=100
10 Correct 11 ms 23896 KB n=100
11 Correct 10 ms 23900 KB n=100
12 Correct 11 ms 23900 KB n=100
13 Correct 11 ms 23952 KB n=100
14 Correct 10 ms 23900 KB n=100
15 Correct 10 ms 23912 KB n=100
16 Correct 12 ms 23948 KB n=100
17 Correct 10 ms 23900 KB n=100
18 Correct 9 ms 23760 KB n=100
19 Correct 11 ms 23900 KB n=100
20 Correct 9 ms 23912 KB n=100
21 Correct 11 ms 23900 KB n=100
22 Correct 11 ms 23900 KB n=100
23 Correct 12 ms 23900 KB n=100
24 Correct 10 ms 23896 KB n=100
25 Correct 12 ms 23896 KB n=100
26 Correct 10 ms 23900 KB n=12
27 Correct 11 ms 23848 KB n=100
28 Correct 10 ms 23900 KB n=500
29 Correct 11 ms 23900 KB n=500
30 Correct 12 ms 23964 KB n=500
31 Correct 10 ms 23900 KB n=500
32 Correct 10 ms 23900 KB n=500
33 Correct 12 ms 23896 KB n=500
34 Correct 11 ms 23900 KB n=500
35 Correct 11 ms 23900 KB n=500
36 Correct 12 ms 23896 KB n=500
37 Correct 11 ms 23896 KB n=500
38 Correct 10 ms 23900 KB n=500
39 Correct 10 ms 24076 KB n=500
40 Correct 10 ms 23900 KB n=500
41 Correct 11 ms 23900 KB n=500
42 Correct 12 ms 23900 KB n=500
43 Correct 11 ms 23900 KB n=500
44 Correct 11 ms 23900 KB n=500
45 Correct 11 ms 23896 KB n=500
46 Correct 11 ms 23896 KB n=500
47 Correct 11 ms 23900 KB n=500
48 Correct 12 ms 23900 KB n=500
49 Correct 12 ms 23900 KB n=500
50 Correct 11 ms 23900 KB n=500
51 Correct 10 ms 23900 KB n=500
52 Correct 11 ms 23900 KB n=500
53 Correct 10 ms 23896 KB n=500
54 Correct 9 ms 24088 KB n=500
55 Correct 10 ms 23900 KB n=278
56 Correct 10 ms 23900 KB n=500
57 Correct 10 ms 23856 KB n=500
58 Correct 11 ms 23900 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23896 KB n=5
2 Correct 11 ms 23900 KB n=100
3 Correct 9 ms 23900 KB n=100
4 Correct 9 ms 23900 KB n=100
5 Correct 12 ms 23900 KB n=100
6 Correct 10 ms 23900 KB n=100
7 Correct 12 ms 23900 KB n=100
8 Correct 11 ms 23900 KB n=100
9 Correct 11 ms 23900 KB n=100
10 Correct 11 ms 23896 KB n=100
11 Correct 10 ms 23900 KB n=100
12 Correct 11 ms 23900 KB n=100
13 Correct 11 ms 23952 KB n=100
14 Correct 10 ms 23900 KB n=100
15 Correct 10 ms 23912 KB n=100
16 Correct 12 ms 23948 KB n=100
17 Correct 10 ms 23900 KB n=100
18 Correct 9 ms 23760 KB n=100
19 Correct 11 ms 23900 KB n=100
20 Correct 9 ms 23912 KB n=100
21 Correct 11 ms 23900 KB n=100
22 Correct 11 ms 23900 KB n=100
23 Correct 12 ms 23900 KB n=100
24 Correct 10 ms 23896 KB n=100
25 Correct 12 ms 23896 KB n=100
26 Correct 10 ms 23900 KB n=12
27 Correct 11 ms 23848 KB n=100
28 Correct 10 ms 23900 KB n=500
29 Correct 11 ms 23900 KB n=500
30 Correct 12 ms 23964 KB n=500
31 Correct 10 ms 23900 KB n=500
32 Correct 10 ms 23900 KB n=500
33 Correct 12 ms 23896 KB n=500
34 Correct 11 ms 23900 KB n=500
35 Correct 11 ms 23900 KB n=500
36 Correct 12 ms 23896 KB n=500
37 Correct 11 ms 23896 KB n=500
38 Correct 10 ms 23900 KB n=500
39 Correct 10 ms 24076 KB n=500
40 Correct 10 ms 23900 KB n=500
41 Correct 11 ms 23900 KB n=500
42 Correct 12 ms 23900 KB n=500
43 Correct 11 ms 23900 KB n=500
44 Correct 11 ms 23900 KB n=500
45 Correct 11 ms 23896 KB n=500
46 Correct 11 ms 23896 KB n=500
47 Correct 11 ms 23900 KB n=500
48 Correct 12 ms 23900 KB n=500
49 Correct 12 ms 23900 KB n=500
50 Correct 11 ms 23900 KB n=500
51 Correct 10 ms 23900 KB n=500
52 Correct 11 ms 23900 KB n=500
53 Correct 10 ms 23896 KB n=500
54 Correct 9 ms 24088 KB n=500
55 Correct 10 ms 23900 KB n=278
56 Correct 10 ms 23900 KB n=500
57 Correct 10 ms 23856 KB n=500
58 Correct 11 ms 23900 KB n=500
59 Correct 11 ms 24412 KB n=2000
60 Correct 11 ms 24412 KB n=2000
61 Correct 11 ms 24408 KB n=2000
62 Correct 15 ms 24408 KB n=2000
63 Correct 15 ms 24412 KB n=2000
64 Correct 12 ms 24412 KB n=2000
65 Correct 11 ms 24412 KB n=2000
66 Correct 12 ms 24408 KB n=2000
67 Correct 12 ms 24412 KB n=2000
68 Correct 12 ms 24408 KB n=2000
69 Correct 11 ms 24408 KB n=2000
70 Correct 11 ms 24412 KB n=2000
71 Correct 12 ms 24412 KB n=2000
72 Correct 12 ms 24408 KB n=2000
73 Correct 13 ms 24212 KB n=2000
74 Correct 12 ms 24152 KB n=1844
75 Correct 13 ms 24412 KB n=2000
76 Correct 13 ms 24412 KB n=2000
77 Correct 12 ms 24412 KB n=2000
78 Correct 12 ms 24412 KB n=2000
79 Correct 12 ms 24408 KB n=2000
80 Correct 14 ms 24412 KB n=2000
81 Correct 13 ms 24412 KB n=2000
82 Correct 12 ms 24412 KB n=2000
83 Correct 13 ms 24412 KB n=2000
84 Correct 14 ms 24412 KB n=2000
85 Correct 14 ms 24408 KB n=2000
86 Correct 12 ms 24408 KB n=2000
87 Correct 13 ms 24412 KB n=2000
88 Correct 14 ms 24412 KB n=2000
89 Correct 13 ms 24412 KB n=2000
90 Correct 13 ms 24412 KB n=2000
91 Correct 13 ms 24300 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23896 KB n=5
2 Correct 11 ms 23900 KB n=100
3 Correct 9 ms 23900 KB n=100
4 Correct 9 ms 23900 KB n=100
5 Correct 12 ms 23900 KB n=100
6 Correct 10 ms 23900 KB n=100
7 Correct 12 ms 23900 KB n=100
8 Correct 11 ms 23900 KB n=100
9 Correct 11 ms 23900 KB n=100
10 Correct 11 ms 23896 KB n=100
11 Correct 10 ms 23900 KB n=100
12 Correct 11 ms 23900 KB n=100
13 Correct 11 ms 23952 KB n=100
14 Correct 10 ms 23900 KB n=100
15 Correct 10 ms 23912 KB n=100
16 Correct 12 ms 23948 KB n=100
17 Correct 10 ms 23900 KB n=100
18 Correct 9 ms 23760 KB n=100
19 Correct 11 ms 23900 KB n=100
20 Correct 9 ms 23912 KB n=100
21 Correct 11 ms 23900 KB n=100
22 Correct 11 ms 23900 KB n=100
23 Correct 12 ms 23900 KB n=100
24 Correct 10 ms 23896 KB n=100
25 Correct 12 ms 23896 KB n=100
26 Correct 10 ms 23900 KB n=12
27 Correct 11 ms 23848 KB n=100
28 Correct 10 ms 23900 KB n=500
29 Correct 11 ms 23900 KB n=500
30 Correct 12 ms 23964 KB n=500
31 Correct 10 ms 23900 KB n=500
32 Correct 10 ms 23900 KB n=500
33 Correct 12 ms 23896 KB n=500
34 Correct 11 ms 23900 KB n=500
35 Correct 11 ms 23900 KB n=500
36 Correct 12 ms 23896 KB n=500
37 Correct 11 ms 23896 KB n=500
38 Correct 10 ms 23900 KB n=500
39 Correct 10 ms 24076 KB n=500
40 Correct 10 ms 23900 KB n=500
41 Correct 11 ms 23900 KB n=500
42 Correct 12 ms 23900 KB n=500
43 Correct 11 ms 23900 KB n=500
44 Correct 11 ms 23900 KB n=500
45 Correct 11 ms 23896 KB n=500
46 Correct 11 ms 23896 KB n=500
47 Correct 11 ms 23900 KB n=500
48 Correct 12 ms 23900 KB n=500
49 Correct 12 ms 23900 KB n=500
50 Correct 11 ms 23900 KB n=500
51 Correct 10 ms 23900 KB n=500
52 Correct 11 ms 23900 KB n=500
53 Correct 10 ms 23896 KB n=500
54 Correct 9 ms 24088 KB n=500
55 Correct 10 ms 23900 KB n=278
56 Correct 10 ms 23900 KB n=500
57 Correct 10 ms 23856 KB n=500
58 Correct 11 ms 23900 KB n=500
59 Correct 11 ms 24412 KB n=2000
60 Correct 11 ms 24412 KB n=2000
61 Correct 11 ms 24408 KB n=2000
62 Correct 15 ms 24408 KB n=2000
63 Correct 15 ms 24412 KB n=2000
64 Correct 12 ms 24412 KB n=2000
65 Correct 11 ms 24412 KB n=2000
66 Correct 12 ms 24408 KB n=2000
67 Correct 12 ms 24412 KB n=2000
68 Correct 12 ms 24408 KB n=2000
69 Correct 11 ms 24408 KB n=2000
70 Correct 11 ms 24412 KB n=2000
71 Correct 12 ms 24412 KB n=2000
72 Correct 12 ms 24408 KB n=2000
73 Correct 13 ms 24212 KB n=2000
74 Correct 12 ms 24152 KB n=1844
75 Correct 13 ms 24412 KB n=2000
76 Correct 13 ms 24412 KB n=2000
77 Correct 12 ms 24412 KB n=2000
78 Correct 12 ms 24412 KB n=2000
79 Correct 12 ms 24408 KB n=2000
80 Correct 14 ms 24412 KB n=2000
81 Correct 13 ms 24412 KB n=2000
82 Correct 12 ms 24412 KB n=2000
83 Correct 13 ms 24412 KB n=2000
84 Correct 14 ms 24412 KB n=2000
85 Correct 14 ms 24408 KB n=2000
86 Correct 12 ms 24408 KB n=2000
87 Correct 13 ms 24412 KB n=2000
88 Correct 14 ms 24412 KB n=2000
89 Correct 13 ms 24412 KB n=2000
90 Correct 13 ms 24412 KB n=2000
91 Correct 13 ms 24300 KB n=2000
92 Correct 441 ms 73036 KB n=200000
93 Correct 526 ms 79444 KB n=200000
94 Correct 540 ms 83796 KB n=200000
95 Correct 424 ms 72640 KB n=200000
96 Correct 393 ms 72896 KB n=200000
97 Correct 588 ms 78160 KB n=200000
98 Correct 436 ms 72900 KB n=200000
99 Correct 506 ms 73000 KB n=200000
100 Correct 452 ms 72904 KB n=200000
101 Correct 564 ms 85584 KB n=200000
102 Correct 293 ms 73988 KB n=200000
103 Correct 283 ms 73996 KB n=200000
104 Correct 251 ms 74068 KB n=200000
105 Correct 299 ms 74304 KB n=200000
106 Correct 279 ms 74300 KB n=200000
107 Correct 257 ms 74324 KB n=200000
108 Correct 540 ms 72832 KB n=200000
109 Correct 535 ms 72968 KB n=200000
110 Correct 512 ms 73012 KB n=200000
111 Correct 479 ms 72236 KB n=200000
112 Correct 660 ms 84288 KB n=200000
113 Correct 641 ms 78160 KB n=200000
114 Correct 438 ms 72388 KB n=200000
115 Correct 609 ms 75608 KB n=200000
116 Correct 475 ms 72896 KB n=200000
117 Correct 657 ms 84816 KB n=200000
118 Correct 622 ms 76648 KB n=200000
119 Correct 452 ms 72924 KB n=200000
120 Correct 577 ms 84500 KB n=200000
121 Correct 570 ms 84664 KB n=200000
122 Correct 518 ms 84780 KB n=200000
123 Correct 247 ms 74324 KB n=200000
124 Incorrect 111 ms 38996 KB Wrong range.
125 Halted 0 ms 0 KB -