Submission #1114286

# Submission time Handle Problem Language Result Execution time Memory
1114286 2024-11-18T13:26:37 Z vjudge1 Birthday gift (IZhO18_treearray) C++17
100 / 100
904 ms 97352 KB
//Dost SEFEROĞLU
#include <bits/stdc++.h>
#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
using namespace std;
#define int long long
#define pii pair<int,int>
#define ff first
#define ss second
#define sp << " " <<    
#define all(cont) cont.begin(),cont.end()
#define vi vector<int>
const int MOD = 1e9+7,inf = 2e18;
const int N = 2e5+50,Q = 2e5+50;
 
 
vi edges[N];
int tin[N],tout[N];
int A[N],B[N];
int timer = 1;
int up[N][20];
 
void dfs(int node,int p) {
    tin[node] = timer++;
    up[node][0] = p;
    for (auto it : edges[node]) {
        if (it != p) dfs(it,node);
    }
    tout[node] = timer-1;
}
 
bool anc(int a,int b) {
    return tin[a] <= tin[b] && tout[a] >= tout[b];
}
 
int lca(int a,int b) {
    if (anc(a,b)) return a;
    if (anc(b,a)) return b;
    for (int i = 19;i>=0;i--) if (!anc(up[a][i],b)) a = up[a][i];
    return up[a][0];
}
 
void solve() { 
    int n,m,q;
    cin >> n >> m >> q;
    for (int i=1;i<=n-1;i++) {
        int a,b;
        cin >> a >> b;
        edges[a].push_back(b);
        edges[b].push_back(a);
    }
    dfs(1,1);
    set<int> pos[n+1],pos2[n+1];
    for (int i = 1;i<=m;i++) {
        cin >> B[i];
        pos[B[i]].insert(i);
    }
    for (int i=1;i<20;i++) {
        for (int j = 1;j<=n;j++) up[j][i] = up[up[j][i-1]][i-1];
    }
    for (int i=1;i<=m-1;i++) {
        pos2[lca(B[i],B[i+1])].insert(i);
    }
    while (q--) {
        int type;
        cin >> type;
        if (type == 1) {
            int p,v;
            cin >> p >> v;
            if (p>1) pos2[lca(B[p],B[p-1])].erase(p-1);
            if (p<m) pos2[lca(B[p],B[p+1])].erase(p);
            pos[B[p]].erase(p);
            B[p] = v;
            pos[v].insert(p);
            if (p > 1) pos2[lca(B[p],B[p-1])].insert(p-1);
            if (p < m) pos2[lca(B[p],B[p+1])].insert(p);
        }
        else {
            int l,r,v;
            cin >> l >> r >> v;
            auto it = pos[v].lower_bound(l);
            auto it2 = pos2[v].lower_bound(l);
            if ((it == pos[v].end() || *it > r)  && (it2 == pos2[v].end() || *it2>=r)) {
                cout << -1 << " " << -1 << '\n';
                continue;
            }
            if (it != pos[v].end() && *it <= r) {
                assert(*it >= 1 && *it <= m);
                cout << *it << " " << *it << '\n';
            }
            else {
                assert(*it2 >= 1 && *it2 < m);
                cout << *it2 << " " << *it2+1 <<'\n';
            }
        }
    }
}                    
                             
