Submission #1114249

# Submission time Handle Problem Language Result Execution time Memory
1114249 2024-11-18T12:36:21 Z dosts Birthday gift (IZhO18_treearray) C++17
100 / 100
983 ms 97312 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 11612 KB n=100
7 Correct 3 ms 11764 KB n=100
8 Correct 3 ms 11600 KB n=100
9 Correct 3 ms 11600 KB n=100
10 Correct 3 ms 11600 KB n=100
11 Correct 3 ms 11600 KB n=100
12 Correct 3 ms 11600 KB n=100
13 Correct 3 ms 11768 KB n=100
14 Correct 3 ms 11600 KB n=100
15 Correct 3 ms 11600 KB n=100
16 Correct 3 ms 11600 KB n=100
17 Correct 3 ms 11600 KB n=100
18 Correct 3 ms 11784 KB n=100
19 Correct 3 ms 11600 KB n=100
20 Correct 3 ms 11600 KB n=100
21 Correct 3 ms 11600 KB n=100
22 Correct 3 ms 11600 KB n=100
23 Correct 3 ms 11600 KB n=100
24 Correct 3 ms 11600 KB n=100
25 Correct 3 ms 11600 KB n=100
26 Correct 3 ms 11600 KB n=12
27 Correct 3 ms 11600 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 11612 KB n=100
7 Correct 3 ms 11764 KB n=100
8 Correct 3 ms 11600 KB n=100
9 Correct 3 ms 11600 KB n=100
10 Correct 3 ms 11600 KB n=100
11 Correct 3 ms 11600 KB n=100
12 Correct 3 ms 11600 KB n=100
13 Correct 3 ms 11768 KB n=100
14 Correct 3 ms 11600 KB n=100
15 Correct 3 ms 11600 KB n=100
16 Correct 3 ms 11600 KB n=100
17 Correct 3 ms 11600 KB n=100
18 Correct 3 ms 11784 KB n=100
19 Correct 3 ms 11600 KB n=100
20 Correct 3 ms 11600 KB n=100
21 Correct 3 ms 11600 KB n=100
22 Correct 3 ms 11600 KB n=100
23 Correct 3 ms 11600 KB n=100
24 Correct 3 ms 11600 KB n=100
25 Correct 3 ms 11600 KB n=100
26 Correct 3 ms 11600 KB n=12
27 Correct 3 ms 11600 KB n=100
28 Correct 3 ms 11856 KB n=500
29 Correct 4 ms 11856 KB n=500
30 Correct 4 ms 11856 KB n=500
31 Correct 3 ms 11856 KB n=500
32 Correct 3 ms 11856 KB n=500
33 Correct 4 ms 11856 KB n=500
34 Correct 3 ms 11856 KB n=500
35 Correct 3 ms 11856 KB n=500
36 Correct 4 ms 11856 KB n=500
37 Correct 4 ms 11856 KB n=500
38 Correct 4 ms 11856 KB n=500
39 Correct 3 ms 11856 KB n=500
40 Correct 3 ms 11856 KB n=500
41 Correct 3 ms 11856 KB n=500
42 Correct 4 ms 12028 KB n=500
43 Correct 3 ms 11856 KB n=500
44 Correct 4 ms 11856 KB n=500
45 Correct 4 ms 11856 KB n=500
46 Correct 3 ms 11856 KB n=500
47 Correct 3 ms 11856 KB n=500
48 Correct 4 ms 11856 KB n=500
49 Correct 4 ms 11856 KB n=500
50 Correct 4 ms 11856 KB n=500
51 Correct 5 ms 11856 KB n=500
52 Correct 3 ms 11856 KB n=500
53 Correct 3 ms 11856 KB n=500
54 Correct 4 ms 11856 KB n=500
55 Correct 3 ms 12024 KB n=278
56 Correct 3 ms 11856 KB n=500
57 Correct 3 ms 11856 KB n=500
58 Correct 3 ms 11856 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 11612 KB n=100
7 Correct 3 ms 11764 KB n=100
8 Correct 3 ms 11600 KB n=100
9 Correct 3 ms 11600 KB n=100
10 Correct 3 ms 11600 KB n=100
11 Correct 3 ms 11600 KB n=100
12 Correct 3 ms 11600 KB n=100
13 Correct 3 ms 11768 KB n=100
14 Correct 3 ms 11600 KB n=100
15 Correct 3 ms 11600 KB n=100
16 Correct 3 ms 11600 KB n=100
17 Correct 3 ms 11600 KB n=100
18 Correct 3 ms 11784 KB n=100
19 Correct 3 ms 11600 KB n=100
20 Correct 3 ms 11600 KB n=100
21 Correct 3 ms 11600 KB n=100
22 Correct 3 ms 11600 KB n=100
23 Correct 3 ms 11600 KB n=100
24 Correct 3 ms 11600 KB n=100
25 Correct 3 ms 11600 KB n=100
26 Correct 3 ms 11600 KB n=12
27 Correct 3 ms 11600 KB n=100
28 Correct 3 ms 11856 KB n=500
29 Correct 4 ms 11856 KB n=500
30 Correct 4 ms 11856 KB n=500
31 Correct 3 ms 11856 KB n=500
32 Correct 3 ms 11856 KB n=500
33 Correct 4 ms 11856 KB n=500
34 Correct 3 ms 11856 KB n=500
35 Correct 3 ms 11856 KB n=500
36 Correct 4 ms 11856 KB n=500
37 Correct 4 ms 11856 KB n=500
38 Correct 4 ms 11856 KB n=500
39 Correct 3 ms 11856 KB n=500
40 Correct 3 ms 11856 KB n=500
41 Correct 3 ms 11856 KB n=500
42 Correct 4 ms 12028 KB n=500
43 Correct 3 ms 11856 KB n=500
44 Correct 4 ms 11856 KB n=500
45 Correct 4 ms 11856 KB n=500
46 Correct 3 ms 11856 KB n=500
47 Correct 3 ms 11856 KB n=500
48 Correct 4 ms 11856 KB n=500
49 Correct 4 ms 11856 KB n=500
50 Correct 4 ms 11856 KB n=500
51 Correct 5 ms 11856 KB n=500
52 Correct 3 ms 11856 KB n=500
53 Correct 3 ms 11856 KB n=500
54 Correct 4 ms 11856 KB n=500
55 Correct 3 ms 12024 KB n=278
56 Correct 3 ms 11856 KB n=500
57 Correct 3 ms 11856 KB n=500
58 Correct 3 ms 11856 KB n=500
59 Correct 5 ms 14416 KB n=2000
60 Correct 5 ms 14416 KB n=2000
61 Correct 5 ms 14584 KB n=2000
62 Correct 8 ms 14352 KB n=2000
63 Correct 5 ms 14416 KB n=2000
64 Correct 7 ms 14416 KB n=2000
65 Correct 5 ms 14416 KB n=2000
66 Correct 5 ms 14416 KB n=2000
67 Correct 6 ms 14416 KB n=2000
68 Correct 5 ms 14532 KB n=2000
69 Correct 5 ms 14416 KB n=2000
70 Correct 4 ms 14416 KB n=2000
71 Correct 5 ms 14416 KB n=2000
72 Correct 5 ms 14416 KB n=2000
73 Correct 5 ms 14416 KB n=2000
74 Correct 5 ms 14416 KB n=1844
75 Correct 5 ms 14416 KB n=2000
76 Correct 6 ms 14416 KB n=2000
77 Correct 5 ms 14416 KB n=2000
78 Correct 6 ms 14416 KB n=2000
79 Correct 6 ms 14416 KB n=2000
80 Correct 5 ms 14416 KB n=2000
81 Correct 5 ms 14416 KB n=2000
82 Correct 5 ms 14580 KB n=2000
83 Correct 6 ms 14344 KB n=2000
84 Correct 6 ms 14584 KB n=2000
85 Correct 5 ms 14584 KB n=2000
86 Correct 9 ms 14416 KB n=2000
87 Correct 6 ms 14416 KB n=2000
88 Correct 5 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 11612 KB n=100
7 Correct 3 ms 11764 KB n=100
8 Correct 3 ms 11600 KB n=100
9 Correct 3 ms 11600 KB n=100
10 Correct 3 ms 11600 KB n=100
11 Correct 3 ms 11600 KB n=100
12 Correct 3 ms 11600 KB n=100
13 Correct 3 ms 11768 KB n=100
14 Correct 3 ms 11600 KB n=100
15 Correct 3 ms 11600 KB n=100
16 Correct 3 ms 11600 KB n=100
17 Correct 3 ms 11600 KB n=100
18 Correct 3 ms 11784 KB n=100
19 Correct 3 ms 11600 KB n=100
20 Correct 3 ms 11600 KB n=100
21 Correct 3 ms 11600 KB n=100
22 Correct 3 ms 11600 KB n=100
23 Correct 3 ms 11600 KB n=100
24 Correct 3 ms 11600 KB n=100
25 Correct 3 ms 11600 KB n=100
26 Correct 3 ms 11600 KB n=12
27 Correct 3 ms 11600 KB n=100
28 Correct 3 ms 11856 KB n=500
29 Correct 4 ms 11856 KB n=500
30 Correct 4 ms 11856 KB n=500
31 Correct 3 ms 11856 KB n=500
32 Correct 3 ms 11856 KB n=500
33 Correct 4 ms 11856 KB n=500
34 Correct 3 ms 11856 KB n=500
35 Correct 3 ms 11856 KB n=500
36 Correct 4 ms 11856 KB n=500
37 Correct 4 ms 11856 KB n=500
38 Correct 4 ms 11856 KB n=500
39 Correct 3 ms 11856 KB n=500
40 Correct 3 ms 11856 KB n=500
41 Correct 3 ms 11856 KB n=500
42 Correct 4 ms 12028 KB n=500
43 Correct 3 ms 11856 KB n=500
44 Correct 4 ms 11856 KB n=500
45 Correct 4 ms 11856 KB n=500
46 Correct 3 ms 11856 KB n=500
47 Correct 3 ms 11856 KB n=500
48 Correct 4 ms 11856 KB n=500
49 Correct 4 ms 11856 KB n=500
50 Correct 4 ms 11856 KB n=500
51 Correct 5 ms 11856 KB n=500
52 Correct 3 ms 11856 KB n=500
53 Correct 3 ms 11856 KB n=500
54 Correct 4 ms 11856 KB n=500
55 Correct 3 ms 12024 KB n=278
56 Correct 3 ms 11856 KB n=500
57 Correct 3 ms 11856 KB n=500
58 Correct 3 ms 11856 KB n=500
59 Correct 5 ms 14416 KB n=2000
60 Correct 5 ms 14416 KB n=2000
61 Correct 5 ms 14584 KB n=2000
62 Correct 8 ms 14352 KB n=2000
63 Correct 5 ms 14416 KB n=2000
64 Correct 7 ms 14416 KB n=2000
65 Correct 5 ms 14416 KB n=2000
66 Correct 5 ms 14416 KB n=2000
67 Correct 6 ms 14416 KB n=2000
68 Correct 5 ms 14532 KB n=2000
69 Correct 5 ms 14416 KB n=2000
70 Correct 4 ms 14416 KB n=2000
71 Correct 5 ms 14416 KB n=2000
72 Correct 5 ms 14416 KB n=2000
73 Correct 5 ms 14416 KB n=2000
74 Correct 5 ms 14416 KB n=1844
75 Correct 5 ms 14416 KB n=2000
76 Correct 6 ms 14416 KB n=2000
77 Correct 5 ms 14416 KB n=2000
78 Correct 6 ms 14416 KB n=2000
79 Correct 6 ms 14416 KB n=2000
80 Correct 5 ms 14416 KB n=2000
81 Correct 5 ms 14416 KB n=2000
82 Correct 5 ms 14580 KB n=2000
83 Correct 6 ms 14344 KB n=2000
84 Correct 6 ms 14584 KB n=2000
85 Correct 5 ms 14584 KB n=2000
86 Correct 9 ms 14416 KB n=2000
87 Correct 6 ms 14416 KB n=2000
88 Correct 5 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 580 ms 95512 KB n=200000
93 Correct 719 ms 95848 KB n=200000
94 Correct 622 ms 93992 KB n=200000
95 Correct 681 ms 96184 KB n=200000
96 Correct 674 ms 96284 KB n=200000
97 Correct 846 ms 95912 KB n=200000
98 Correct 678 ms 96484 KB n=200000
99 Correct 663 ms 95816 KB n=200000
100 Correct 679 ms 93944 KB n=200000
101 Correct 550 ms 93768 KB n=200000
102 Correct 421 ms 95500 KB n=200000
103 Correct 379 ms 96948 KB n=200000
104 Correct 374 ms 95304 KB n=200000
105 Correct 376 ms 95864 KB n=200000
106 Correct 337 ms 97312 KB n=200000
107 Correct 363 ms 95852 KB n=200000
108 Correct 594 ms 94464 KB n=200000
109 Correct 675 ms 95652 KB n=200000
110 Correct 639 ms 94240 KB n=200000
111 Correct 693 ms 95676 KB n=200000
112 Correct 504 ms 93892 KB n=200000
113 Correct 818 ms 94280 KB n=200000
114 Correct 694 ms 93476 KB n=200000
115 Correct 983 ms 94536 KB n=200000
116 Correct 536 ms 94948 KB n=200000
117 Correct 466 ms 94984 KB n=200000
118 Correct 836 ms 95792 KB n=200000
119 Correct 558 ms 96072 KB n=200000
120 Correct 342 ms 92544 KB n=200000
121 Correct 370 ms 92488 KB n=200000
122 Correct 347 ms 92744 KB n=200000
123 Correct 334 ms 97124 KB n=200000
124 Correct 97 ms 29772 KB n=25264