답안 #921159

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
921159 2024-02-03T11:17:58 Z phong Birthday gift (IZhO18_treearray) C++17
100 / 100
911 ms 73888 KB
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>

#define ll long long
const int nmax = 2e5 + 100;
const ll oo = 1e18, base = 311;
const int lg = 19, M =2e2 + 5;
const ll mod = 1e9 + 7;
#define pii pair<ll, int>
#define fi first
#define se second
#define debug(a, n) for(int i = 1; i <= n; ++i) cout << a[i] << ' ';
using namespace std;

int n, m, q, a[nmax], h[nmax], up[nmax][lg + 1];
set<int> mt[nmax][2];
vector<int> adj[nmax];
void dfs(int u, int p){
    for(auto v : adj[u]){
        if(v == p) continue;
        h[v] = h[u] + 1;
        up[v][0] = u;
        for(int j = 1; j <= lg; ++j) up[v][j] = up[up[v][j - 1]][j - 1];
        dfs(v, u);
    }
}

int lca(int u, int v){
    if(h[u] != h[v]){
        if(h[u] < h[v]) swap(u, v);
        int k = h[u] - h[v];
        for(int j = 0; j <= __lg(k); ++j){
            if(k >> j & 1){
                u = up[u][j];
            }
        }
    }
    if(u == v) return u;
    int k = h[u];
    for(int j = __lg(k); j >= 0; --j){
        if(up[u][j] != up[v][j]){
            u = up[u][j];
            v = up[v][j];
        }
    }
    return up[u][0];
}
void update(int i, int val){
    int x = a[i];
    mt[x][0].erase(i);
    mt[val][0].insert(i);
    if(i > 1){
        x = lca(a[i], a[i - 1]);
        mt[x][1].erase(i);
        x = lca(val, a[i - 1]);
        mt[x][1].insert(i);
    }
    if(i < m){
        x = lca(a[i + 1], a[i]);
        mt[x][1].erase(i + 1);
        x = lca(val, a[i + 1]);
        mt[x][1].insert(i + 1);
    }
    a[i] = val;
}
pii get(int l, int r, int v){
    if(mt[v][0].size()){
        auto x = mt[v][0].lower_bound(l);
        auto y = *x;
        if(x != mt[v][0].end() && y <= r) return {y, y};
    }
    if(mt[v][1].size()){
        auto x = mt[v][1].upper_bound(l);
        auto y = *x;
        if(x != mt[v][1].end() && y <= r) return {y - 1, y};
    }
//    cout << y << ' ';

    return {-1, -1};

}
main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
//    freopen("code.inp", "r", stdin);
//    freopen("code.out", "w", stdout);
    cin >> n >> m >> q;
    for(int i = 1, u, v; i < n; ++i){
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    dfs(1, -1);
    for(int i = 1; i <= m; ++i){
        cin >> a[i];
        mt[a[i]][0].insert(i);
        if(i > 1) mt[lca(a[i], a[i - 1])][1].insert(i);
    }
//    cout << *mt[1][1].lower_bound(1);
    int t, u, x, l, r;
    while(q--){
        cin >> t;
        if(t == 1){
            cin >> u >> x;
            update(u, x);
        }
        else{
            cin >> l >> r >> x;
            pii tmp = get(l, r, x);
            cout << tmp.fi << ' ' << tmp.se <<  "\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:84:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   84 | main(){
      | ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 25332 KB n=5
2 Correct 7 ms 25180 KB n=100
3 Correct 7 ms 25180 KB n=100
4 Correct 7 ms 25180 KB n=100
5 Correct 7 ms 25180 KB n=100
6 Correct 7 ms 25180 KB n=100
7 Correct 7 ms 25180 KB n=100
8 Correct 7 ms 25180 KB n=100
9 Correct 7 ms 25180 KB n=100
10 Correct 7 ms 25180 KB n=100
11 Correct 7 ms 25432 KB n=100
12 Correct 7 ms 25180 KB n=100
13 Correct 7 ms 25180 KB n=100
14 Correct 7 ms 25180 KB n=100
15 Correct 7 ms 25180 KB n=100
16 Correct 7 ms 25180 KB n=100
17 Correct 7 ms 25180 KB n=100
18 Correct 7 ms 25180 KB n=100
19 Correct 8 ms 25436 KB n=100
20 Correct 8 ms 25180 KB n=100
21 Correct 8 ms 25180 KB n=100
22 Correct 7 ms 25180 KB n=100
23 Correct 7 ms 25180 KB n=100
24 Correct 7 ms 25276 KB n=100
25 Correct 7 ms 25180 KB n=100
26 Correct 7 ms 25176 KB n=12
27 Correct 7 ms 25176 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 25332 KB n=5
2 Correct 7 ms 25180 KB n=100
3 Correct 7 ms 25180 KB n=100
4 Correct 7 ms 25180 KB n=100
5 Correct 7 ms 25180 KB n=100
6 Correct 7 ms 25180 KB n=100
7 Correct 7 ms 25180 KB n=100
8 Correct 7 ms 25180 KB n=100
9 Correct 7 ms 25180 KB n=100
10 Correct 7 ms 25180 KB n=100
11 Correct 7 ms 25432 KB n=100
12 Correct 7 ms 25180 KB n=100
13 Correct 7 ms 25180 KB n=100
14 Correct 7 ms 25180 KB n=100
15 Correct 7 ms 25180 KB n=100
16 Correct 7 ms 25180 KB n=100
17 Correct 7 ms 25180 KB n=100
18 Correct 7 ms 25180 KB n=100
19 Correct 8 ms 25436 KB n=100
20 Correct 8 ms 25180 KB n=100
21 Correct 8 ms 25180 KB n=100
22 Correct 7 ms 25180 KB n=100
23 Correct 7 ms 25180 KB n=100
24 Correct 7 ms 25276 KB n=100
25 Correct 7 ms 25180 KB n=100
26 Correct 7 ms 25176 KB n=12
27 Correct 7 ms 25176 KB n=100
28 Correct 7 ms 25180 KB n=500
29 Correct 7 ms 25180 KB n=500
30 Correct 8 ms 25176 KB n=500
31 Correct 7 ms 25180 KB n=500
32 Correct 7 ms 25176 KB n=500
33 Correct 7 ms 25432 KB n=500
34 Correct 7 ms 25180 KB n=500
35 Correct 9 ms 25332 KB n=500
36 Correct 7 ms 25180 KB n=500
37 Correct 7 ms 25180 KB n=500
38 Correct 8 ms 25180 KB n=500
39 Correct 9 ms 25284 KB n=500
40 Correct 7 ms 25180 KB n=500
41 Correct 8 ms 25176 KB n=500
42 Correct 7 ms 25180 KB n=500
43 Correct 9 ms 25180 KB n=500
44 Correct 8 ms 25180 KB n=500
45 Correct 7 ms 25180 KB n=500
46 Correct 7 ms 25300 KB n=500
47 Correct 8 ms 25180 KB n=500
48 Correct 8 ms 25180 KB n=500
49 Correct 7 ms 25180 KB n=500
50 Correct 7 ms 25176 KB n=500
51 Correct 7 ms 25180 KB n=500
52 Correct 8 ms 25180 KB n=500
53 Correct 8 ms 25180 KB n=500
54 Correct 8 ms 25180 KB n=500
55 Correct 7 ms 25180 KB n=278
56 Correct 7 ms 25348 KB n=500
57 Correct 8 ms 25180 KB n=500
58 Correct 7 ms 25432 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 25332 KB n=5
2 Correct 7 ms 25180 KB n=100
3 Correct 7 ms 25180 KB n=100
4 Correct 7 ms 25180 KB n=100
5 Correct 7 ms 25180 KB n=100
6 Correct 7 ms 25180 KB n=100
7 Correct 7 ms 25180 KB n=100
8 Correct 7 ms 25180 KB n=100
9 Correct 7 ms 25180 KB n=100
10 Correct 7 ms 25180 KB n=100
11 Correct 7 ms 25432 KB n=100
12 Correct 7 ms 25180 KB n=100
13 Correct 7 ms 25180 KB n=100
14 Correct 7 ms 25180 KB n=100
15 Correct 7 ms 25180 KB n=100
16 Correct 7 ms 25180 KB n=100
17 Correct 7 ms 25180 KB n=100
18 Correct 7 ms 25180 KB n=100
19 Correct 8 ms 25436 KB n=100
20 Correct 8 ms 25180 KB n=100
21 Correct 8 ms 25180 KB n=100
22 Correct 7 ms 25180 KB n=100
23 Correct 7 ms 25180 KB n=100
24 Correct 7 ms 25276 KB n=100
25 Correct 7 ms 25180 KB n=100
26 Correct 7 ms 25176 KB n=12
27 Correct 7 ms 25176 KB n=100
28 Correct 7 ms 25180 KB n=500
29 Correct 7 ms 25180 KB n=500
30 Correct 8 ms 25176 KB n=500
31 Correct 7 ms 25180 KB n=500
32 Correct 7 ms 25176 KB n=500
33 Correct 7 ms 25432 KB n=500
34 Correct 7 ms 25180 KB n=500
35 Correct 9 ms 25332 KB n=500
36 Correct 7 ms 25180 KB n=500
37 Correct 7 ms 25180 KB n=500
38 Correct 8 ms 25180 KB n=500
39 Correct 9 ms 25284 KB n=500
40 Correct 7 ms 25180 KB n=500
41 Correct 8 ms 25176 KB n=500
42 Correct 7 ms 25180 KB n=500
43 Correct 9 ms 25180 KB n=500
44 Correct 8 ms 25180 KB n=500
45 Correct 7 ms 25180 KB n=500
46 Correct 7 ms 25300 KB n=500
47 Correct 8 ms 25180 KB n=500
48 Correct 8 ms 25180 KB n=500
49 Correct 7 ms 25180 KB n=500
50 Correct 7 ms 25176 KB n=500
51 Correct 7 ms 25180 KB n=500
52 Correct 8 ms 25180 KB n=500
53 Correct 8 ms 25180 KB n=500
54 Correct 8 ms 25180 KB n=500
55 Correct 7 ms 25180 KB n=278
56 Correct 7 ms 25348 KB n=500
57 Correct 8 ms 25180 KB n=500
58 Correct 7 ms 25432 KB n=500
59 Correct 9 ms 25432 KB n=2000
60 Correct 10 ms 25688 KB n=2000
61 Correct 9 ms 25436 KB n=2000
62 Correct 10 ms 25432 KB n=2000
63 Correct 9 ms 25436 KB n=2000
64 Correct 9 ms 25436 KB n=2000
65 Correct 10 ms 25436 KB n=2000
66 Correct 9 ms 25436 KB n=2000
67 Correct 10 ms 25436 KB n=2000
68 Correct 10 ms 25828 KB n=2000
69 Correct 9 ms 25636 KB n=2000
70 Correct 9 ms 25436 KB n=2000
71 Correct 9 ms 25432 KB n=2000
72 Correct 8 ms 25436 KB n=2000
73 Correct 9 ms 25436 KB n=2000
74 Correct 9 ms 25432 KB n=1844
75 Correct 8 ms 25436 KB n=2000
76 Correct 9 ms 25436 KB n=2000
77 Correct 9 ms 25436 KB n=2000
78 Correct 10 ms 25432 KB n=2000
79 Correct 9 ms 25432 KB n=2000
80 Correct 9 ms 25436 KB n=2000
81 Correct 9 ms 25436 KB n=2000
82 Correct 11 ms 25432 KB n=2000
83 Correct 9 ms 25436 KB n=2000
84 Correct 9 ms 25436 KB n=2000
85 Correct 9 ms 25436 KB n=2000
86 Correct 9 ms 25436 KB n=2000
87 Correct 9 ms 25436 KB n=2000
88 Correct 8 ms 25436 KB n=2000
89 Correct 9 ms 25436 KB n=2000
90 Correct 10 ms 25432 KB n=2000
91 Correct 10 ms 25436 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 25332 KB n=5
2 Correct 7 ms 25180 KB n=100
3 Correct 7 ms 25180 KB n=100
4 Correct 7 ms 25180 KB n=100
5 Correct 7 ms 25180 KB n=100
6 Correct 7 ms 25180 KB n=100
7 Correct 7 ms 25180 KB n=100
8 Correct 7 ms 25180 KB n=100
9 Correct 7 ms 25180 KB n=100
10 Correct 7 ms 25180 KB n=100
11 Correct 7 ms 25432 KB n=100
12 Correct 7 ms 25180 KB n=100
13 Correct 7 ms 25180 KB n=100
14 Correct 7 ms 25180 KB n=100
15 Correct 7 ms 25180 KB n=100
16 Correct 7 ms 25180 KB n=100
17 Correct 7 ms 25180 KB n=100
18 Correct 7 ms 25180 KB n=100
19 Correct 8 ms 25436 KB n=100
20 Correct 8 ms 25180 KB n=100
21 Correct 8 ms 25180 KB n=100
22 Correct 7 ms 25180 KB n=100
23 Correct 7 ms 25180 KB n=100
24 Correct 7 ms 25276 KB n=100
25 Correct 7 ms 25180 KB n=100
26 Correct 7 ms 25176 KB n=12
27 Correct 7 ms 25176 KB n=100
28 Correct 7 ms 25180 KB n=500
29 Correct 7 ms 25180 KB n=500
30 Correct 8 ms 25176 KB n=500
31 Correct 7 ms 25180 KB n=500
32 Correct 7 ms 25176 KB n=500
33 Correct 7 ms 25432 KB n=500
34 Correct 7 ms 25180 KB n=500
35 Correct 9 ms 25332 KB n=500
36 Correct 7 ms 25180 KB n=500
37 Correct 7 ms 25180 KB n=500
38 Correct 8 ms 25180 KB n=500
39 Correct 9 ms 25284 KB n=500
40 Correct 7 ms 25180 KB n=500
41 Correct 8 ms 25176 KB n=500
42 Correct 7 ms 25180 KB n=500
43 Correct 9 ms 25180 KB n=500
44 Correct 8 ms 25180 KB n=500
45 Correct 7 ms 25180 KB n=500
46 Correct 7 ms 25300 KB n=500
47 Correct 8 ms 25180 KB n=500
48 Correct 8 ms 25180 KB n=500
49 Correct 7 ms 25180 KB n=500
50 Correct 7 ms 25176 KB n=500
51 Correct 7 ms 25180 KB n=500
52 Correct 8 ms 25180 KB n=500
53 Correct 8 ms 25180 KB n=500
54 Correct 8 ms 25180 KB n=500
55 Correct 7 ms 25180 KB n=278
56 Correct 7 ms 25348 KB n=500
57 Correct 8 ms 25180 KB n=500
58 Correct 7 ms 25432 KB n=500
59 Correct 9 ms 25432 KB n=2000
60 Correct 10 ms 25688 KB n=2000
61 Correct 9 ms 25436 KB n=2000
62 Correct 10 ms 25432 KB n=2000
63 Correct 9 ms 25436 KB n=2000
64 Correct 9 ms 25436 KB n=2000
65 Correct 10 ms 25436 KB n=2000
66 Correct 9 ms 25436 KB n=2000
67 Correct 10 ms 25436 KB n=2000
68 Correct 10 ms 25828 KB n=2000
69 Correct 9 ms 25636 KB n=2000
70 Correct 9 ms 25436 KB n=2000
71 Correct 9 ms 25432 KB n=2000
72 Correct 8 ms 25436 KB n=2000
73 Correct 9 ms 25436 KB n=2000
74 Correct 9 ms 25432 KB n=1844
75 Correct 8 ms 25436 KB n=2000
76 Correct 9 ms 25436 KB n=2000
77 Correct 9 ms 25436 KB n=2000
78 Correct 10 ms 25432 KB n=2000
79 Correct 9 ms 25432 KB n=2000
80 Correct 9 ms 25436 KB n=2000
81 Correct 9 ms 25436 KB n=2000
82 Correct 11 ms 25432 KB n=2000
83 Correct 9 ms 25436 KB n=2000
84 Correct 9 ms 25436 KB n=2000
85 Correct 9 ms 25436 KB n=2000
86 Correct 9 ms 25436 KB n=2000
87 Correct 9 ms 25436 KB n=2000
88 Correct 8 ms 25436 KB n=2000
89 Correct 9 ms 25436 KB n=2000
90 Correct 10 ms 25432 KB n=2000
91 Correct 10 ms 25436 KB n=2000
92 Correct 600 ms 68132 KB n=200000
93 Correct 762 ms 70512 KB n=200000
94 Correct 780 ms 72820 KB n=200000
95 Correct 538 ms 68072 KB n=200000
96 Correct 507 ms 67944 KB n=200000
97 Correct 816 ms 69824 KB n=200000
98 Correct 571 ms 68296 KB n=200000
99 Correct 749 ms 67372 KB n=200000
100 Correct 511 ms 68044 KB n=200000
101 Correct 729 ms 73736 KB n=200000
102 Correct 319 ms 68856 KB n=200000
103 Correct 325 ms 68948 KB n=200000
104 Correct 317 ms 68948 KB n=200000
105 Correct 347 ms 68640 KB n=200000
106 Correct 328 ms 68800 KB n=200000
107 Correct 320 ms 68776 KB n=200000
108 Correct 635 ms 67696 KB n=200000
109 Correct 638 ms 67612 KB n=200000
110 Correct 623 ms 67664 KB n=200000
111 Correct 519 ms 67920 KB n=200000
112 Correct 764 ms 73004 KB n=200000
113 Correct 819 ms 69836 KB n=200000
114 Correct 502 ms 67888 KB n=200000
115 Correct 857 ms 68432 KB n=200000
116 Correct 502 ms 67824 KB n=200000
117 Correct 803 ms 72992 KB n=200000
118 Correct 911 ms 69340 KB n=200000
119 Correct 499 ms 67852 KB n=200000
120 Correct 872 ms 73560 KB n=200000
121 Correct 656 ms 73576 KB n=200000
122 Correct 846 ms 73888 KB n=200000
123 Correct 339 ms 68436 KB n=200000
124 Correct 166 ms 37920 KB n=25264