Submission #712180

# Submission time Handle Problem Language Result Execution time Memory
712180 2023-03-18T10:16:14 Z Ahmed57 Birthday gift (IZhO18_treearray) C++14
100 / 100
1452 ms 88900 KB
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
const int N=2e5+5;
ordered_set s[N];
set<int> v[N];
vector<int> adj[N];
int P[N][18],hi[N];
int n;vector<int>  nodes;
void dfs(int node,int pa){
    P[node][0]=pa;
    hi[node]=hi[pa]+1;
    for(int k=1;k<=17;k++)
        P[node][k]=P[P[node][k-1]][k-1];
    for(int i:adj[node])
    {
        if(i==pa) continue;
        dfs(i,node);
    }
}
int lca(int x,int y){
    if(hi[x]<hi[y]) swap(x,y);
    for(int k=17;k>=0;k--)
    {
        if(hi[x]-(1<<k) >= hi[y])
            x=P[x][k];
    }
    if(x==y) return x;
    for(int k=17;k>=0;k--)
    {
        if(P[x][k] != P[y][k])
            x=P[x][k],y=P[y][k];
    }
    return P[x][0];
}
void remn(int i){
    auto it = v[nodes[i]].lower_bound(i);
    v[nodes[i]].erase(it);
}
void addn(int i){
    v[nodes[i]].insert(i);
}
void add(int i){
    s[lca(nodes[i],nodes[i+1])].insert(i);
}void rem(int i){
    auto it = s[lca(nodes[i],nodes[i+1])].lower_bound(i);
    s[lca(nodes[i],nodes[i+1])].erase(it);
}
signed main(){
    scanf("%d",&n);
    int k,q;cin>>k>>q;
    for(int i=1;i<n;i++){
        int x,y;
        scanf("%d %d",&x,&y);
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    dfs(1,0);
    for(int i = 0;i<k;i++){
        int x;cin>>x;
        nodes.push_back(x);
    }
    for(int i = 0;i<k-1;i++){add(i);addn(i);}
    addn(k-1);
    while(q--){
        int ty;cin>>ty;
        if(ty==1){
            int pos,val;
            cin>>pos>>val;
            pos--;
            remn(pos);
            if(pos>0)rem(pos-1);
            if(pos<k-1)rem(pos);
            nodes[pos] = val;
            if(pos>0)add(pos-1);
            if(pos<k-1)add(pos);
            addn(pos);
        }else{
            int l,r,vv;cin>>l>>r>>vv;
            l--;r--;
            int rr = s[vv].order_of_key(r);
            int ll = s[vv].order_of_key(l);
            int val = rr-ll;
            if(val>0){
                cout<<*s[vv].find_by_order(ll)+1<<" "<<*s[vv].find_by_order(ll)+2<<"\n";
            }else {
                auto it = v[vv].lower_bound(l);
                if(it!=v[vv].end()&&*it<=r){
                    cout<<*it+1<<" "<<*it+1<<endl;
                }else{
                    cout<<-1<<" "<<-1<<endl;
                }
            }
        }
    }
}

Compilation message

treearray.cpp: In function 'int main()':
treearray.cpp:53:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   53 |     scanf("%d",&n);
      |     ~~~~~^~~~~~~~~
treearray.cpp:57:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |         scanf("%d %d",&x,&y);
      |         ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 18 ms 30036 KB n=5
2 Correct 19 ms 30056 KB n=100
3 Correct 21 ms 30004 KB n=100
4 Correct 21 ms 30060 KB n=100
5 Correct 20 ms 30012 KB n=100
6 Correct 19 ms 29988 KB n=100
7 Correct 19 ms 30036 KB n=100
8 Correct 20 ms 30072 KB n=100
9 Correct 20 ms 30164 KB n=100
10 Correct 19 ms 30064 KB n=100
11 Correct 19 ms 30036 KB n=100
12 Correct 19 ms 30036 KB n=100
13 Correct 19 ms 30036 KB n=100
14 Correct 21 ms 30092 KB n=100
15 Correct 21 ms 30064 KB n=100
16 Correct 24 ms 30004 KB n=100
17 Correct 19 ms 30036 KB n=100
18 Correct 21 ms 30036 KB n=100
19 Correct 22 ms 30008 KB n=100
20 Correct 19 ms 30060 KB n=100
21 Correct 21 ms 30052 KB n=100
22 Correct 23 ms 30052 KB n=100
23 Correct 23 ms 30028 KB n=100
24 Correct 19 ms 30044 KB n=100
25 Correct 20 ms 30096 KB n=100
26 Correct 26 ms 30052 KB n=12
27 Correct 21 ms 30036 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 18 ms 30036 KB n=5
2 Correct 19 ms 30056 KB n=100
3 Correct 21 ms 30004 KB n=100
4 Correct 21 ms 30060 KB n=100
5 Correct 20 ms 30012 KB n=100
6 Correct 19 ms 29988 KB n=100
7 Correct 19 ms 30036 KB n=100
8 Correct 20 ms 30072 KB n=100
9 Correct 20 ms 30164 KB n=100
10 Correct 19 ms 30064 KB n=100
11 Correct 19 ms 30036 KB n=100
12 Correct 19 ms 30036 KB n=100
13 Correct 19 ms 30036 KB n=100
14 Correct 21 ms 30092 KB n=100
15 Correct 21 ms 30064 KB n=100
16 Correct 24 ms 30004 KB n=100
17 Correct 19 ms 30036 KB n=100
18 Correct 21 ms 30036 KB n=100
19 Correct 22 ms 30008 KB n=100
20 Correct 19 ms 30060 KB n=100
21 Correct 21 ms 30052 KB n=100
22 Correct 23 ms 30052 KB n=100
23 Correct 23 ms 30028 KB n=100
24 Correct 19 ms 30044 KB n=100
25 Correct 20 ms 30096 KB n=100
26 Correct 26 ms 30052 KB n=12
27 Correct 21 ms 30036 KB n=100
28 Correct 22 ms 30164 KB n=500
29 Correct 21 ms 30176 KB n=500
30 Correct 21 ms 30172 KB n=500
31 Correct 20 ms 30104 KB n=500
32 Correct 21 ms 30156 KB n=500
33 Correct 22 ms 30164 KB n=500
34 Correct 24 ms 30164 KB n=500
35 Correct 22 ms 30088 KB n=500
36 Correct 22 ms 30140 KB n=500
37 Correct 22 ms 30260 KB n=500
38 Correct 21 ms 30068 KB n=500
39 Correct 21 ms 30164 KB n=500
40 Correct 29 ms 30164 KB n=500
41 Correct 29 ms 30112 KB n=500
42 Correct 20 ms 30176 KB n=500
43 Correct 23 ms 30164 KB n=500
44 Correct 22 ms 30164 KB n=500
45 Correct 21 ms 30072 KB n=500
46 Correct 26 ms 30084 KB n=500
47 Correct 20 ms 30164 KB n=500
48 Correct 19 ms 30176 KB n=500
49 Correct 20 ms 30068 KB n=500
50 Correct 22 ms 30164 KB n=500
51 Correct 22 ms 30156 KB n=500
52 Correct 21 ms 30160 KB n=500
53 Correct 24 ms 30152 KB n=500
54 Correct 20 ms 30164 KB n=500
55 Correct 23 ms 30060 KB n=278
56 Correct 21 ms 30156 KB n=500
57 Correct 21 ms 30176 KB n=500
58 Correct 22 ms 30072 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 18 ms 30036 KB n=5
2 Correct 19 ms 30056 KB n=100
3 Correct 21 ms 30004 KB n=100
4 Correct 21 ms 30060 KB n=100
5 Correct 20 ms 30012 KB n=100
6 Correct 19 ms 29988 KB n=100
7 Correct 19 ms 30036 KB n=100
8 Correct 20 ms 30072 KB n=100
9 Correct 20 ms 30164 KB n=100
10 Correct 19 ms 30064 KB n=100
11 Correct 19 ms 30036 KB n=100
12 Correct 19 ms 30036 KB n=100
13 Correct 19 ms 30036 KB n=100
14 Correct 21 ms 30092 KB n=100
15 Correct 21 ms 30064 KB n=100
16 Correct 24 ms 30004 KB n=100
17 Correct 19 ms 30036 KB n=100
18 Correct 21 ms 30036 KB n=100
19 Correct 22 ms 30008 KB n=100
20 Correct 19 ms 30060 KB n=100
21 Correct 21 ms 30052 KB n=100
22 Correct 23 ms 30052 KB n=100
23 Correct 23 ms 30028 KB n=100
24 Correct 19 ms 30044 KB n=100
25 Correct 20 ms 30096 KB n=100
26 Correct 26 ms 30052 KB n=12
27 Correct 21 ms 30036 KB n=100
28 Correct 22 ms 30164 KB n=500
29 Correct 21 ms 30176 KB n=500
30 Correct 21 ms 30172 KB n=500
31 Correct 20 ms 30104 KB n=500
32 Correct 21 ms 30156 KB n=500
33 Correct 22 ms 30164 KB n=500
34 Correct 24 ms 30164 KB n=500
35 Correct 22 ms 30088 KB n=500
36 Correct 22 ms 30140 KB n=500
37 Correct 22 ms 30260 KB n=500
38 Correct 21 ms 30068 KB n=500
39 Correct 21 ms 30164 KB n=500
40 Correct 29 ms 30164 KB n=500
41 Correct 29 ms 30112 KB n=500
42 Correct 20 ms 30176 KB n=500
43 Correct 23 ms 30164 KB n=500
44 Correct 22 ms 30164 KB n=500
45 Correct 21 ms 30072 KB n=500
46 Correct 26 ms 30084 KB n=500
47 Correct 20 ms 30164 KB n=500
48 Correct 19 ms 30176 KB n=500
49 Correct 20 ms 30068 KB n=500
50 Correct 22 ms 30164 KB n=500
51 Correct 22 ms 30156 KB n=500
52 Correct 21 ms 30160 KB n=500
53 Correct 24 ms 30152 KB n=500
54 Correct 20 ms 30164 KB n=500
55 Correct 23 ms 30060 KB n=278
56 Correct 21 ms 30156 KB n=500
57 Correct 21 ms 30176 KB n=500
58 Correct 22 ms 30072 KB n=500
59 Correct 26 ms 30516 KB n=2000
60 Correct 31 ms 30548 KB n=2000
61 Correct 26 ms 30472 KB n=2000
62 Correct 27 ms 30544 KB n=2000
63 Correct 29 ms 30424 KB n=2000
64 Correct 26 ms 30464 KB n=2000
65 Correct 27 ms 30472 KB n=2000
66 Correct 26 ms 30548 KB n=2000
67 Correct 26 ms 30420 KB n=2000
68 Correct 31 ms 30516 KB n=2000
69 Correct 27 ms 30460 KB n=2000
70 Correct 29 ms 30448 KB n=2000
71 Correct 25 ms 30528 KB n=2000
72 Correct 26 ms 30420 KB n=2000
73 Correct 25 ms 30420 KB n=2000
74 Correct 24 ms 30380 KB n=1844
75 Correct 31 ms 30540 KB n=2000
76 Correct 27 ms 30480 KB n=2000
77 Correct 26 ms 30440 KB n=2000
78 Correct 25 ms 30420 KB n=2000
79 Correct 29 ms 30520 KB n=2000
80 Correct 30 ms 30532 KB n=2000
81 Correct 26 ms 30548 KB n=2000
82 Correct 27 ms 30452 KB n=2000
83 Correct 25 ms 30548 KB n=2000
84 Correct 24 ms 30420 KB n=2000
85 Correct 24 ms 30540 KB n=2000
86 Correct 25 ms 30520 KB n=2000
87 Correct 26 ms 30512 KB n=2000
88 Correct 25 ms 30588 KB n=2000
89 Correct 30 ms 30548 KB n=2000
90 Correct 24 ms 30568 KB n=2000
91 Correct 25 ms 30460 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 18 ms 30036 KB n=5
2 Correct 19 ms 30056 KB n=100
3 Correct 21 ms 30004 KB n=100
4 Correct 21 ms 30060 KB n=100
5 Correct 20 ms 30012 KB n=100
6 Correct 19 ms 29988 KB n=100
7 Correct 19 ms 30036 KB n=100
8 Correct 20 ms 30072 KB n=100
9 Correct 20 ms 30164 KB n=100
10 Correct 19 ms 30064 KB n=100
11 Correct 19 ms 30036 KB n=100
12 Correct 19 ms 30036 KB n=100
13 Correct 19 ms 30036 KB n=100
14 Correct 21 ms 30092 KB n=100
15 Correct 21 ms 30064 KB n=100
16 Correct 24 ms 30004 KB n=100
17 Correct 19 ms 30036 KB n=100
18 Correct 21 ms 30036 KB n=100
19 Correct 22 ms 30008 KB n=100
20 Correct 19 ms 30060 KB n=100
21 Correct 21 ms 30052 KB n=100
22 Correct 23 ms 30052 KB n=100
23 Correct 23 ms 30028 KB n=100
24 Correct 19 ms 30044 KB n=100
25 Correct 20 ms 30096 KB n=100
26 Correct 26 ms 30052 KB n=12
27 Correct 21 ms 30036 KB n=100
28 Correct 22 ms 30164 KB n=500
29 Correct 21 ms 30176 KB n=500
30 Correct 21 ms 30172 KB n=500
31 Correct 20 ms 30104 KB n=500
32 Correct 21 ms 30156 KB n=500
33 Correct 22 ms 30164 KB n=500
34 Correct 24 ms 30164 KB n=500
35 Correct 22 ms 30088 KB n=500
36 Correct 22 ms 30140 KB n=500
37 Correct 22 ms 30260 KB n=500
38 Correct 21 ms 30068 KB n=500
39 Correct 21 ms 30164 KB n=500
40 Correct 29 ms 30164 KB n=500
41 Correct 29 ms 30112 KB n=500
42 Correct 20 ms 30176 KB n=500
43 Correct 23 ms 30164 KB n=500
44 Correct 22 ms 30164 KB n=500
45 Correct 21 ms 30072 KB n=500
46 Correct 26 ms 30084 KB n=500
47 Correct 20 ms 30164 KB n=500
48 Correct 19 ms 30176 KB n=500
49 Correct 20 ms 30068 KB n=500
50 Correct 22 ms 30164 KB n=500
51 Correct 22 ms 30156 KB n=500
52 Correct 21 ms 30160 KB n=500
53 Correct 24 ms 30152 KB n=500
54 Correct 20 ms 30164 KB n=500
55 Correct 23 ms 30060 KB n=278
56 Correct 21 ms 30156 KB n=500
57 Correct 21 ms 30176 KB n=500
58 Correct 22 ms 30072 KB n=500
59 Correct 26 ms 30516 KB n=2000
60 Correct 31 ms 30548 KB n=2000
61 Correct 26 ms 30472 KB n=2000
62 Correct 27 ms 30544 KB n=2000
63 Correct 29 ms 30424 KB n=2000
64 Correct 26 ms 30464 KB n=2000
65 Correct 27 ms 30472 KB n=2000
66 Correct 26 ms 30548 KB n=2000
67 Correct 26 ms 30420 KB n=2000
68 Correct 31 ms 30516 KB n=2000
69 Correct 27 ms 30460 KB n=2000
70 Correct 29 ms 30448 KB n=2000
71 Correct 25 ms 30528 KB n=2000
72 Correct 26 ms 30420 KB n=2000
73 Correct 25 ms 30420 KB n=2000
74 Correct 24 ms 30380 KB n=1844
75 Correct 31 ms 30540 KB n=2000
76 Correct 27 ms 30480 KB n=2000
77 Correct 26 ms 30440 KB n=2000
78 Correct 25 ms 30420 KB n=2000
79 Correct 29 ms 30520 KB n=2000
80 Correct 30 ms 30532 KB n=2000
81 Correct 26 ms 30548 KB n=2000
82 Correct 27 ms 30452 KB n=2000
83 Correct 25 ms 30548 KB n=2000
84 Correct 24 ms 30420 KB n=2000
85 Correct 24 ms 30540 KB n=2000
86 Correct 25 ms 30520 KB n=2000
87 Correct 26 ms 30512 KB n=2000
88 Correct 25 ms 30588 KB n=2000
89 Correct 30 ms 30548 KB n=2000
90 Correct 24 ms 30568 KB n=2000
91 Correct 25 ms 30460 KB n=2000
92 Correct 1187 ms 79224 KB n=200000
93 Correct 1413 ms 84108 KB n=200000
94 Correct 1327 ms 87548 KB n=200000
95 Correct 1188 ms 78900 KB n=200000
96 Correct 1134 ms 79144 KB n=200000
97 Correct 1425 ms 83152 KB n=200000
98 Correct 1060 ms 78804 KB n=200000
99 Correct 1297 ms 79184 KB n=200000
100 Correct 1066 ms 78996 KB n=200000
101 Correct 1421 ms 88900 KB n=200000
102 Correct 874 ms 80280 KB n=200000
103 Correct 880 ms 80344 KB n=200000
104 Correct 946 ms 80296 KB n=200000
105 Correct 924 ms 80672 KB n=200000
106 Correct 896 ms 80660 KB n=200000
107 Correct 949 ms 80644 KB n=200000
108 Correct 1163 ms 79128 KB n=200000
109 Correct 1188 ms 79196 KB n=200000
110 Correct 1192 ms 79112 KB n=200000
111 Correct 1061 ms 78496 KB n=200000
112 Correct 1339 ms 87700 KB n=200000
113 Correct 1452 ms 83240 KB n=200000
114 Correct 1062 ms 78608 KB n=200000
115 Correct 1441 ms 81256 KB n=200000
116 Correct 959 ms 79228 KB n=200000
117 Correct 1293 ms 88196 KB n=200000
118 Correct 1400 ms 82084 KB n=200000
119 Correct 995 ms 79196 KB n=200000
120 Correct 1216 ms 87672 KB n=200000
121 Correct 1211 ms 87692 KB n=200000
122 Correct 1321 ms 88184 KB n=200000
123 Correct 909 ms 80440 KB n=200000
124 Correct 362 ms 44880 KB n=25264