Submission #542383

# Submission time Handle Problem Language Result Execution time Memory
542383 2022-03-26T10:31:00 Z krit3379 Birthday gift (IZhO18_treearray) C++17
100 / 100
1762 ms 71484 KB
#include<bits/stdc++.h>
using namespace std;
#define N 200005
 
struct A{
    int v,l,r;
    bool operator<(const A& o)const{
        if(v!=o.v)return v<o.v;
        if(l!=o.l)return l<o.l;
        return r<o.r;
    }
};
 
int dep[N],bp[20][N],val[N];
vector<int> g[N];
multiset<A> s;
 
void dfs(int s,int f){
    dep[s]=dep[f]+1;
    bp[0][s]=f;
    for(auto x:g[s])if(x!=f)dfs(x,s);
}
 
int lca(int a,int b){
    if(dep[a]>dep[b])swap(a,b);
    int i,dif=dep[b]-dep[a];
    for(i=0;i<20;i++)if(dif&(1<<i))b=bp[i][b];
    if(a==b)return a;
    for(i=19;i>=0;i--)if(bp[i][a]!=bp[i][b]){
        a=bp[i][a];
        b=bp[i][b];
    }
    return bp[0][a];
}
 
int main(){
    int n,m,q,i,j,a,b,op,pos,v,l,r;
    scanf("%d %d %d",&n,&m,&q);
    for(i=1;i<n;i++){
        scanf("%d %d",&a,&b);
        g[a].push_back(b);
        g[b].push_back(a);
    }
    for(i=1;i<=m;i++)scanf("%d",&val[i]),s.insert({val[i],i,i});
    dfs(1,0);
    for(i=1;i<20;i++)for(j=1;j<=n;j++)bp[i][j]=bp[i-1][bp[i-1][j]];
    for(i=2;i<=m;i++)s.insert({lca(val[i],val[i-1]),i-1,i});
    while(q--){
        scanf("%d",&op);
        if(op==1){
            scanf("%d %d",&pos,&v);
            s.erase(s.find({val[pos],pos,pos}));
            if(pos>1)s.erase(s.find({lca(val[pos],val[pos-1]),pos-1,pos}));
            if(pos<m)s.erase(s.find({lca(val[pos],val[pos+1]),pos,pos+1}));
            val[pos]=v;
            s.insert({val[pos],pos,pos});
            if(pos>1)s.insert({lca(val[pos],val[pos-1]),pos-1,pos});
            if(pos<m)s.insert({lca(val[pos],val[pos+1]),pos,pos+1});
        }
        else{
            scanf("%d %d %d",&l,&r,&v);
            auto it=s.lower_bound({v,l,l});
            if(it==s.end()){printf("-1 -1\n");continue;}
            A x=*it;
            if(x.v==v&&l<=x.l&&x.r<=r)printf("%d %d\n",x.l,x.r);
            else printf("-1 -1\n");
        }
    }
    return 0;
}

Compilation message

treearray.cpp: In function 'int main()':
treearray.cpp:38:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |     scanf("%d %d %d",&n,&m,&q);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~
treearray.cpp:40:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |         scanf("%d %d",&a,&b);
      |         ~~~~~^~~~~~~~~~~~~~~
treearray.cpp:44:27: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   44 |     for(i=1;i<=m;i++)scanf("%d",&val[i]),s.insert({val[i],i,i});
      |                      ~~~~~^~~~~~~~~~~~~~
treearray.cpp:49:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   49 |         scanf("%d",&op);
      |         ~~~~~^~~~~~~~~~
treearray.cpp:51:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   51 |             scanf("%d %d",&pos,&v);
      |             ~~~~~^~~~~~~~~~~~~~~~~
treearray.cpp:61:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |             scanf("%d %d %d",&l,&r,&v);
      |             ~~~~~^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB n=5
