답안 #865773

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
865773 2023-10-24T15:37:34 Z vjudge1 Birthday gift (IZhO18_treearray) C++
100 / 100
1514 ms 161804 KB
// Bolatulu
#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
#define int long long
#define kanagattandirilmagandiktarinizdan ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define pb push_back
#define F first
#define S second
#define md (tl+tr)/2
#define TL v+v,tl,md
#define TR v+v+1,md+1,tr

#pragma GCC target( "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("Ofast,unroll-loops,fast-math,O3")

using namespace std;

int binpow(int a,int n,int M) {
    if (n==0)
        return 1;
    if (n%2!=0)
        return (a * binpow(a,n-1,M))%M;
    int z=binpow(a,n/2,M);
    return (z*z)%M;
}

const ll INF = 1e18;
const int N =  1e6+7;
const int M = 1e9+7;
const ll HZ = 1e5;
const int MAX = INT_MAX;
const int MIN = INT_MIN;
const db pi = 3.141592653;
const int P=31;

int n,m,q,tin[N],tout[N],t,up[20][N],a[N];
vector <int> g[N];

void dfs(int v,int p) {
    tin[v] = ++t;
    up[0][v] = p;
    for (auto now: g[v]) {
        if (now == p)
            continue;
        dfs(now, v);
    }
    tout[v] = ++t;
}

bool parent(int a,int b) {
    return tin[a] <= tin[b] and tout[b] <= tout[a];
}

void prepare(int pw) {
    if (pw == 20)
        return;
    for (int i = 1; i <= n; i++)
        up[pw][i] = up[pw - 1][up[pw - 1][i]];
    prepare(pw + 1);
}

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

void solve() {
    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, 0), prepare(1);
    vector<set<int>> d(n + 1), s(n + 1);
    for (int i = 1; i <= m; i++) {
        cin >> a[i];
        if (i > 1)
            d[lca(a[i - 1], a[i])].insert(i - 1);
        s[a[i]].insert(i);
    }
    while (q--) {
        int tp, l, r, v;
        cin >> tp;
        if (tp == 1) {
            cin >> l >> v;
            if (l > 1)
                d[lca(a[l - 1], a[l])].erase(l - 1), d[lca(a[l - 1], v)].insert(l - 1);
            if (l < m)
                d[lca(a[l], a[l + 1])].erase(l), d[lca(v, a[l + 1])].insert(l);
            s[a[l]].erase(l), s[v].insert(l);
            a[l] = v;
        } else {
            cin >> l >> r >> v;
            if (!s[v].empty() and s[v].lower_bound(l) != s[v].end() and *s[v].lower_bound(l) <= r)
                cout << *s[v].lower_bound(l) << ' ' << *s[v].lower_bound(l) << '\n';
            else if (!d[v].empty() and d[v].lower_bound(l) != d[v].end() and *d[v].lower_bound(l) <= r - 1)
                cout << *d[v].lower_bound(l) << ' ' << *d[v].lower_bound(l) + 1 << '\n';
            else
                cout << -1 << ' ' << -1 << '\n';
        }
    }
}