signed main() { 
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    #ifdef Dodi
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    int t = 1;
    //cin >> t; 
    while (t --> 0) solve();
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 11600 KB n=5
2 Correct 3 ms 11600 KB n=100
3 Correct 3 ms 11600 KB n=100
4 Correct 3 ms 11600 KB n=100
5 Correct 3 ms 11600 KB n=100
6 Correct 3 ms 11768 KB n=100
7 Correct 3 ms 11600 KB n=100
8 Correct 3 ms 11600 KB n=100
9 Correct 3 ms 11600 KB n=100
10 Correct 3 ms 11944 KB n=100
11 Correct 3 ms 11600 KB n=100
12 Correct 3 ms 11600 KB n=100
13 Correct 4 ms 11600 KB n=100
14 Correct 3 ms 11600 KB n=100
15 Correct 3 ms 11776 KB n=100
16 Correct 4 ms 11600 KB n=100
17 Correct 4 ms 11600 KB n=100
18 Correct 3 ms 11600 KB n=100
19 Correct 3 ms 11948 KB n=100
20 Correct 3 ms 11600 KB n=100
21 Correct 4 ms 11600 KB n=100
22 Correct 3 ms 11600 KB n=100
23 Correct 4 ms 11600 KB n=100
24 Correct 3 ms 11600 KB n=100
25 Correct 3 ms 9552 KB n=100
26 Correct 4 ms 5200 KB n=12
27 Correct 3 ms 5456 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 3 ms 11600 KB n=5
2 Correct 3 ms 11600 KB n=100
3 Correct 3 ms 11600 KB n=100
4 Correct 3 ms 11600 KB n=100
5 Correct 3 ms 11600 KB n=100
6 Correct 3 ms 11768 KB n=100
7 Correct 3 ms 11600 KB n=100
8 Correct 3 ms 11600 KB n=100
9 Correct 3 ms 11600 KB n=100
10 Correct 3 ms 11944 KB n=100
11 Correct 3 ms 11600 KB n=100
12 Correct 3 ms 11600 KB n=100
13 Correct 4 ms 11600 KB n=100
14 Correct 3 ms 11600 KB n=100
15 Correct 3 ms 11776 KB n=100
16 Correct 4 ms 11600 KB n=100
17 Correct 4 ms 11600 KB n=100
18 Correct 3 ms 11600 KB n=100
19 Correct 3 ms 11948 KB n=100
20 Correct 3 ms 11600 KB n=100
21 Correct 4 ms 11600 KB n=100
22 Correct 3 ms 11600 KB n=100
23 Correct 4 ms 11600 KB n=100
24 Correct 3 ms 11600 KB n=100
25 Correct 3 ms 9552 KB n=100
26 Correct 4 ms 5200 KB n=12
27 Correct 3 ms 5456 KB n=100
28 Correct 5 ms 11856 KB n=500
29 Correct 3 ms 11856 KB n=500
30 Correct 4 ms 11856 KB n=500
31 Correct 4 ms 11856 KB n=500
32 Correct 4 ms 11856 KB n=500
33 Correct 4 ms 11856 KB n=500
34 Correct 4 ms 11856 KB n=500
35 Correct 4 ms 11856 KB n=500
36 Correct 3 ms 11856 KB n=500
37 Correct 4 ms 11856 KB n=500
38 Correct 4 ms 11792 KB n=500
39 Correct 4 ms 11856 KB n=500
40 Correct 4 ms 9808 KB n=500
41 Correct 4 ms 11856 KB n=500
42 Correct 3 ms 11856 KB n=500
43 Correct 4 ms 11856 KB n=500
44 Correct 3 ms 11856 KB n=500
45 Correct 4 ms 11856 KB n=500
46 Correct 3 ms 11856 KB n=500
47 Correct 4 ms 11856 KB n=500
48 Correct 3 ms 11856 KB n=500
49 Correct 4 ms 11856 KB n=500
50 Correct 3 ms 11856 KB n=500
51 Correct 3 ms 11856 KB n=500
52 Correct 4 ms 11856 KB n=500
53 Correct 3 ms 11856 KB n=500
54 Correct 5 ms 11856 KB n=500
55 Correct 3 ms 11856 KB n=278
56 Correct 3 ms 11856 KB n=500
57 Correct 3 ms 11856 KB n=500
58 Correct 4 ms 9808 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 3 ms 11600 KB n=5
2 Correct 3 ms 11600 KB n=100
3 Correct 3 ms 11600 KB n=100
4 Correct 3 ms 11600 KB n=100
5 Correct 3 ms 11600 KB n=100
6 Correct 3 ms 11768 KB n=100
7 Correct 3 ms 11600 KB n=100
8 Correct 3 ms 11600 KB n=100
9 Correct 3 ms 11600 KB n=100
10 Correct 3 ms 11944 KB n=100
11 Correct 3 ms 11600 KB n=100
12 Correct 3 ms 11600 KB n=100
13 Correct 4 ms 11600 KB n=100
14 Correct 3 ms 11600 KB n=100
15 Correct 3 ms 11776 KB n=100
16 Correct 4 ms 11600 KB n=100
17 Correct 4 ms 11600 KB n=100
18 Correct 3 ms 11600 KB n=100
19 Correct 3 ms 11948 KB n=100
20 Correct 3 ms 11600 KB n=100
21 Correct 4 ms 11600 KB n=100
22 Correct 3 ms 11600 KB n=100
23 Correct 4 ms 11600 KB n=100
24 Correct 3 ms 11600 KB n=100
25 Correct 3 ms 9552 KB n=100
26 Correct 4 ms 5200 KB n=12
27 Correct 3 ms 5456 KB n=100
28 Correct 5 ms 11856 KB n=500
29 Correct 3 ms 11856 KB n=500
30 Correct 4 ms 11856 KB n=500
31 Correct 4 ms 11856 KB n=500
32 Correct 4 ms 11856 KB n=500
33 Correct 4 ms 11856 KB n=500
34 Correct 4 ms 11856 KB n=500
35 Correct 4 ms 11856 KB n=500
36 Correct 3 ms 11856 KB n=500
37 Correct 4 ms 11856 KB n=500
38 Correct 4 ms 11792 KB n=500
39 Correct 4 ms 11856 KB n=500
40 Correct 4 ms 9808 KB n=500
41 Correct 4 ms 11856 KB n=500
42 Correct 3 ms 11856 KB n=500
43 Correct 4 ms 11856 KB n=500
44 Correct 3 ms 11856 KB n=500
45 Correct 4 ms 11856 KB n=500
46 Correct 3 ms 11856 KB n=500
47 Correct 4 ms 11856 KB n=500
48 Correct 3 ms 11856 KB n=500
49 Correct 4 ms 11856 KB n=500
50 Correct 3 ms 11856 KB n=500
51 Correct 3 ms 11856 KB n=500
52 Correct 4 ms 11856 KB n=500
53 Correct 3 ms 11856 KB n=500
54 Correct 5 ms 11856 KB n=500
55 Correct 3 ms 11856 KB n=278
56 Correct 3 ms 11856 KB n=500
57 Correct 3 ms 11856 KB n=500
58 Correct 4 ms 9808 KB n=500
59 Correct 7 ms 14416 KB n=2000
60 Correct 5 ms 14416 KB n=2000
61 Correct 7 ms 14416 KB n=2000
62 Correct 6 ms 14416 KB n=2000
63 Correct 6 ms 14416 KB n=2000
64 Correct 7 ms 14416 KB n=2000
65 Correct 7 ms 14416 KB n=2000
66 Correct 6 ms 14428 KB n=2000
67 Correct 6 ms 14420 KB n=2000
68 Correct 6 ms 14416 KB n=2000
69 Correct 5 ms 14416 KB n=2000
70 Correct 5 ms 14416 KB n=2000
71 Correct 6 ms 14416 KB n=2000
72 Correct 6 ms 14584 KB n=2000
73 Correct 5 ms 14416 KB n=2000
74 Correct 6 ms 14416 KB n=1844
75 Correct 5 ms 14416 KB n=2000
76 Correct 6 ms 14416 KB n=2000
77 Correct 6 ms 14584 KB n=2000
78 Correct 9 ms 14524 KB n=2000
79 Correct 7 ms 14416 KB n=2000
80 Correct 6 ms 14520 KB n=2000
81 Correct 6 ms 10576 KB n=2000
82 Correct 6 ms 14416 KB n=2000
83 Correct 6 ms 14416 KB n=2000
84 Correct 6 ms 14416 KB n=2000
85 Correct 6 ms 14416 KB n=2000
86 Correct 6 ms 14432 KB n=2000
87 Correct 6 ms 14348 KB n=2000
88 Correct 7 ms 14416 KB n=2000
89 Correct 5 ms 14416 KB n=2000
90 Correct 5 ms 14416 KB n=2000
91 Correct 5 ms 14416 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 3 ms 11600 KB n=5
2 Correct 3 ms 11600 KB n=100
3 Correct 3 ms 11600 KB n=100
4 Correct 3 ms 11600 KB n=100
5 Correct 3 ms 11600 KB n=100
6 Correct 3 ms 11768 KB n=100
7 Correct 3 ms 11600 KB n=100
8 Correct 3 ms 11600 KB n=100
9 Correct 3 ms 11600 KB n=100
10 Correct 3 ms 11944 KB n=100
11 Correct 3 ms 11600 KB n=100
12 Correct 3 ms 11600 KB n=100
13 Correct 4 ms 11600 KB n=100
14 Correct 3 ms 11600 KB n=100
15 Correct 3 ms 11776 KB n=100
16 Correct 4 ms 11600 KB n=100
17 Correct 4 ms 11600 KB n=100
18 Correct 3 ms 11600 KB n=100
19 Correct 3 ms 11948 KB n=100
20 Correct 3 ms 11600 KB n=100
21 Correct 4 ms 11600 KB n=100
22 Correct 3 ms 11600 KB n=100
23 Correct 4 ms 11600 KB n=100
24 Correct 3 ms 11600 KB n=100
25 Correct 3 ms 9552 KB n=100
26 Correct 4 ms 5200 KB n=12
27 Correct 3 ms 5456 KB n=100
28 Correct 5 ms 11856 KB n=500
29 Correct 3 ms 11856 KB n=500
30 Correct 4 ms 11856 KB n=500
31 Correct 4 ms 11856 KB n=500
32 Correct 4 ms 11856 KB n=500
33 Correct 4 ms 11856 KB n=500
34 Correct 4 ms 11856 KB n=500
35 Correct 4 ms 11856 KB n=500
36 Correct 3 ms 11856 KB n=500
37 Correct 4 ms 11856 KB n=500
38 Correct 4 ms 11792 KB n=500
39 Correct 4 ms 11856 KB n=500
40 Correct 4 ms 9808 KB n=500
41 Correct 4 ms 11856 KB n=500
42 Correct 3 ms 11856 KB n=500
43 Correct 4 ms 11856 KB n=500
44 Correct 3 ms 11856 KB n=500
45 Correct 4 ms 11856 KB n=500
46 Correct 3 ms 11856 KB n=500
47 Correct 4 ms 11856 KB n=500
48 Correct 3 ms 11856 KB n=500
49 Correct 4 ms 11856 KB n=500
50 Correct 3 ms 11856 KB n=500
51 Correct 3 ms 11856 KB n=500
52 Correct 4 ms 11856 KB n=500
53 Correct 3 ms 11856 KB n=500
54 Correct 5 ms 11856 KB n=500
55 Correct 3 ms 11856 KB n=278
56 Correct 3 ms 11856 KB n=500
57 Correct 3 ms 11856 KB n=500
58 Correct 4 ms 9808 KB n=500
59 Correct 7 ms 14416 KB n=2000
60 Correct 5 ms 14416 KB n=2000
61 Correct 7 ms 14416 KB n=2000
62 Correct 6 ms 14416 KB n=2000
63 Correct 6 ms 14416 KB n=2000
64 Correct 7 ms 14416 KB n=2000
65 Correct 7 ms 14416 KB n=2000
66 Correct 6 ms 14428 KB n=2000
67 Correct 6 ms 14420 KB n=2000
68 Correct 6 ms 14416 KB n=2000
69 Correct 5 ms 14416 KB n=2000
70 Correct 5 ms 14416 KB n=2000
71 Correct 6 ms 14416 KB n=2000
72 Correct 6 ms 14584 KB n=2000
73 Correct 5 ms 14416 KB n=2000
74 Correct 6 ms 14416 KB n=1844
75 Correct 5 ms 14416 KB n=2000
76 Correct 6 ms 14416 KB n=2000
77 Correct 6 ms 14584 KB n=2000
78 Correct 9 ms 14524 KB n=2000
79 Correct 7 ms 14416 KB n=2000
80 Correct 6 ms 14520 KB n=2000
81 Correct 6 ms 10576 KB n=2000
82 Correct 6 ms 14416 KB n=2000
83 Correct 6 ms 14416 KB n=2000
84 Correct 6 ms 14416 KB n=2000
85 Correct 6 ms 14416 KB n=2000
86 Correct 6 ms 14432 KB n=2000
87 Correct 6 ms 14348 KB n=2000
88 Correct 7 ms 14416 KB n=2000
89 Correct 5 ms 14416 KB n=2000
90 Correct 5 ms 14416 KB n=2000
91 Correct 5 ms 14416 KB n=2000
92 Correct 543 ms 95428 KB n=200000
93 Correct 620 ms 96152 KB n=200000
94 Correct 542 ms 95560 KB n=200000
95 Correct 542 ms 96268 KB n=200000
96 Correct 551 ms 96440 KB n=200000
97 Correct 615 ms 96112 KB n=200000
98 Correct 460 ms 96612 KB n=200000
99 Correct 565 ms 95900 KB n=200000
100 Correct 526 ms 95772 KB n=200000
101 Correct 364 ms 95140 KB n=200000
102 Correct 315 ms 97048 KB n=200000
103 Correct 298 ms 96848 KB n=200000
104 Correct 327 ms 95308 KB n=200000
105 Correct 340 ms 95700 KB n=200000
106 Correct 322 ms 97352 KB n=200000
107 Correct 310 ms 95816 KB n=200000
108 Correct 544 ms 94276 KB n=200000
109 Correct 578 ms 95816 KB n=200000
110 Correct 595 ms 94140 KB n=200000
111 Correct 633 ms 95700 KB n=200000
112 Correct 469 ms 94812 KB n=200000
113 Correct 815 ms 95788 KB n=200000
114 Correct 614 ms 95928 KB n=200000
115 Correct 807 ms 96064 KB n=200000
116 Correct 520 ms 96616 KB n=200000
117 Correct 449 ms 95048 KB n=200000
118 Correct 904 ms 95684 KB n=200000
119 Correct 535 ms 94648 KB n=200000
120 Correct 374 ms 92548 KB n=200000
121 Correct 437 ms 92540 KB n=200000
122 Correct 393 ms 92932 KB n=200000
123 Correct 363 ms 97116 KB n=200000
124 Correct 102 ms 29768 KB n=25264