2 Correct 3 ms 5076 KB n=100
3 Correct 2 ms 5076 KB n=100
4 Correct 2 ms 5076 KB n=100
5 Correct 4 ms 5144 KB n=100
6 Correct 3 ms 5076 KB n=100
7 Correct 3 ms 5076 KB n=100
8 Correct 3 ms 5076 KB n=100
9 Correct 4 ms 5100 KB n=100
10 Correct 3 ms 5076 KB n=100
11 Correct 3 ms 5076 KB n=100
12 Correct 3 ms 5076 KB n=100
13 Correct 3 ms 5076 KB n=100
14 Correct 3 ms 5076 KB n=100
15 Correct 3 ms 5076 KB n=100
16 Correct 3 ms 5076 KB n=100
17 Correct 3 ms 5076 KB n=100
18 Correct 3 ms 5076 KB n=100
19 Correct 3 ms 5076 KB n=100
20 Correct 3 ms 5116 KB n=100
21 Correct 3 ms 5076 KB n=100
22 Correct 3 ms 5076 KB n=100
23 Correct 4 ms 5076 KB n=100
24 Correct 4 ms 5076 KB n=100
25 Correct 3 ms 5076 KB n=100
26 Correct 3 ms 5076 KB n=12
27 Correct 3 ms 5136 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB n=5
2 Correct 3 ms 5076 KB n=100
3 Correct 2 ms 5076 KB n=100
4 Correct 2 ms 5076 KB n=100
5 Correct 4 ms 5144 KB n=100
6 Correct 3 ms 5076 KB n=100
7 Correct 3 ms 5076 KB n=100
8 Correct 3 ms 5076 KB n=100
9 Correct 4 ms 5100 KB n=100
10 Correct 3 ms 5076 KB n=100
11 Correct 3 ms 5076 KB n=100
12 Correct 3 ms 5076 KB n=100
13 Correct 3 ms 5076 KB n=100
14 Correct 3 ms 5076 KB n=100
15 Correct 3 ms 5076 KB n=100
16 Correct 3 ms 5076 KB n=100
17 Correct 3 ms 5076 KB n=100
18 Correct 3 ms 5076 KB n=100
19 Correct 3 ms 5076 KB n=100
20 Correct 3 ms 5116 KB n=100
21 Correct 3 ms 5076 KB n=100
22 Correct 3 ms 5076 KB n=100
23 Correct 4 ms 5076 KB n=100
24 Correct 4 ms 5076 KB n=100
25 Correct 3 ms 5076 KB n=100
26 Correct 3 ms 5076 KB n=12
27 Correct 3 ms 5136 KB n=100
28 Correct 3 ms 5204 KB n=500
29 Correct 4 ms 5204 KB n=500
30 Correct 5 ms 5204 KB n=500
31 Correct 3 ms 5204 KB n=500
32 Correct 3 ms 5140 KB n=500
33 Correct 4 ms 5204 KB n=500
34 Correct 4 ms 5204 KB n=500
35 Correct 4 ms 5204 KB n=500
36 Correct 4 ms 5204 KB n=500
37 Correct 4 ms 5204 KB n=500
38 Correct 4 ms 5204 KB n=500
39 Correct 3 ms 5204 KB n=500
40 Correct 4 ms 5204 KB n=500
41 Correct 4 ms 5204 KB n=500
42 Correct 4 ms 5204 KB n=500
43 Correct 4 ms 5204 KB n=500
44 Correct 4 ms 5204 KB n=500
45 Correct 4 ms 5212 KB n=500
46 Correct 4 ms 5204 KB n=500
47 Correct 4 ms 5204 KB n=500
48 Correct 4 ms 5144 KB n=500
49 Correct 3 ms 5204 KB n=500
50 Correct 5 ms 5204 KB n=500
51 Correct 5 ms 5148 KB n=500
52 Correct 4 ms 5204 KB n=500
53 Correct 4 ms 5204 KB n=500
54 Correct 4 ms 5204 KB n=500
55 Correct 4 ms 5204 KB n=278
56 Correct 4 ms 5204 KB n=500
57 Correct 4 ms 5204 KB n=500
58 Correct 4 ms 5204 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB n=5
2 Correct 3 ms 5076 KB n=100
3 Correct 2 ms 5076 KB n=100
4 Correct 2 ms 5076 KB n=100
5 Correct 4 ms 5144 KB n=100
6 Correct 3 ms 5076 KB n=100
7 Correct 3 ms 5076 KB n=100
8 Correct 3 ms 5076 KB n=100
9 Correct 4 ms 5100 KB n=100
10 Correct 3 ms 5076 KB n=100
11 Correct 3 ms 5076 KB n=100
12 Correct 3 ms 5076 KB n=100
13 Correct 3 ms 5076 KB n=100
14 Correct 3 ms 5076 KB n=100
15 Correct 3 ms 5076 KB n=100
16 Correct 3 ms 5076 KB n=100
17 Correct 3 ms 5076 KB n=100
18 Correct 3 ms 5076 KB n=100
19 Correct 3 ms 5076 KB n=100
20 Correct 3 ms 5116 KB n=100
21 Correct 3 ms 5076 KB n=100
22 Correct 3 ms 5076 KB n=100
23 Correct 4 ms 5076 KB n=100
24 Correct 4 ms 5076 KB n=100
25 Correct 3 ms 5076 KB n=100
26 Correct 3 ms 5076 KB n=12
27 Correct 3 ms 5136 KB n=100
28 Correct 3 ms 5204 KB n=500
29 Correct 4 ms 5204 KB n=500
30 Correct 5 ms 5204 KB n=500
31 Correct 3 ms 5204 KB n=500
32 Correct 3 ms 5140 KB n=500
33 Correct 4 ms 5204 KB n=500
34 Correct 4 ms 5204 KB n=500
35 Correct 4 ms 5204 KB n=500
36 Correct 4 ms 5204 KB n=500
37 Correct 4 ms 5204 KB n=500
38 Correct 4 ms 5204 KB n=500
39 Correct 3 ms 5204 KB n=500
40 Correct 4 ms 5204 KB n=500
41 Correct 4 ms 5204 KB n=500
42 Correct 4 ms 5204 KB n=500
43 Correct 4 ms 5204 KB n=500
44 Correct 4 ms 5204 KB n=500
45 Correct 4 ms 5212 KB n=500
46 Correct 4 ms 5204 KB n=500
47 Correct 4 ms 5204 KB n=500
48 Correct 4 ms 5144 KB n=500
49 Correct 3 ms 5204 KB n=500
50 Correct 5 ms 5204 KB n=500
51 Correct 5 ms 5148 KB n=500
52 Correct 4 ms 5204 KB n=500
53 Correct 4 ms 5204 KB n=500
54 Correct 4 ms 5204 KB n=500
55 Correct 4 ms 5204 KB n=278
56 Correct 4 ms 5204 KB n=500
57 Correct 4 ms 5204 KB n=500
58 Correct 4 ms 5204 KB n=500
59 Correct 7 ms 5588 KB n=2000
60 Correct 7 ms 5844 KB n=2000
61 Correct 8 ms 5716 KB n=2000
62 Correct 7 ms 5624 KB n=2000
63 Correct 7 ms 5588 KB n=2000
64 Correct 8 ms 5656 KB n=2000
65 Correct 6 ms 5660 KB n=2000
66 Correct 6 ms 5716 KB n=2000
67 Correct 8 ms 5588 KB n=2000
68 Correct 7 ms 5588 KB n=2000
69 Correct 6 ms 5588 KB n=2000
70 Correct 7 ms 5664 KB n=2000
71 Correct 7 ms 5588 KB n=2000
72 Correct 6 ms 5588 KB n=2000
73 Correct 6 ms 5588 KB n=2000
74 Correct 6 ms 5588 KB n=1844
75 Correct 5 ms 5588 KB n=2000
76 Correct 7 ms 5588 KB n=2000
77 Correct 7 ms 5588 KB n=2000
78 Correct 6 ms 5660 KB n=2000
79 Correct 7 ms 5588 KB n=2000
80 Correct 7 ms 5660 KB n=2000
81 Correct 7 ms 5660 KB n=2000
82 Correct 7 ms 5588 KB n=2000
83 Correct 6 ms 5716 KB n=2000
84 Correct 7 ms 5672 KB n=2000
85 Correct 6 ms 5588 KB n=2000
86 Correct 7 ms 5588 KB n=2000
87 Correct 7 ms 5588 KB n=2000
88 Correct 7 ms 5716 KB n=2000
89 Correct 7 ms 5716 KB n=2000
90 Correct 7 ms 5844 KB n=2000
91 Correct 6 ms 5660 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB n=5
2 Correct 3 ms 5076 KB n=100
3 Correct 2 ms 5076 KB n=100
4 Correct 2 ms 5076 KB n=100
5 Correct 4 ms 5144 KB n=100
6 Correct 3 ms 5076 KB n=100
7 Correct 3 ms 5076 KB n=100
8 Correct 3 ms 5076 KB n=100
9 Correct 4 ms 5100 KB n=100
10 Correct 3 ms 5076 KB n=100
11 Correct 3 ms 5076 KB n=100
12 Correct 3 ms 5076 KB n=100
13 Correct 3 ms 5076 KB n=100
14 Correct 3 ms 5076 KB n=100
15 Correct 3 ms 5076 KB n=100
16 Correct 3 ms 5076 KB n=100
17 Correct 3 ms 5076 KB n=100
18 Correct 3 ms 5076 KB n=100
19 Correct 3 ms 5076 KB n=100
20 Correct 3 ms 5116 KB n=100
21 Correct 3 ms 5076 KB n=100
22 Correct 3 ms 5076 KB n=100
23 Correct 4 ms 5076 KB n=100
24 Correct 4 ms 5076 KB n=100
25 Correct 3 ms 5076 KB n=100
26 Correct 3 ms 5076 KB n=12
27 Correct 3 ms 5136 KB n=100
28 Correct 3 ms 5204 KB n=500
29 Correct 4 ms 5204 KB n=500
30 Correct 5 ms 5204 KB n=500
31 Correct 3 ms 5204 KB n=500
32 Correct 3 ms 5140 KB n=500
33 Correct 4 ms 5204 KB n=500
34 Correct 4 ms 5204 KB n=500
35 Correct 4 ms 5204 KB n=500
36 Correct 4 ms 5204 KB n=500
37 Correct 4 ms 5204 KB n=500
38 Correct 4 ms 5204 KB n=500
39 Correct 3 ms 5204 KB n=500
40 Correct 4 ms 5204 KB n=500
41 Correct 4 ms 5204 KB n=500
42 Correct 4 ms 5204 KB n=500
43 Correct 4 ms 5204 KB n=500
44 Correct 4 ms 5204 KB n=500
45 Correct 4 ms 5212 KB n=500
46 Correct 4 ms 5204 KB n=500
47 Correct 4 ms 5204 KB n=500
48 Correct 4 ms 5144 KB n=500
49 Correct 3 ms 5204 KB n=500
50 Correct 5 ms 5204 KB n=500
51 Correct 5 ms 5148 KB n=500
52 Correct 4 ms 5204 KB n=500
53 Correct 4 ms 5204 KB n=500
54 Correct 4 ms 5204 KB n=500
55 Correct 4 ms 5204 KB n=278
56 Correct 4 ms 5204 KB n=500
57 Correct 4 ms 5204 KB n=500
58 Correct 4 ms 5204 KB n=500
59 Correct 7 ms 5588 KB n=2000
60 Correct 7 ms 5844 KB n=2000
61 Correct 8 ms 5716 KB n=2000
62 Correct 7 ms 5624 KB n=2000
63 Correct 7 ms 5588 KB n=2000
64 Correct 8 ms 5656 KB n=2000
65 Correct 6 ms 5660 KB n=2000
66 Correct 6 ms 5716 KB n=2000
67 Correct 8 ms 5588 KB n=2000
68 Correct 7 ms 5588 KB n=2000
69 Correct 6 ms 5588 KB n=2000
70 Correct 7 ms 5664 KB n=2000
71 Correct 7 ms 5588 KB n=2000
72 Correct 6 ms 5588 KB n=2000
73 Correct 6 ms 5588 KB n=2000
74 Correct 6 ms 5588 KB n=1844
75 Correct 5 ms 5588 KB n=2000
76 Correct 7 ms 5588 KB n=2000
77 Correct 7 ms 5588 KB n=2000
78 Correct 6 ms 5660 KB n=2000
79 Correct 7 ms 5588 KB n=2000
80 Correct 7 ms 5660 KB n=2000
81 Correct 7 ms 5660 KB n=2000
82 Correct 7 ms 5588 KB n=2000
83 Correct 6 ms 5716 KB n=2000
84 Correct 7 ms 5672 KB n=2000
85 Correct 6 ms 5588 KB n=2000
86 Correct 7 ms 5588 KB n=2000
87 Correct 7 ms 5588 KB n=2000
88 Correct 7 ms 5716 KB n=2000
89 Correct 7 ms 5716 KB n=2000
90 Correct 7 ms 5844 KB n=2000
91 Correct 6 ms 5660 KB n=2000
92 Correct 1074 ms 61972 KB n=200000
93 Correct 1542 ms 66964 KB n=200000
94 Correct 1365 ms 70252 KB n=200000
95 Correct 1049 ms 61856 KB n=200000
96 Correct 980 ms 61760 KB n=200000
97 Correct 1496 ms 65944 KB n=200000
98 Correct 1037 ms 61888 KB n=200000
99 Correct 1307 ms 61936 KB n=200000
100 Correct 1008 ms 61780 KB n=200000
101 Correct 1343 ms 71484 KB n=200000
102 Correct 521 ms 62924 KB n=200000
103 Correct 539 ms 62992 KB n=200000
104 Correct 539 ms 62924 KB n=200000
105 Correct 486 ms 63444 KB n=200000
106 Correct 488 ms 63496 KB n=200000
107 Correct 491 ms 63432 KB n=200000
108 Correct 1368 ms 61936 KB n=200000
109 Correct 1289 ms 61880 KB n=200000
110 Correct 1405 ms 61868 KB n=200000
111 Correct 1121 ms 61236 KB n=200000
112 Correct 1651 ms 70460 KB n=200000
113 Correct 1653 ms 65924 KB n=200000
114 Correct 1076 ms 61272 KB n=200000
115 Correct 1762 ms 63852 KB n=200000
116 Correct 1088 ms 61956 KB n=200000
117 Correct 1434 ms 70800 KB n=200000
118 Correct 1690 ms 64788 KB n=200000
119 Correct 1132 ms 61936 KB n=200000
120 Correct 1458 ms 70444 KB n=200000
121 Correct 1451 ms 70396 KB n=200000
122 Correct 1475 ms 70676 KB n=200000
123 Correct 612 ms 63132 KB n=200000
124 Correct 356 ms 22888 KB n=25264