답안 #140749

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
140749 2019-08-04T23:49:30 Z rzbt Birthday gift (IZhO18_treearray) C++14
100 / 100
1771 ms 77560 KB
    #include <bits/stdc++.h>
    #define mp make_pair
    #define pb push_back
    #define F first
    #define S second
    #define all(x) x.begin(),x.end()
    #define MAXN 200005
     
    using namespace std;
     
    int n,m,q;
    vector<int> niz[MAXN];
    int predak[MAXN][21];
    int dubina[MAXN];
    int a[MAXN];
     
    set<int> s[MAXN],sp[MAXN];
     
    void dfs(int t,int o,int h){
        predak[t][0]=o;
        dubina[t]=h;
        for(auto x:niz[t])
            if(x!=o)
                dfs(x,t,h+1);
     
    }
     
    int lca(int a, int b){
        if(dubina[b]>dubina[a])swap(a,b);
        for(int j=20;j>=0;j--){
            if(dubina[predak[a][j]]>=dubina[b])a=predak[a][j];
        }
        if(a==b)return a;
        for(int j=20;j>=0;j--){
            if(predak[a][j]!=predak[b][j]){
                a=predak[a][j];
                b=predak[b][j];
            }
        }
        return predak[a][0];
     
    }
     
    int main()
    {
        scanf("%d %d %d", &n, &m, &q);
        for(int i=1;i<n;i++){
            int t1,t2;
            scanf("%d %d", &t1, &t2);
            niz[t1].pb(t2);
            niz[t2].pb(t1);
        }
        dfs(1,0,1);
        for(int j=1;j<21;j++)
            for(int i=1;i<=n;i++)
                predak[i][j]=predak[predak[i][j-1]][j-1];
        for(int i=1;i<=m;i++)
            scanf("%d",a+i);
        for(int i=1;i<m;i++)
            s[lca(a[i],a[i+1])].insert(i);
        for(int i=1;i<=m;i++)
            sp[a[i]].insert(i);
        while(q--){
            int qt; //FITTER HAPPIER MORE PRODUCTIVE
            scanf("%d", &qt);
            if(qt==1){
                int pos,x;
                scanf("%d %d", &pos,&x);
                if(pos!=1)s[lca(a[pos],a[pos-1])].erase(pos-1);
                if(pos!=m)s[lca(a[pos],a[pos+1])].erase(pos);
                sp[a[pos]].erase(pos);
                a[pos]=x;
                if(pos!=1)s[lca(a[pos],a[pos-1])].insert(pos-1);
                if(pos!=m)s[lca(a[pos],a[pos+1])].insert(pos);
                sp[a[pos]].insert(pos);
     
     
            }else if(qt==2){
     
                int l,r,x;
                scanf("%d %d %d", &l, &r, &x);
                //printf("    %d %d\n",sp[x].size(),s[x].size());
                int t1=0,t2=0;
                if(!sp[x].empty() && sp[x].lower_bound(l)!=sp[x].end())t1=*(sp[x].lower_bound(l));
                if(!s[x].empty() && s[x].lower_bound(l)!=s[x].end())t2=*(s[x].lower_bound(l));
                //printf("    %d %d    %d %d\n",sp[x].size(),s[x].size(),t1,t2);
                if(t1<=r && t1)printf("%d %d\n",t1,t1);
                else if(t2<r && t2)printf("%d %d\n",t2,t2+1);
                else printf("-1 -1\n");
            }
            else{
                int x,y;
                scanf("%d %d", &x, &y);
                printf("    %d\n",lca(a[x],a[y]));
            }
        }
     
     
     
     
     
        return 0;
    }

Compilation message

treearray.cpp: In function 'int main()':
treearray.cpp:46:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d %d", &n, &m, &q);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:49:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d %d", &t1, &t2);
             ~~~~~^~~~~~~~~~~~~~~~~~~
treearray.cpp:58:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d",a+i);
             ~~~~~^~~~~~~~~~
