답안 #523384

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
523384 2022-02-07T15:05:33 Z vonatlus Birthday gift (IZhO18_treearray) C++17
56 / 100
1708 ms 179028 KB
/// adil sultanov | vonat1us 

#pragma GCC optimize("O3")
//#pragma comment(linker, "/STACK:36777216")

#include<bits/stdc++.h>

#define x first
#define y second
#define pb push_back
#define sz(x) (int) x.size()
#define all(z) (z).begin(), (z).end()
 
using namespace std;

using ll = long long;
using pii = pair<int, int>;                                   

const int MOD = 1e9 + 7; 
const int INF = 1e9 + 1e2;
  
void fin() {
#ifdef AM
    freopen(".in", "r", stdin);
#endif        
}                   

const bool flag = 0;

const int N = 1e6+10;
const int K = 20;

set<int> s[N], one[N];

vector<int> adj[N];

int up[K][N], tin[N], tout[N], tmr;

void dfs(int x = 0, int p = 0) {
    up[0][x] = p;
    for (int i = 1; i < K; ++i) {
        up[i][x] = up[i-1][up[i-1][x]];
    }
    tin[x] = ++tmr;
    for (int to : adj[x]) {
        if (to != p) {
            dfs(to, x);
        }
    }
    tout[x] = tmr;
}

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 = K-1; ~i; --i) {
        if (!upper(up[i][a], b)) {
            a = up[i][a];
        }
    }
    return up[0][a];
}