signed main() {
    // freopen("lca.in", "r", stdin);
    // freopen("lca.out", "w", stdout);
    kanagattandirilmagandiktarinizdan
    int test = 1, count = 1;
    // cin >> test;
    while (test--) {
        // cout << "Case " << count << ":\n";
        solve();
        if (test)
            // cout << '\n';
            count++;
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 68440 KB n=5
2 Correct 10 ms 68188 KB n=100
3 Correct 10 ms 68440 KB n=100
4 Correct 10 ms 68296 KB n=100
5 Correct 10 ms 68188 KB n=100
6 Correct 10 ms 68188 KB n=100
7 Correct 10 ms 68376 KB n=100
8 Correct 10 ms 68188 KB n=100
9 Correct 10 ms 68184 KB n=100
10 Correct 10 ms 68184 KB n=100
11 Correct 10 ms 68184 KB n=100
12 Correct 10 ms 68188 KB n=100
13 Correct 10 ms 68264 KB n=100
14 Correct 10 ms 68188 KB n=100
15 Correct 10 ms 68188 KB n=100
16 Correct 10 ms 68188 KB n=100
17 Correct 10 ms 68440 KB n=100
18 Correct 11 ms 68440 KB n=100
19 Correct 10 ms 68188 KB n=100
20 Correct 10 ms 68216 KB n=100
21 Correct 10 ms 68344 KB n=100
22 Correct 10 ms 68188 KB n=100
23 Correct 10 ms 68188 KB n=100
24 Correct 10 ms 68388 KB n=100
25 Correct 10 ms 68188 KB n=100
26 Correct 10 ms 68208 KB n=12
27 Correct 10 ms 68188 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 68440 KB n=5
2 Correct 10 ms 68188 KB n=100
3 Correct 10 ms 68440 KB n=100
4 Correct 10 ms 68296 KB n=100
5 Correct 10 ms 68188 KB n=100
6 Correct 10 ms 68188 KB n=100
7 Correct 10 ms 68376 KB n=100
8 Correct 10 ms 68188 KB n=100
9 Correct 10 ms 68184 KB n=100
10 Correct 10 ms 68184 KB n=100
11 Correct 10 ms 68184 KB n=100
12 Correct 10 ms 68188 KB n=100
13 Correct 10 ms 68264 KB n=100
14 Correct 10 ms 68188 KB n=100
15 Correct 10 ms 68188 KB n=100
16 Correct 10 ms 68188 KB n=100
17 Correct 10 ms 68440 KB n=100
18 Correct 11 ms 68440 KB n=100
19 Correct 10 ms 68188 KB n=100
20 Correct 10 ms 68216 KB n=100
21 Correct 10 ms 68344 KB n=100
22 Correct 10 ms 68188 KB n=100
23 Correct 10 ms 68188 KB n=100
24 Correct 10 ms 68388 KB n=100
25 Correct 10 ms 68188 KB n=100
26 Correct 10 ms 68208 KB n=12
27 Correct 10 ms 68188 KB n=100
28 Correct 10 ms 68444 KB n=500
29 Correct 10 ms 68304 KB n=500
30 Correct 11 ms 68444 KB n=500
31 Correct 10 ms 68696 KB n=500
32 Correct 10 ms 68328 KB n=500
33 Correct 10 ms 68444 KB n=500
34 Correct 10 ms 68444 KB n=500
35 Correct 10 ms 68440 KB n=500
36 Correct 10 ms 68440 KB n=500
37 Correct 10 ms 68444 KB n=500
38 Correct 10 ms 68444 KB n=500
39 Correct 10 ms 68444 KB n=500
40 Correct 10 ms 68444 KB n=500
41 Correct 10 ms 68440 KB n=500
42 Correct 10 ms 68444 KB n=500
43 Correct 10 ms 68444 KB n=500
44 Correct 10 ms 68440 KB n=500
45 Correct 10 ms 68444 KB n=500
46 Correct 10 ms 68440 KB n=500
47 Correct 12 ms 68444 KB n=500
48 Correct 10 ms 68444 KB n=500
49 Correct 10 ms 68440 KB n=500
50 Correct 10 ms 68444 KB n=500
51 Correct 10 ms 68304 KB n=500
52 Correct 10 ms 68444 KB n=500
53 Correct 10 ms 68444 KB n=500
54 Correct 10 ms 68440 KB n=500
55 Correct 10 ms 68444 KB n=278
56 Correct 10 ms 68444 KB n=500
57 Correct 10 ms 68444 KB n=500
58 Correct 10 ms 68444 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 68440 KB n=5
2 Correct 10 ms 68188 KB n=100
3 Correct 10 ms 68440 KB n=100
4 Correct 10 ms 68296 KB n=100
5 Correct 10 ms 68188 KB n=100
6 Correct 10 ms 68188 KB n=100
7 Correct 10 ms 68376 KB n=100
8 Correct 10 ms 68188 KB n=100
9 Correct 10 ms 68184 KB n=100
10 Correct 10 ms 68184 KB n=100
11 Correct 10 ms 68184 KB n=100
12 Correct 10 ms 68188 KB n=100
13 Correct 10 ms 68264 KB n=100
14 Correct 10 ms 68188 KB n=100
15 Correct 10 ms 68188 KB n=100
16 Correct 10 ms 68188 KB n=100
17 Correct 10 ms 68440 KB n=100
18 Correct 11 ms 68440 KB n=100
19 Correct 10 ms 68188 KB n=100
20 Correct 10 ms 68216 KB n=100
21 Correct 10 ms 68344 KB n=100
22 Correct 10 ms 68188 KB n=100
23 Correct 10 ms 68188 KB n=100
24 Correct 10 ms 68388 KB n=100
25 Correct 10 ms 68188 KB n=100
26 Correct 10 ms 68208 KB n=12
27 Correct 10 ms 68188 KB n=100
28 Correct 10 ms 68444 KB n=500
29 Correct 10 ms 68304 KB n=500
30 Correct 11 ms 68444 KB n=500
31 Correct 10 ms 68696 KB n=500
32 Correct 10 ms 68328 KB n=500
33 Correct 10 ms 68444 KB n=500
34 Correct 10 ms 68444 KB n=500
35 Correct 10 ms 68440 KB n=500
36 Correct 10 ms 68440 KB n=500
37 Correct 10 ms 68444 KB n=500
38 Correct 10 ms 68444 KB n=500
39 Correct 10 ms 68444 KB n=500
40 Correct 10 ms 68444 KB n=500
41 Correct 10 ms 68440 KB n=500
42 Correct 10 ms 68444 KB n=500
43 Correct 10 ms 68444 KB n=500
44 Correct 10 ms 68440 KB n=500
45 Correct 10 ms 68444 KB n=500
46 Correct 10 ms 68440 KB n=500
47 Correct 12 ms 68444 KB n=500
48 Correct 10 ms 68444 KB n=500
49 Correct 10 ms 68440 KB n=500
50 Correct 10 ms 68444 KB n=500
51 Correct 10 ms 68304 KB n=500
52 Correct 10 ms 68444 KB n=500
53 Correct 10 ms 68444 KB n=500
54 Correct 10 ms 68440 KB n=500
55 Correct 10 ms 68444 KB n=278
56 Correct 10 ms 68444 KB n=500
57 Correct 10 ms 68444 KB n=500
58 Correct 10 ms 68444 KB n=500
59 Correct 12 ms 68700 KB n=2000
60 Correct 11 ms 68756 KB n=2000
61 Correct 12 ms 68700 KB n=2000
62 Correct 12 ms 68700 KB n=2000
63 Correct 12 ms 68700 KB n=2000
64 Correct 12 ms 68700 KB n=2000
65 Correct 12 ms 68700 KB n=2000
66 Correct 12 ms 68700 KB n=2000
67 Correct 12 ms 68700 KB n=2000
68 Correct 12 ms 68696 KB n=2000
69 Correct 11 ms 68748 KB n=2000
70 Correct 11 ms 68700 KB n=2000
71 Correct 11 ms 68696 KB n=2000
72 Correct 11 ms 68696 KB n=2000
73 Correct 12 ms 68700 KB n=2000
74 Correct 12 ms 68700 KB n=1844
75 Correct 13 ms 68696 KB n=2000
76 Correct 12 ms 68696 KB n=2000
77 Correct 12 ms 68700 KB n=2000
78 Correct 12 ms 68700 KB n=2000
79 Correct 12 ms 68700 KB n=2000
80 Correct 12 ms 68732 KB n=2000
81 Correct 12 ms 68700 KB n=2000
82 Correct 12 ms 68700 KB n=2000
83 Correct 12 ms 68732 KB n=2000
84 Correct 12 ms 68700 KB n=2000
85 Correct 13 ms 68724 KB n=2000
86 Correct 12 ms 68700 KB n=2000
87 Correct 12 ms 68700 KB n=2000
88 Correct 11 ms 68700 KB n=2000
89 Correct 12 ms 68700 KB n=2000
90 Correct 11 ms 68772 KB n=2000
91 Correct 11 ms 68696 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 68440 KB n=5
2 Correct 10 ms 68188 KB n=100
3 Correct 10 ms 68440 KB n=100
4 Correct 10 ms 68296 KB n=100
5 Correct 10 ms 68188 KB n=100
6 Correct 10 ms 68188 KB n=100
7 Correct 10 ms 68376 KB n=100
8 Correct 10 ms 68188 KB n=100
9 Correct 10 ms 68184 KB n=100
10 Correct 10 ms 68184 KB n=100
11 Correct 10 ms 68184 KB n=100
12 Correct 10 ms 68188 KB n=100
13 Correct 10 ms 68264 KB n=100
14 Correct 10 ms 68188 KB n=100
15 Correct 10 ms 68188 KB n=100
16 Correct 10 ms 68188 KB n=100
17 Correct 10 ms 68440 KB n=100
18 Correct 11 ms 68440 KB n=100
19 Correct 10 ms 68188 KB n=100
20 Correct 10 ms 68216 KB n=100
21 Correct 10 ms 68344 KB n=100
22 Correct 10 ms 68188 KB n=100
23 Correct 10 ms 68188 KB n=100
24 Correct 10 ms 68388 KB n=100
25 Correct 10 ms 68188 KB n=100
26 Correct 10 ms 68208 KB n=12
27 Correct 10 ms 68188 KB n=100
28 Correct 10 ms 68444 KB n=500
29 Correct 10 ms 68304 KB n=500
30 Correct 11 ms 68444 KB n=500
31 Correct 10 ms 68696 KB n=500
32 Correct 10 ms 68328 KB n=500
33 Correct 10 ms 68444 KB n=500
34 Correct 10 ms 68444 KB n=500
35 Correct 10 ms 68440 KB n=500
36 Correct 10 ms 68440 KB n=500
37 Correct 10 ms 68444 KB n=500
38 Correct 10 ms 68444 KB n=500
39 Correct 10 ms 68444 KB n=500
40 Correct 10 ms 68444 KB n=500
41 Correct 10 ms 68440 KB n=500
42 Correct 10 ms 68444 KB n=500
43 Correct 10 ms 68444 KB n=500
44 Correct 10 ms 68440 KB n=500
45 Correct 10 ms 68444 KB n=500
46 Correct 10 ms 68440 KB n=500
47 Correct 12 ms 68444 KB n=500
48 Correct 10 ms 68444 KB n=500
49 Correct 10 ms 68440 KB n=500
50 Correct 10 ms 68444 KB n=500
51 Correct 10 ms 68304 KB n=500
52 Correct 10 ms 68444 KB n=500
53 Correct 10 ms 68444 KB n=500
54 Correct 10 ms 68440 KB n=500
55 Correct 10 ms 68444 KB n=278
56 Correct 10 ms 68444 KB n=500
57 Correct 10 ms 68444 KB n=500
58 Correct 10 ms 68444 KB n=500
59 Correct 12 ms 68700 KB n=2000
60 Correct 11 ms 68756 KB n=2000
61 Correct 12 ms 68700 KB n=2000
62 Correct 12 ms 68700 KB n=2000
63 Correct 12 ms 68700 KB n=2000
64 Correct 12 ms 68700 KB n=2000
65 Correct 12 ms 68700 KB n=2000
66 Correct 12 ms 68700 KB n=2000
67 Correct 12 ms 68700 KB n=2000
68 Correct 12 ms 68696 KB n=2000
69 Correct 11 ms 68748 KB n=2000
70 Correct 11 ms 68700 KB n=2000
71 Correct 11 ms 68696 KB n=2000
72 Correct 11 ms 68696 KB n=2000
73 Correct 12 ms 68700 KB n=2000
74 Correct 12 ms 68700 KB n=1844
75 Correct 13 ms 68696 KB n=2000
76 Correct 12 ms 68696 KB n=2000
77 Correct 12 ms 68700 KB n=2000
78 Correct 12 ms 68700 KB n=2000
79 Correct 12 ms 68700 KB n=2000
80 Correct 12 ms 68732 KB n=2000
81 Correct 12 ms 68700 KB n=2000
82 Correct 12 ms 68700 KB n=2000
83 Correct 12 ms 68732 KB n=2000
84 Correct 12 ms 68700 KB n=2000
85 Correct 13 ms 68724 KB n=2000
86 Correct 12 ms 68700 KB n=2000
87 Correct 12 ms 68700 KB n=2000
88 Correct 11 ms 68700 KB n=2000
89 Correct 12 ms 68700 KB n=2000
90 Correct 11 ms 68772 KB n=2000
91 Correct 11 ms 68696 KB n=2000
92 Correct 595 ms 156728 KB n=200000
93 Correct 854 ms 159084 KB n=200000
94 Correct 487 ms 160340 KB n=200000
95 Correct 559 ms 156352 KB n=200000
96 Correct 531 ms 156228 KB n=200000
97 Correct 932 ms 158524 KB n=200000
98 Correct 536 ms 156452 KB n=200000
99 Correct 680 ms 156628 KB n=200000
100 Correct 611 ms 156656 KB n=200000
101 Correct 458 ms 160736 KB n=200000
102 Correct 357 ms 157436 KB n=200000
103 Correct 351 ms 157560 KB n=200000
104 Correct 349 ms 157436 KB n=200000
105 Correct 379 ms 157652 KB n=200000
106 Correct 352 ms 157524 KB n=200000
107 Correct 364 ms 157404 KB n=200000
108 Correct 631 ms 156316 KB n=200000
109 Correct 660 ms 156104 KB n=200000
110 Correct 646 ms 156112 KB n=200000
111 Correct 667 ms 155976 KB n=200000
112 Correct 507 ms 144556 KB n=200000
113 Correct 1132 ms 128852 KB n=200000
114 Correct 667 ms 139068 KB n=200000
115 Correct 1514 ms 143180 KB n=200000
116 Correct 629 ms 129664 KB n=200000
117 Correct 532 ms 131592 KB n=200000
118 Correct 1227 ms 158136 KB n=200000
119 Correct 595 ms 151112 KB n=200000
120 Correct 401 ms 132812 KB n=200000
121 Correct 386 ms 161804 KB n=200000
122 Correct 377 ms 152660 KB n=200000
123 Correct 364 ms 152060 KB n=200000
124 Correct 120 ms 86864 KB n=25264