답안 #422237

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
422237 2021-06-09T22:44:42 Z Runtime_error_ Birthday gift (IZhO18_treearray) C++14
100 / 100
1822 ms 83372 KB
#include <bits/stdc++.h>
#define pb push_back
#define pi pair<int,int>
using namespace std;
const int inf = 2e5+9,lg = 19;
int n,m,q,a[inf],par[lg][inf],level[inf];
vector<int> adj[inf];
set<int> ans[inf],idx[inf];

void dfs(int u,int p){
    level[u] = level[p]+1;
    par[0][u] = p;
    for(int j=1;j<lg;j++)
        par[j][u] = par[j-1][ par[j-1][u] ];
    for(auto v:adj[u])
        if(v != p)
            dfs(v,u);
}

int LCA(int u,int v){
    if(level[v] < level[u])
        swap(u,v);
    int d = level[v] - level[u];
    for(int j=lg-1;j>=0;j--)
        if(d & (1<<j))
            v = par[j][v];
    if(u == v)return u;
    for(int j=lg-1;j>=0;j--)
        if(par[j][v] != par[j][u])
            v = par[j][v],u = par[j][u];
    return (u == v?v:par[0][v]);
}

int main(){

    scanf("%d%d%d",&n,&m,&q);
    for(int i=1;i<n;i++){
        int u,v;
        scanf("%d%d",&u,&v);
        adj[u].pb(v);
        adj[v].pb(u);
    }
    dfs(1,0);

    for(int i=1;i<=m;i++){
        scanf("%d",a+i);
        if(i>1)
            ans[ LCA(a[i-1],a[i]) ].insert(i-1);
        idx[a[i]].insert(i);
    }

     while(q--){
        int type,i,v,l,r;
        scanf("%d",&type);
        if(type == 1){
            scanf("%d%d",&i,&v);
            if(i<m){
                ans[ LCA(a[i],a[i+1]) ].erase(i);
                ans[ LCA(v,a[i+1]) ].insert(i);
            }
            if(i>1){
                ans[ LCA(a[i-1],a[i]) ].erase(i-1);
                ans[ LCA(a[i-1],v) ].insert(i-1);
            }
            idx[a[i]].erase(i);
            a[i] = v;
            idx[a[i]].insert(i);
        }
        else {
            scanf("%d%d%d",&l,&r,&v);
            set<int> ::iterator it;
            if(idx[v].size() > 0 && *idx[v].rbegin() >= l){
                it = idx[v].lower_bound(l);
                if(*it <= r){
                    printf("%d %d\n",*it,*it);
                    continue;
                }
            }
            if(ans[v].size() >0 && *ans[v].rbegin() >=l){
                it = ans[v].lower_bound(l);
                if(*it < r){
                    printf("%d %d\n",*it,*it+1);
                    continue;
                }
            }
            puts("-1 -1");
        }
    }
}
/*
5 4 4
1 2
3 1
3 4
5 3
4 5 2 3
2 1 3 1
1 3 5
2 3 4 5
2 1 3 1
*/

Compilation message

treearray.cpp: In function 'int main()':
treearray.cpp:37:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |     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",&u,&v);
      |         ~~~~~^~~~~~~~~~~~~~
treearray.cpp:47:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |         scanf("%d",a+i);
      |         ~~~~~^~~~~~~~~~
treearray.cpp:55:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   55 |         scanf("%d",&type);
      |         ~~~~~^~~~~~~~~~~~
treearray.cpp:57:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |             scanf("%d%d",&i,&v);
      |             ~~~~~^~~~~~~~~~~~~~
treearray.cpp:71:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |             scanf("%d%d%d",&l,&r,&v);
      |             ~~~~~^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23884 KB n=5