void ma1n() {
    int n, m, q;
    cin >> n >> m >> q;
    for (int i = 1; i < n; ++i) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    vector<int> a(m);
    for (int& x : a) cin >> x, x--;

    dfs();
    for (int i = 0; i < m; ++i) {
        one[a[i]].insert(i);
    }
    for (int i = 0; i < m-1; ++i) {
        int l = lca(a[i+1], a[i]);
        s[l].insert(i);
    }
    
    for (int i = 0; i < q; ++i) {
        int t;
        cin >> t;
        if (t == 1) {
            int pos, v;
            cin >> pos >> v;
            pos--, v--;
            one[a[pos]].erase(pos);

            if (pos+1 < n) s[lca(a[pos], a[pos+1])].erase(pos);
            if (pos > 0) s[lca(a[pos-1], a[pos])].erase(pos-1);
            
            a[pos] = v;
            one[a[pos]].insert(pos);

            if (pos+1 < n) s[lca(a[pos], a[pos+1])].insert(pos);
            if (pos > 0) s[lca(a[pos-1], a[pos])].insert(pos-1);
        } else {
            int l, r, v;
            cin >> l >> r >> v;
            l--, r--, v--;
            auto it = one[v].lower_bound(l);
            if (it != one[v].end() && *it <= r) {
                cout << *it+1 << " " << *it+1 << "\n";
                continue;
            }      
            it = s[v].lower_bound(l);
            if (it != s[v].end() && *it < r) {
                cout << *it+1 << " " << *it +2 << "\n";
            } else {
                cout << "-1 -1\n";
            }
        }
    }
} 

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr), fin();
    int ts = 1;
    if (flag) {
        cin >> ts;
    }
    while (ts--) {
        ma1n(); 
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 57 ms 117828 KB n=5
2 Correct 59 ms 117812 KB n=100
3 Correct 58 ms 117896 KB n=100
4 Correct 56 ms 117852 KB n=100
5 Correct 67 ms 117796 KB n=100
6 Correct 59 ms 117812 KB n=100
7 Correct 57 ms 117880 KB n=100
8 Correct 59 ms 117880 KB n=100
9 Correct 64 ms 117852 KB n=100
10 Correct 56 ms 117820 KB n=100
11 Correct 58 ms 117916 KB n=100
12 Correct 56 ms 117812 KB n=100
13 Correct 58 ms 117864 KB n=100
14 Correct 56 ms 117892 KB n=100
15 Correct 57 ms 117916 KB n=100
16 Correct 55 ms 117872 KB n=100
17 Correct 62 ms 117892 KB n=100
18 Correct 61 ms 117860 KB n=100
19 Correct 55 ms 117880 KB n=100
20 Correct 55 ms 117896 KB n=100
21 Correct 56 ms 117828 KB n=100
22 Correct 58 ms 117848 KB n=100
23 Correct 61 ms 117920 KB n=100
24 Correct 57 ms 117816 KB n=100
25 Correct 57 ms 117848 KB n=100
26 Correct 55 ms 117828 KB n=12
27 Correct 57 ms 117852 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 57 ms 117828 KB n=5
2 Correct 59 ms 117812 KB n=100
3 Correct 58 ms 117896 KB n=100
4 Correct 56 ms 117852 KB n=100
5 Correct 67 ms 117796 KB n=100
6 Correct 59 ms 117812 KB n=100
7 Correct 57 ms 117880 KB n=100
8 Correct 59 ms 117880 KB n=100
9 Correct 64 ms 117852 KB n=100
10 Correct 56 ms 117820 KB n=100
11 Correct 58 ms 117916 KB n=100
12 Correct 56 ms 117812 KB n=100
13 Correct 58 ms 117864 KB n=100
14 Correct 56 ms 117892 KB n=100
15 Correct 57 ms 117916 KB n=100
16 Correct 55 ms 117872 KB n=100
17 Correct 62 ms 117892 KB n=100
18 Correct 61 ms 117860 KB n=100
19 Correct 55 ms 117880 KB n=100
20 Correct 55 ms 117896 KB n=100
21 Correct 56 ms 117828 KB n=100
22 Correct 58 ms 117848 KB n=100
23 Correct 61 ms 117920 KB n=100
24 Correct 57 ms 117816 KB n=100
25 Correct 57 ms 117848 KB n=100
26 Correct 55 ms 117828 KB n=12
27 Correct 57 ms 117852 KB n=100
28 Correct 69 ms 117956 KB n=500
29 Correct 58 ms 117920 KB n=500
30 Correct 56 ms 117956 KB n=500
31 Correct 60 ms 117984 KB n=500
32 Correct 58 ms 117992 KB n=500
33 Correct 63 ms 118000 KB n=500
34 Correct 64 ms 117984 KB n=500
35 Correct 56 ms 118028 KB n=500
36 Correct 63 ms 117956 KB n=500
37 Correct 56 ms 117956 KB n=500
38 Correct 66 ms 118016 KB n=500
39 Correct 58 ms 117956 KB n=500
40 Correct 58 ms 117956 KB n=500
41 Correct 57 ms 118032 KB n=500
42 Correct 58 ms 117944 KB n=500
43 Correct 61 ms 117984 KB n=500
44 Correct 57 ms 118020 KB n=500
45 Correct 57 ms 118020 KB n=500
46 Correct 58 ms 117956 KB n=500
47 Correct 59 ms 117956 KB n=500
48 Correct 65 ms 117900 KB n=500
49 Correct 60 ms 117952 KB n=500
50 Correct 61 ms 117952 KB n=500
51 Correct 61 ms 118140 KB n=500
52 Correct 60 ms 117980 KB n=500
53 Correct 58 ms 117972 KB n=500
54 Correct 58 ms 117968 KB n=500
55 Correct 70 ms 117928 KB n=278
56 Correct 60 ms 117956 KB n=500
57 Correct 56 ms 117924 KB n=500
58 Correct 57 ms 118008 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 57 ms 117828 KB n=5
2 Correct 59 ms 117812 KB n=100
3 Correct 58 ms 117896 KB n=100
4 Correct 56 ms 117852 KB n=100
5 Correct 67 ms 117796 KB n=100
6 Correct 59 ms 117812 KB n=100
7 Correct 57 ms 117880 KB n=100
8 Correct 59 ms 117880 KB n=100
9 Correct 64 ms 117852 KB n=100
10 Correct 56 ms 117820 KB n=100
11 Correct 58 ms 117916 KB n=100
12 Correct 56 ms 117812 KB n=100
13 Correct 58 ms 117864 KB n=100
14 Correct 56 ms 117892 KB n=100
15 Correct 57 ms 117916 KB n=100
16 Correct 55 ms 117872 KB n=100
17 Correct 62 ms 117892 KB n=100
18 Correct 61 ms 117860 KB n=100
19 Correct 55 ms 117880 KB n=100
20 Correct 55 ms 117896 KB n=100
21 Correct 56 ms 117828 KB n=100
22 Correct 58 ms 117848 KB n=100
23 Correct 61 ms 117920 KB n=100
24 Correct 57 ms 117816 KB n=100
25 Correct 57 ms 117848 KB n=100
26 Correct 55 ms 117828 KB n=12
27 Correct 57 ms 117852 KB n=100
28 Correct 69 ms 117956 KB n=500
29 Correct 58 ms 117920 KB n=500
30 Correct 56 ms 117956 KB n=500
31 Correct 60 ms 117984 KB n=500
32 Correct 58 ms 117992 KB n=500
33 Correct 63 ms 118000 KB n=500
34 Correct 64 ms 117984 KB n=500
35 Correct 56 ms 118028 KB n=500
36 Correct 63 ms 117956 KB n=500
37 Correct 56 ms 117956 KB n=500
38 Correct 66 ms 118016 KB n=500
39 Correct 58 ms 117956 KB n=500
40 Correct 58 ms 117956 KB n=500
41 Correct 57 ms 118032 KB n=500
42 Correct 58 ms 117944 KB n=500
43 Correct 61 ms 117984 KB n=500
44 Correct 57 ms 118020 KB n=500
45 Correct 57 ms 118020 KB n=500
46 Correct 58 ms 117956 KB n=500
47 Correct 59 ms 117956 KB n=500
48 Correct 65 ms 117900 KB n=500
49 Correct 60 ms 117952 KB n=500
50 Correct 61 ms 117952 KB n=500
51 Correct 61 ms 118140 KB n=500
52 Correct 60 ms 117980 KB n=500
53 Correct 58 ms 117972 KB n=500
54 Correct 58 ms 117968 KB n=500
55 Correct 70 ms 117928 KB n=278
56 Correct 60 ms 117956 KB n=500
57 Correct 56 ms 117924 KB n=500
58 Correct 57 ms 118008 KB n=500
59 Correct 59 ms 118356 KB n=2000
60 Correct 59 ms 118372 KB n=2000
61 Correct 71 ms 118336 KB n=2000
62 Correct 68 ms 118464 KB n=2000
63 Correct 70 ms 118292 KB n=2000
64 Correct 60 ms 118420 KB n=2000
65 Correct 61 ms 118356 KB n=2000
66 Correct 64 ms 118328 KB n=2000
67 Correct 76 ms 118384 KB n=2000
68 Correct 66 ms 118316 KB n=2000
69 Correct 60 ms 118328 KB n=2000
70 Correct 58 ms 118376 KB n=2000
71 Correct 61 ms 118332 KB n=2000
72 Correct 59 ms 118340 KB n=2000
73 Correct 58 ms 118356 KB n=2000
74 Correct 59 ms 118332 KB n=1844
75 Correct 58 ms 118384 KB n=2000
76 Correct 65 ms 118284 KB n=2000
77 Correct 62 ms 118380 KB n=2000
78 Correct 65 ms 118340 KB n=2000
79 Correct 63 ms 118272 KB n=2000
80 Correct 60 ms 118344 KB n=2000
81 Correct 60 ms 118340 KB n=2000
82 Correct 59 ms 118324 KB n=2000
83 Correct 60 ms 118440 KB n=2000
84 Correct 59 ms 118336 KB n=2000
85 Correct 65 ms 118344 KB n=2000
86 Correct 65 ms 118376 KB n=2000
87 Correct 63 ms 118284 KB n=2000
88 Correct 63 ms 118476 KB n=2000
89 Correct 60 ms 118356 KB n=2000
90 Correct 59 ms 118384 KB n=2000
91 Correct 61 ms 118396 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 57 ms 117828 KB n=5
2 Correct 59 ms 117812 KB n=100
3 Correct 58 ms 117896 KB n=100
4 Correct 56 ms 117852 KB n=100
5 Correct 67 ms 117796 KB n=100
6 Correct 59 ms 117812 KB n=100
7 Correct 57 ms 117880 KB n=100
8 Correct 59 ms 117880 KB n=100
9 Correct 64 ms 117852 KB n=100
10 Correct 56 ms 117820 KB n=100
11 Correct 58 ms 117916 KB n=100
12 Correct 56 ms 117812 KB n=100
13 Correct 58 ms 117864 KB n=100
14 Correct 56 ms 117892 KB n=100
15 Correct 57 ms 117916 KB n=100
16 Correct 55 ms 117872 KB n=100
17 Correct 62 ms 117892 KB n=100
18 Correct 61 ms 117860 KB n=100
19 Correct 55 ms 117880 KB n=100
20 Correct 55 ms 117896 KB n=100
21 Correct 56 ms 117828 KB n=100
22 Correct 58 ms 117848 KB n=100
23 Correct 61 ms 117920 KB n=100
24 Correct 57 ms 117816 KB n=100
25 Correct 57 ms 117848 KB n=100
26 Correct 55 ms 117828 KB n=12
27 Correct 57 ms 117852 KB n=100
28 Correct 69 ms 117956 KB n=500
29 Correct 58 ms 117920 KB n=500
30 Correct 56 ms 117956 KB n=500
31 Correct 60 ms 117984 KB n=500
32 Correct 58 ms 117992 KB n=500
33 Correct 63 ms 118000 KB n=500
34 Correct 64 ms 117984 KB n=500
35 Correct 56 ms 118028 KB n=500
36 Correct 63 ms 117956 KB n=500
37 Correct 56 ms 117956 KB n=500
38 Correct 66 ms 118016 KB n=500
39 Correct 58 ms 117956 KB n=500
40 Correct 58 ms 117956 KB n=500
41 Correct 57 ms 118032 KB n=500
42 Correct 58 ms 117944 KB n=500
43 Correct 61 ms 117984 KB n=500
44 Correct 57 ms 118020 KB n=500
45 Correct 57 ms 118020 KB n=500
46 Correct 58 ms 117956 KB n=500
47 Correct 59 ms 117956 KB n=500
48 Correct 65 ms 117900 KB n=500
49 Correct 60 ms 117952 KB n=500
50 Correct 61 ms 117952 KB n=500
51 Correct 61 ms 118140 KB n=500
52 Correct 60 ms 117980 KB n=500
53 Correct 58 ms 117972 KB n=500
54 Correct 58 ms 117968 KB n=500
55 Correct 70 ms 117928 KB n=278
56 Correct 60 ms 117956 KB n=500
57 Correct 56 ms 117924 KB n=500
58 Correct 57 ms 118008 KB n=500
59 Correct 59 ms 118356 KB n=2000
60 Correct 59 ms 118372 KB n=2000
61 Correct 71 ms 118336 KB n=2000
62 Correct 68 ms 118464 KB n=2000
63 Correct 70 ms 118292 KB n=2000
64 Correct 60 ms 118420 KB n=2000
65 Correct 61 ms 118356 KB n=2000
66 Correct 64 ms 118328 KB n=2000
67 Correct 76 ms 118384 KB n=2000
68 Correct 66 ms 118316 KB n=2000
69 Correct 60 ms 118328 KB n=2000
70 Correct 58 ms 118376 KB n=2000
71 Correct 61 ms 118332 KB n=2000
72 Correct 59 ms 118340 KB n=2000
73 Correct 58 ms 118356 KB n=2000
74 Correct 59 ms 118332 KB n=1844
75 Correct 58 ms 118384 KB n=2000
76 Correct 65 ms 118284 KB n=2000
77 Correct 62 ms 118380 KB n=2000
78 Correct 65 ms 118340 KB n=2000
79 Correct 63 ms 118272 KB n=2000
80 Correct 60 ms 118344 KB n=2000
81 Correct 60 ms 118340 KB n=2000
82 Correct 59 ms 118324 KB n=2000
83 Correct 60 ms 118440 KB n=2000
84 Correct 59 ms 118336 KB n=2000
85 Correct 65 ms 118344 KB n=2000
86 Correct 65 ms 118376 KB n=2000
87 Correct 63 ms 118284 KB n=2000
88 Correct 63 ms 118476 KB n=2000
89 Correct 60 ms 118356 KB n=2000
90 Correct 59 ms 118384 KB n=2000
91 Correct 61 ms 118396 KB n=2000
92 Correct 1708 ms 169548 KB n=200000
93 Correct 1169 ms 174236 KB n=200000
94 Correct 691 ms 177712 KB n=200000
95 Correct 1597 ms 169196 KB n=200000
96 Correct 1528 ms 169364 KB n=200000
97 Correct 1203 ms 173468 KB n=200000
98 Correct 1516 ms 169184 KB n=200000
99 Correct 1566 ms 169584 KB n=200000
100 Correct 1536 ms 169340 KB n=200000
101 Correct 598 ms 179028 KB n=200000
102 Correct 716 ms 170480 KB n=200000
103 Correct 647 ms 170388 KB n=200000
104 Correct 650 ms 170416 KB n=200000
105 Correct 669 ms 170860 KB n=200000
106 Correct 685 ms 170804 KB n=200000
107 Correct 666 ms 170916 KB n=200000
108 Correct 1380 ms 169376 KB n=200000
109 Correct 1449 ms 169476 KB n=200000
110 Correct 1456 ms 169400 KB n=200000
111 Correct 1377 ms 168692 KB n=200000
112 Correct 599 ms 177984 KB n=200000
113 Correct 1195 ms 173368 KB n=200000
114 Correct 1308 ms 168800 KB n=200000
115 Correct 1503 ms 171360 KB n=200000
116 Correct 1472 ms 169548 KB n=200000
117 Correct 576 ms 178272 KB n=200000
118 Correct 1331 ms 172376 KB n=200000
119 Correct 1463 ms 169548 KB n=200000
120 Correct 475 ms 177868 KB n=200000
121 Correct 472 ms 177892 KB n=200000
122 Correct 473 ms 178216 KB n=200000
123 Correct 690 ms 170708 KB n=200000
124 Incorrect 197 ms 132940 KB Wrong range.
125 Halted 0 ms 0 KB -