treearray.cpp:65:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d", &qt);
             ~~~~~^~~~~~~~~~~
treearray.cpp:68:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
                 scanf("%d %d", &pos,&x);
                 ~~~~~^~~~~~~~~~~~~~~~~~
treearray.cpp:81:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
                 scanf("%d %d %d", &l, &r, &x);
                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:93:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
                 scanf("%d %d", &x, &y);
                 ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 23800 KB n=5
2 Correct 23 ms 23800 KB n=100
3 Correct 23 ms 23800 KB n=100
4 Correct 22 ms 23800 KB n=100
5 Correct 23 ms 23800 KB n=100
6 Correct 27 ms 23800 KB n=100
7 Correct 23 ms 23800 KB n=100
8 Correct 23 ms 23800 KB n=100
9 Correct 22 ms 23800 KB n=100
10 Correct 23 ms 23928 KB n=100
11 Correct 23 ms 23800 KB n=100
12 Correct 23 ms 23928 KB n=100
13 Correct 24 ms 23776 KB n=100
14 Correct 28 ms 23800 KB n=100
15 Correct 27 ms 23800 KB n=100
16 Correct 23 ms 23800 KB n=100
17 Correct 27 ms 23800 KB n=100
18 Correct 25 ms 23928 KB n=100
19 Correct 22 ms 23800 KB n=100
20 Correct 22 ms 23800 KB n=100
21 Correct 24 ms 23900 KB n=100
22 Correct 23 ms 23800 KB n=100
23 Correct 23 ms 23820 KB n=100
24 Correct 22 ms 23800 KB n=100
25 Correct 29 ms 23800 KB n=100
26 Correct 22 ms 23800 KB n=12
27 Correct 22 ms 23800 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 23800 KB n=5
2 Correct 23 ms 23800 KB n=100
3 Correct 23 ms 23800 KB n=100
4 Correct 22 ms 23800 KB n=100
5 Correct 23 ms 23800 KB n=100
6 Correct 27 ms 23800 KB n=100
7 Correct 23 ms 23800 KB n=100
8 Correct 23 ms 23800 KB n=100
9 Correct 22 ms 23800 KB n=100
10 Correct 23 ms 23928 KB n=100
11 Correct 23 ms 23800 KB n=100
12 Correct 23 ms 23928 KB n=100
13 Correct 24 ms 23776 KB n=100
14 Correct 28 ms 23800 KB n=100
15 Correct 27 ms 23800 KB n=100
16 Correct 23 ms 23800 KB n=100
17 Correct 27 ms 23800 KB n=100
18 Correct 25 ms 23928 KB n=100
19 Correct 22 ms 23800 KB n=100
20 Correct 22 ms 23800 KB n=100
21 Correct 24 ms 23900 KB n=100
22 Correct 23 ms 23800 KB n=100
23 Correct 23 ms 23820 KB n=100
24 Correct 22 ms 23800 KB n=100
25 Correct 29 ms 23800 KB n=100
26 Correct 22 ms 23800 KB n=12
27 Correct 22 ms 23800 KB n=100
28 Correct 24 ms 23928 KB n=500
29 Correct 23 ms 23932 KB n=500
30 Correct 23 ms 23928 KB n=500
31 Correct 24 ms 23932 KB n=500
32 Correct 24 ms 23928 KB n=500
33 Correct 23 ms 23928 KB n=500
34 Correct 23 ms 23928 KB n=500
35 Correct 23 ms 23928 KB n=500
36 Correct 23 ms 23928 KB n=500
37 Correct 23 ms 23928 KB n=500
38 Correct 23 ms 23928 KB n=500
39 Correct 23 ms 23928 KB n=500
40 Correct 23 ms 23928 KB n=500
41 Correct 24 ms 24056 KB n=500
42 Correct 24 ms 23928 KB n=500
43 Correct 23 ms 23928 KB n=500
44 Correct 23 ms 23932 KB n=500
45 Correct 24 ms 23928 KB n=500
46 Correct 23 ms 23928 KB n=500
47 Correct 23 ms 23932 KB n=500
48 Correct 23 ms 23928 KB n=500
49 Correct 23 ms 23928 KB n=500
50 Correct 23 ms 23928 KB n=500
51 Correct 24 ms 23928 KB n=500
52 Correct 24 ms 23928 KB n=500
53 Correct 24 ms 23928 KB n=500
54 Correct 23 ms 23928 KB n=500
55 Correct 23 ms 23928 KB n=278
56 Correct 23 ms 23928 KB n=500
57 Correct 23 ms 23928 KB n=500
58 Correct 23 ms 23928 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 23800 KB n=5
2 Correct 23 ms 23800 KB n=100
3 Correct 23 ms 23800 KB n=100
4 Correct 22 ms 23800 KB n=100
5 Correct 23 ms 23800 KB n=100
6 Correct 27 ms 23800 KB n=100
7 Correct 23 ms 23800 KB n=100
8 Correct 23 ms 23800 KB n=100
9 Correct 22 ms 23800 KB n=100
10 Correct 23 ms 23928 KB n=100
11 Correct 23 ms 23800 KB n=100
12 Correct 23 ms 23928 KB n=100
13 Correct 24 ms 23776 KB n=100
14 Correct 28 ms 23800 KB n=100
15 Correct 27 ms 23800 KB n=100
16 Correct 23 ms 23800 KB n=100
17 Correct 27 ms 23800 KB n=100
18 Correct 25 ms 23928 KB n=100
19 Correct 22 ms 23800 KB n=100
20 Correct 22 ms 23800 KB n=100
21 Correct 24 ms 23900 KB n=100
22 Correct 23 ms 23800 KB n=100
23 Correct 23 ms 23820 KB n=100
24 Correct 22 ms 23800 KB n=100
25 Correct 29 ms 23800 KB n=100
26 Correct 22 ms 23800 KB n=12
27 Correct 22 ms 23800 KB n=100
28 Correct 24 ms 23928 KB n=500
29 Correct 23 ms 23932 KB n=500
30 Correct 23 ms 23928 KB n=500
31 Correct 24 ms 23932 KB n=500
32 Correct 24 ms 23928 KB n=500
33 Correct 23 ms 23928 KB n=500
34 Correct 23 ms 23928 KB n=500
35 Correct 23 ms 23928 KB n=500
36 Correct 23 ms 23928 KB n=500
37 Correct 23 ms 23928 KB n=500
38 Correct 23 ms 23928 KB n=500
39 Correct 23 ms 23928 KB n=500
40 Correct 23 ms 23928 KB n=500
41 Correct 24 ms 24056 KB n=500
42 Correct 24 ms 23928 KB n=500
43 Correct 23 ms 23928 KB n=500
44 Correct 23 ms 23932 KB n=500
45 Correct 24 ms 23928 KB n=500
46 Correct 23 ms 23928 KB n=500
47 Correct 23 ms 23932 KB n=500
48 Correct 23 ms 23928 KB n=500
49 Correct 23 ms 23928 KB n=500
50 Correct 23 ms 23928 KB n=500
51 Correct 24 ms 23928 KB n=500
52 Correct 24 ms 23928 KB n=500
53 Correct 24 ms 23928 KB n=500
54 Correct 23 ms 23928 KB n=500
55 Correct 23 ms 23928 KB n=278
56 Correct 23 ms 23928 KB n=500
57 Correct 23 ms 23928 KB n=500
58 Correct 23 ms 23928 KB n=500
59 Correct 27 ms 24440 KB n=2000
60 Correct 27 ms 24312 KB n=2000
61 Correct 27 ms 24312 KB n=2000
62 Correct 28 ms 24312 KB n=2000
63 Correct 27 ms 24312 KB n=2000
64 Correct 28 ms 24312 KB n=2000
65 Correct 28 ms 24184 KB n=2000
66 Correct 27 ms 24312 KB n=2000
67 Correct 28 ms 24312 KB n=2000
68 Correct 28 ms 24312 KB n=2000
69 Correct 26 ms 24312 KB n=2000
70 Correct 26 ms 24312 KB n=2000
71 Correct 26 ms 24312 KB n=2000
72 Correct 30 ms 24312 KB n=2000
73 Correct 26 ms 24316 KB n=2000
74 Correct 27 ms 24312 KB n=1844
75 Correct 26 ms 24184 KB n=2000
76 Correct 28 ms 24184 KB n=2000
77 Correct 27 ms 24184 KB n=2000
78 Correct 28 ms 24312 KB n=2000
79 Correct 31 ms 24284 KB n=2000
80 Correct 28 ms 24312 KB n=2000
81 Correct 28 ms 24312 KB n=2000
82 Correct 30 ms 24284 KB n=2000
83 Correct 33 ms 24312 KB n=2000
84 Correct 34 ms 24312 KB n=2000
85 Correct 28 ms 24312 KB n=2000
86 Correct 28 ms 24312 KB n=2000
87 Correct 28 ms 24312 KB n=2000
88 Correct 28 ms 24392 KB n=2000
89 Correct 27 ms 24312 KB n=2000
90 Correct 27 ms 24440 KB n=2000
91 Correct 26 ms 24284 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 23800 KB n=5
2 Correct 23 ms 23800 KB n=100
3 Correct 23 ms 23800 KB n=100
4 Correct 22 ms 23800 KB n=100
5 Correct 23 ms 23800 KB n=100
6 Correct 27 ms 23800 KB n=100
7 Correct 23 ms 23800 KB n=100
8 Correct 23 ms 23800 KB n=100
9 Correct 22 ms 23800 KB n=100
10 Correct 23 ms 23928 KB n=100
11 Correct 23 ms 23800 KB n=100
12 Correct 23 ms 23928 KB n=100
13 Correct 24 ms 23776 KB n=100
14 Correct 28 ms 23800 KB n=100
15 Correct 27 ms 23800 KB n=100
16 Correct 23 ms 23800 KB n=100
17 Correct 27 ms 23800 KB n=100
18 Correct 25 ms 23928 KB n=100
19 Correct 22 ms 23800 KB n=100
20 Correct 22 ms 23800 KB n=100
21 Correct 24 ms 23900 KB n=100
22 Correct 23 ms 23800 KB n=100
23 Correct 23 ms 23820 KB n=100
24 Correct 22 ms 23800 KB n=100
25 Correct 29 ms 23800 KB n=100
26 Correct 22 ms 23800 KB n=12
27 Correct 22 ms 23800 KB n=100
28 Correct 24 ms 23928 KB n=500
29 Correct 23 ms 23932 KB n=500
30 Correct 23 ms 23928 KB n=500
31 Correct 24 ms 23932 KB n=500
32 Correct 24 ms 23928 KB n=500
33 Correct 23 ms 23928 KB n=500
34 Correct 23 ms 23928 KB n=500
35 Correct 23 ms 23928 KB n=500
36 Correct 23 ms 23928 KB n=500
37 Correct 23 ms 23928 KB n=500
38 Correct 23 ms 23928 KB n=500
39 Correct 23 ms 23928 KB n=500
40 Correct 23 ms 23928 KB n=500
41 Correct 24 ms 24056 KB n=500
42 Correct 24 ms 23928 KB n=500
43 Correct 23 ms 23928 KB n=500
44 Correct 23 ms 23932 KB n=500
45 Correct 24 ms 23928 KB n=500
46 Correct 23 ms 23928 KB n=500
47 Correct 23 ms 23932 KB n=500
48 Correct 23 ms 23928 KB n=500
49 Correct 23 ms 23928 KB n=500
50 Correct 23 ms 23928 KB n=500
51 Correct 24 ms 23928 KB n=500
52 Correct 24 ms 23928 KB n=500
53 Correct 24 ms 23928 KB n=500
54 Correct 23 ms 23928 KB n=500
55 Correct 23 ms 23928 KB n=278
56 Correct 23 ms 23928 KB n=500
57 Correct 23 ms 23928 KB n=500
58 Correct 23 ms 23928 KB n=500
59 Correct 27 ms 24440 KB n=2000
60 Correct 27 ms 24312 KB n=2000
61 Correct 27 ms 24312 KB n=2000
62 Correct 28 ms 24312 KB n=2000
63 Correct 27 ms 24312 KB n=2000
64 Correct 28 ms 24312 KB n=2000
65 Correct 28 ms 24184 KB n=2000
66 Correct 27 ms 24312 KB n=2000
67 Correct 28 ms 24312 KB n=2000
68 Correct 28 ms 24312 KB n=2000
69 Correct 26 ms 24312 KB n=2000
70 Correct 26 ms 24312 KB n=2000
71 Correct 26 ms 24312 KB n=2000
72 Correct 30 ms 24312 KB n=2000
73 Correct 26 ms 24316 KB n=2000
74 Correct 27 ms 24312 KB n=1844
75 Correct 26 ms 24184 KB n=2000
76 Correct 28 ms 24184 KB n=2000
77 Correct 27 ms 24184 KB n=2000
78 Correct 28 ms 24312 KB n=2000
79 Correct 31 ms 24284 KB n=2000
80 Correct 28 ms 24312 KB n=2000
81 Correct 28 ms 24312 KB n=2000
82 Correct 30 ms 24284 KB n=2000
83 Correct 33 ms 24312 KB n=2000
84 Correct 34 ms 24312 KB n=2000
85 Correct 28 ms 24312 KB n=2000
86 Correct 28 ms 24312 KB n=2000
87 Correct 28 ms 24312 KB n=2000
88 Correct 28 ms 24392 KB n=2000
89 Correct 27 ms 24312 KB n=2000
90 Correct 27 ms 24440 KB n=2000
91 Correct 26 ms 24284 KB n=2000
92 Correct 1028 ms 68996 KB n=200000
93 Correct 1499 ms 72696 KB n=200000
94 Correct 1394 ms 76000 KB n=200000
95 Correct 953 ms 68972 KB n=200000
96 Correct 1031 ms 68860 KB n=200000
97 Correct 1750 ms 71952 KB n=200000
98 Correct 1101 ms 68900 KB n=200000
99 Correct 1206 ms 68472 KB n=200000
100 Correct 1048 ms 68928 KB n=200000
101 Correct 1425 ms 77292 KB n=200000
102 Correct 598 ms 69824 KB n=200000
103 Correct 573 ms 69648 KB n=200000
104 Correct 574 ms 69728 KB n=200000
105 Correct 643 ms 69440 KB n=200000
106 Correct 600 ms 69680 KB n=200000
107 Correct 573 ms 69624 KB n=200000
108 Correct 1077 ms 68600 KB n=200000
109 Correct 1119 ms 68488 KB n=200000
110 Correct 1143 ms 68600 KB n=200000
111 Correct 1036 ms 68896 KB n=200000
112 Correct 1455 ms 76152 KB n=200000
113 Correct 1470 ms 71692 KB n=200000
114 Correct 1040 ms 68840 KB n=200000
115 Correct 1518 ms 69704 KB n=200000
116 Correct 926 ms 68772 KB n=200000
117 Correct 1413 ms 76508 KB n=200000
118 Correct 1771 ms 70648 KB n=200000
119 Correct 1004 ms 68584 KB n=200000
120 Correct 1381 ms 77048 KB n=200000
121 Correct 1365 ms 77020 KB n=200000
122 Correct 1372 ms 77560 KB n=200000
123 Correct 569 ms 69240 KB n=200000
124 Correct 321 ms 36940 KB n=25264