2 Correct 13 ms 23884 KB n=100
3 Correct 13 ms 23884 KB n=100
4 Correct 13 ms 23884 KB n=100
5 Correct 13 ms 23920 KB n=100
6 Correct 14 ms 23920 KB n=100
7 Correct 13 ms 23884 KB n=100
8 Correct 13 ms 23884 KB n=100
9 Correct 13 ms 23888 KB n=100
10 Correct 13 ms 23920 KB n=100
11 Correct 13 ms 23916 KB n=100
12 Correct 13 ms 23884 KB n=100
13 Correct 13 ms 23900 KB n=100
14 Correct 13 ms 23932 KB n=100
15 Correct 13 ms 23920 KB n=100
16 Correct 14 ms 23920 KB n=100
17 Correct 13 ms 23884 KB n=100
18 Correct 13 ms 23884 KB n=100
19 Correct 15 ms 23924 KB n=100
20 Correct 13 ms 23908 KB n=100
21 Correct 13 ms 23884 KB n=100
22 Correct 16 ms 23884 KB n=100
23 Correct 13 ms 23916 KB n=100
24 Correct 13 ms 23884 KB n=100
25 Correct 13 ms 23884 KB n=100
26 Correct 13 ms 23900 KB n=12
27 Correct 14 ms 23924 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23884 KB n=5
2 Correct 13 ms 23884 KB n=100
3 Correct 13 ms 23884 KB n=100
4 Correct 13 ms 23884 KB n=100
5 Correct 13 ms 23920 KB n=100
6 Correct 14 ms 23920 KB n=100
7 Correct 13 ms 23884 KB n=100
8 Correct 13 ms 23884 KB n=100
9 Correct 13 ms 23888 KB n=100
10 Correct 13 ms 23920 KB n=100
11 Correct 13 ms 23916 KB n=100
12 Correct 13 ms 23884 KB n=100
13 Correct 13 ms 23900 KB n=100
14 Correct 13 ms 23932 KB n=100
15 Correct 13 ms 23920 KB n=100
16 Correct 14 ms 23920 KB n=100
17 Correct 13 ms 23884 KB n=100
18 Correct 13 ms 23884 KB n=100
19 Correct 15 ms 23924 KB n=100
20 Correct 13 ms 23908 KB n=100
21 Correct 13 ms 23884 KB n=100
22 Correct 16 ms 23884 KB n=100
23 Correct 13 ms 23916 KB n=100
24 Correct 13 ms 23884 KB n=100
25 Correct 13 ms 23884 KB n=100
26 Correct 13 ms 23900 KB n=12
27 Correct 14 ms 23924 KB n=100
28 Correct 16 ms 24012 KB n=500
29 Correct 16 ms 23984 KB n=500
30 Correct 14 ms 24016 KB n=500
31 Correct 14 ms 24012 KB n=500
32 Correct 16 ms 23920 KB n=500
33 Correct 14 ms 23908 KB n=500
34 Correct 14 ms 23948 KB n=500
35 Correct 15 ms 24040 KB n=500
36 Correct 14 ms 23944 KB n=500
37 Correct 15 ms 23928 KB n=500
38 Correct 15 ms 24012 KB n=500
39 Correct 14 ms 24012 KB n=500
40 Correct 14 ms 23932 KB n=500
41 Correct 14 ms 23996 KB n=500
42 Correct 15 ms 23928 KB n=500
43 Correct 14 ms 23932 KB n=500
44 Correct 14 ms 24012 KB n=500
45 Correct 14 ms 23960 KB n=500
46 Correct 14 ms 23932 KB n=500
47 Correct 14 ms 24012 KB n=500
48 Correct 14 ms 24020 KB n=500
49 Correct 14 ms 24044 KB n=500
50 Correct 14 ms 24012 KB n=500
51 Correct 15 ms 23936 KB n=500
52 Correct 15 ms 24012 KB n=500
53 Correct 14 ms 24012 KB n=500
54 Correct 14 ms 23948 KB n=500
55 Correct 13 ms 23884 KB n=278
56 Correct 15 ms 24008 KB n=500
57 Correct 14 ms 23928 KB n=500
58 Correct 14 ms 24012 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23884 KB n=5
2 Correct 13 ms 23884 KB n=100
3 Correct 13 ms 23884 KB n=100
4 Correct 13 ms 23884 KB n=100
5 Correct 13 ms 23920 KB n=100
6 Correct 14 ms 23920 KB n=100
7 Correct 13 ms 23884 KB n=100
8 Correct 13 ms 23884 KB n=100
9 Correct 13 ms 23888 KB n=100
10 Correct 13 ms 23920 KB n=100
11 Correct 13 ms 23916 KB n=100
12 Correct 13 ms 23884 KB n=100
13 Correct 13 ms 23900 KB n=100
14 Correct 13 ms 23932 KB n=100
15 Correct 13 ms 23920 KB n=100
16 Correct 14 ms 23920 KB n=100
17 Correct 13 ms 23884 KB n=100
18 Correct 13 ms 23884 KB n=100
19 Correct 15 ms 23924 KB n=100
20 Correct 13 ms 23908 KB n=100
21 Correct 13 ms 23884 KB n=100
22 Correct 16 ms 23884 KB n=100
23 Correct 13 ms 23916 KB n=100
24 Correct 13 ms 23884 KB n=100
25 Correct 13 ms 23884 KB n=100
26 Correct 13 ms 23900 KB n=12
27 Correct 14 ms 23924 KB n=100
28 Correct 16 ms 24012 KB n=500
29 Correct 16 ms 23984 KB n=500
30 Correct 14 ms 24016 KB n=500
31 Correct 14 ms 24012 KB n=500
32 Correct 16 ms 23920 KB n=500
33 Correct 14 ms 23908 KB n=500
34 Correct 14 ms 23948 KB n=500
35 Correct 15 ms 24040 KB n=500
36 Correct 14 ms 23944 KB n=500
37 Correct 15 ms 23928 KB n=500
38 Correct 15 ms 24012 KB n=500
39 Correct 14 ms 24012 KB n=500
40 Correct 14 ms 23932 KB n=500
41 Correct 14 ms 23996 KB n=500
42 Correct 15 ms 23928 KB n=500
43 Correct 14 ms 23932 KB n=500
44 Correct 14 ms 24012 KB n=500
45 Correct 14 ms 23960 KB n=500
46 Correct 14 ms 23932 KB n=500
47 Correct 14 ms 24012 KB n=500
48 Correct 14 ms 24020 KB n=500
49 Correct 14 ms 24044 KB n=500
50 Correct 14 ms 24012 KB n=500
51 Correct 15 ms 23936 KB n=500
52 Correct 15 ms 24012 KB n=500
53 Correct 14 ms 24012 KB n=500
54 Correct 14 ms 23948 KB n=500
55 Correct 13 ms 23884 KB n=278
56 Correct 15 ms 24008 KB n=500
57 Correct 14 ms 23928 KB n=500
58 Correct 14 ms 24012 KB n=500
59 Correct 17 ms 24312 KB n=2000
60 Correct 17 ms 24396 KB n=2000
61 Correct 17 ms 24396 KB n=2000
62 Correct 18 ms 24392 KB n=2000
63 Correct 18 ms 24316 KB n=2000
64 Correct 17 ms 24412 KB n=2000
65 Correct 18 ms 24384 KB n=2000
66 Correct 19 ms 24396 KB n=2000
67 Correct 22 ms 24268 KB n=2000
68 Correct 17 ms 24412 KB n=2000
69 Correct 17 ms 24308 KB n=2000
70 Correct 16 ms 24268 KB n=2000
71 Correct 17 ms 24316 KB n=2000
72 Correct 16 ms 24268 KB n=2000
73 Correct 16 ms 24268 KB n=2000
74 Correct 21 ms 24224 KB n=1844
75 Correct 17 ms 24264 KB n=2000
76 Correct 19 ms 24268 KB n=2000
77 Correct 18 ms 24268 KB n=2000
78 Correct 18 ms 24312 KB n=2000
79 Correct 17 ms 24268 KB n=2000
80 Correct 17 ms 24444 KB n=2000
81 Correct 19 ms 24412 KB n=2000
82 Correct 21 ms 24268 KB n=2000
83 Correct 18 ms 24428 KB n=2000
84 Correct 18 ms 24284 KB n=2000
85 Correct 19 ms 24312 KB n=2000
86 Correct 18 ms 24316 KB n=2000
87 Correct 21 ms 24268 KB n=2000
88 Correct 19 ms 24460 KB n=2000
89 Correct 17 ms 24388 KB n=2000
90 Correct 17 ms 24416 KB n=2000
91 Correct 17 ms 24312 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23884 KB n=5
2 Correct 13 ms 23884 KB n=100
3 Correct 13 ms 23884 KB n=100
4 Correct 13 ms 23884 KB n=100
5 Correct 13 ms 23920 KB n=100
6 Correct 14 ms 23920 KB n=100
7 Correct 13 ms 23884 KB n=100
8 Correct 13 ms 23884 KB n=100
9 Correct 13 ms 23888 KB n=100
10 Correct 13 ms 23920 KB n=100
11 Correct 13 ms 23916 KB n=100
12 Correct 13 ms 23884 KB n=100
13 Correct 13 ms 23900 KB n=100
14 Correct 13 ms 23932 KB n=100
15 Correct 13 ms 23920 KB n=100
16 Correct 14 ms 23920 KB n=100
17 Correct 13 ms 23884 KB n=100
18 Correct 13 ms 23884 KB n=100
19 Correct 15 ms 23924 KB n=100
20 Correct 13 ms 23908 KB n=100
21 Correct 13 ms 23884 KB n=100
22 Correct 16 ms 23884 KB n=100
23 Correct 13 ms 23916 KB n=100
24 Correct 13 ms 23884 KB n=100
25 Correct 13 ms 23884 KB n=100
26 Correct 13 ms 23900 KB n=12
27 Correct 14 ms 23924 KB n=100
28 Correct 16 ms 24012 KB n=500
29 Correct 16 ms 23984 KB n=500
30 Correct 14 ms 24016 KB n=500
31 Correct 14 ms 24012 KB n=500
32 Correct 16 ms 23920 KB n=500
33 Correct 14 ms 23908 KB n=500
34 Correct 14 ms 23948 KB n=500
35 Correct 15 ms 24040 KB n=500
36 Correct 14 ms 23944 KB n=500
37 Correct 15 ms 23928 KB n=500
38 Correct 15 ms 24012 KB n=500
39 Correct 14 ms 24012 KB n=500
40 Correct 14 ms 23932 KB n=500
41 Correct 14 ms 23996 KB n=500
42 Correct 15 ms 23928 KB n=500
43 Correct 14 ms 23932 KB n=500
44 Correct 14 ms 24012 KB n=500
45 Correct 14 ms 23960 KB n=500
46 Correct 14 ms 23932 KB n=500
47 Correct 14 ms 24012 KB n=500
48 Correct 14 ms 24020 KB n=500
49 Correct 14 ms 24044 KB n=500
50 Correct 14 ms 24012 KB n=500
51 Correct 15 ms 23936 KB n=500
52 Correct 15 ms 24012 KB n=500
53 Correct 14 ms 24012 KB n=500
54 Correct 14 ms 23948 KB n=500
55 Correct 13 ms 23884 KB n=278
56 Correct 15 ms 24008 KB n=500
57 Correct 14 ms 23928 KB n=500
58 Correct 14 ms 24012 KB n=500
59 Correct 17 ms 24312 KB n=2000
60 Correct 17 ms 24396 KB n=2000
61 Correct 17 ms 24396 KB n=2000
62 Correct 18 ms 24392 KB n=2000
63 Correct 18 ms 24316 KB n=2000
64 Correct 17 ms 24412 KB n=2000
65 Correct 18 ms 24384 KB n=2000
66 Correct 19 ms 24396 KB n=2000
67 Correct 22 ms 24268 KB n=2000
68 Correct 17 ms 24412 KB n=2000
69 Correct 17 ms 24308 KB n=2000
70 Correct 16 ms 24268 KB n=2000
71 Correct 17 ms 24316 KB n=2000
72 Correct 16 ms 24268 KB n=2000
73 Correct 16 ms 24268 KB n=2000
74 Correct 21 ms 24224 KB n=1844
75 Correct 17 ms 24264 KB n=2000
76 Correct 19 ms 24268 KB n=2000
77 Correct 18 ms 24268 KB n=2000
78 Correct 18 ms 24312 KB n=2000
79 Correct 17 ms 24268 KB n=2000
80 Correct 17 ms 24444 KB n=2000
81 Correct 19 ms 24412 KB n=2000
82 Correct 21 ms 24268 KB n=2000
83 Correct 18 ms 24428 KB n=2000
84 Correct 18 ms 24284 KB n=2000
85 Correct 19 ms 24312 KB n=2000
86 Correct 18 ms 24316 KB n=2000
87 Correct 21 ms 24268 KB n=2000
88 Correct 19 ms 24460 KB n=2000
89 Correct 17 ms 24388 KB n=2000
90 Correct 17 ms 24416 KB n=2000
91 Correct 17 ms 24312 KB n=2000
92 Correct 1731 ms 73988 KB n=200000
93 Correct 1322 ms 78572 KB n=200000
94 Correct 1061 ms 81988 KB n=200000
95 Correct 1719 ms 73536 KB n=200000
96 Correct 1572 ms 73592 KB n=200000
97 Correct 1427 ms 77704 KB n=200000
98 Correct 1630 ms 73664 KB n=200000
99 Correct 1804 ms 73792 KB n=200000
100 Correct 1648 ms 73580 KB n=200000
101 Correct 1045 ms 83372 KB n=200000
102 Correct 807 ms 74680 KB n=200000
103 Correct 810 ms 74772 KB n=200000
104 Correct 758 ms 74772 KB n=200000
105 Correct 842 ms 75100 KB n=200000
106 Correct 816 ms 75164 KB n=200000
107 Correct 823 ms 75176 KB n=200000
108 Correct 1790 ms 73644 KB n=200000
109 Correct 1760 ms 73608 KB n=200000
110 Correct 1822 ms 73648 KB n=200000
111 Correct 1610 ms 73152 KB n=200000
112 Correct 1083 ms 82128 KB n=200000
113 Correct 1485 ms 77692 KB n=200000
114 Correct 1572 ms 72996 KB n=200000
115 Correct 1774 ms 75724 KB n=200000
116 Correct 1715 ms 73748 KB n=200000
117 Correct 1020 ms 82488 KB n=200000
118 Correct 1627 ms 76552 KB n=200000
119 Correct 1760 ms 73712 KB n=200000
120 Correct 973 ms 82244 KB n=200000
121 Correct 980 ms 82236 KB n=200000
122 Correct 981 ms 82508 KB n=200000
123 Correct 888 ms 74920 KB n=200000
124 Correct 261 ms 38636 KB n=25264