답안 #554914

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
554914 2022-04-29T15:16:46 Z shahriarkhan Birthday gift (IZhO18_treearray) C++14
100 / 100
1030 ms 89612 KB
#include<bits/stdc++.h>
using namespace std ;

const int MX = 2e5 + 5 , LG = 25 ;

int up[MX][LG] , tin[MX] , tout[MX] , timer ;

vector<int> adj[MX] ;

set<int> S[MX] , V[MX] ;

void dfs(int s , int p)
{
    tin[s] = ++timer ;
    up[s][0] = p ;
    for(int i = 1 ; i < LG ; ++i)
    {
        up[s][i] = up[up[s][i-1]][i-1] ;
    }
    for(int t : adj[s])
    {
        if(t==p) continue ;
        dfs(t,s) ;
    }
    tout[s] = ++timer ;
}

int is_ancestor(int u , int v)
{
    return ((tin[u]<=tin[v]) && (tout[u]>=tout[v])) ;
}

int find_lca(int u , int v)
{
    if(is_ancestor(u,v)) return u ;
    if(is_ancestor(v,u)) return v ;
    for(int i = LG - 1 ; i >= 0 ; --i)
    {
        if(!is_ancestor(up[u][i],v))
        {
            u = up[u][i] ;
        }
    }
    return up[u][0] ;
}

int main()
{
    int n , m , q ;
    scanf("%d%d%d",&n,&m,&q) ;
    for(int i = 1 ; i < n ; ++i)
    {
        int a , b ;
        scanf("%d%d",&a,&b) ;
        adj[a].push_back(b) ;
        adj[b].push_back(a) ;
    }
    int a[m+2] , b[m+2] ;
    for(int i = 1 ; i <= m ; ++i)
    {
        scanf("%d",&a[i]) ;
        V[a[i]].insert(i) ;
    }
    dfs(1,1) ;
    for(int i = 1 ; i < m ; ++i)
    {
        b[i] = find_lca(a[i],a[i+1]) ;
        S[b[i]].insert(i) ;
    }
    while(q--)
    {
        int type ;
        scanf("%d",&type) ;
        if(type==1)
        {
            int pos , v ;
            scanf("%d%d",&pos,&v) ;
            V[a[pos]].erase(pos) ;
            a[pos] = v ;
            V[a[pos]].insert(pos) ;
            if(pos!=m)
            {
                S[b[pos]].erase(pos) ;
                b[pos] = find_lca(a[pos],a[pos+1]) ;
                S[b[pos]].insert(pos) ;
            }
            if(pos!=1)
            {
                S[b[pos-1]].erase(pos-1) ;
                b[pos-1] = find_lca(a[pos-1],a[pos]) ;
                S[b[pos-1]].insert(pos-1) ;
            }
        }
        else
        {
            int l , r , v ;
            scanf("%d%d%d",&l,&r,&v) ;
            int L = -1 , R = -1 ;
            if(!S[v].empty())
            {
                auto it = S[v].lower_bound(l) ;
                if(it!=S[v].end())
                {
                    int cr = *it ;
                    if(cr<r) L = cr , R = cr + 1 ;
                }
            }
            if(!V[v].empty())
            {
                auto it = V[v].lower_bound(l) ;
                if(it!=V[v].end())
                {
                    int cr = *it ;
                    if(cr<=r) L = cr , R = cr ;
                }
            }
            printf("%d %d\n",L,R) ;
        }
    }
    return 0 ;
}

Compilation message

treearray.cpp: In function 'int main()':
treearray.cpp:50:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   50 |     scanf("%d%d%d",&n,&m,&q) ;
      |     ~~~~~^~~~~~~~~~~~~~~~~~~
treearray.cpp:54:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   54 |         scanf("%d%d",&a,&b) ;
      |         ~~~~~^~~~~~~~~~~~~~
treearray.cpp:61:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |         scanf("%d",&a[i]) ;
      |         ~~~~~^~~~~~~~~~~~
treearray.cpp:73:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |         scanf("%d",&type) ;
      |         ~~~~~^~~~~~~~~~~~
treearray.cpp:77:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   77 |             scanf("%d%d",&pos,&v) ;
      |             ~~~~~^~~~~~~~~~~~~~~~
treearray.cpp:97:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   97 |             scanf("%d%d%d",&l,&r,&v) ;
      |             ~~~~~^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB n=5
2 Correct 12 ms 23796 KB n=100
3 Correct 12 ms 23764 KB n=100
4 Correct 12 ms 23716 KB n=100
5 Correct 13 ms 23928 KB n=100
6 Correct 12 ms 23764 KB n=100
7 Correct 12 ms 23728 KB n=100
8 Correct 12 ms 23792 KB n=100
9 Correct 12 ms 23728 KB n=100
10 Correct 13 ms 23764 KB n=100
11 Correct 12 ms 23796 KB n=100
12 Correct 12 ms 23796 KB n=100
13 Correct 12 ms 23764 KB n=100
14 Correct 11 ms 23764 KB n=100
15 Correct 13 ms 23764 KB n=100
16 Correct 12 ms 23756 KB n=100
17 Correct 13 ms 23764 KB n=100
18 Correct 12 ms 23792 KB n=100
19 Correct 12 ms 23712 KB n=100
20 Correct 12 ms 23764 KB n=100
21 Correct 12 ms 23736 KB n=100
22 Correct 12 ms 23708 KB n=100
23 Correct 12 ms 23792 KB n=100
24 Correct 15 ms 23764 KB n=100
25 Correct 12 ms 23816 KB n=100
26 Correct 12 ms 23788 KB n=12
27 Correct 12 ms 23756 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB n=5
2 Correct 12 ms 23796 KB n=100
3 Correct 12 ms 23764 KB n=100
4 Correct 12 ms 23716 KB n=100
5 Correct 13 ms 23928 KB n=100
6 Correct 12 ms 23764 KB n=100
7 Correct 12 ms 23728 KB n=100
8 Correct 12 ms 23792 KB n=100
9 Correct 12 ms 23728 KB n=100
10 Correct 13 ms 23764 KB n=100
11 Correct 12 ms 23796 KB n=100
12 Correct 12 ms 23796 KB n=100
13 Correct 12 ms 23764 KB n=100
14 Correct 11 ms 23764 KB n=100
15 Correct 13 ms 23764 KB n=100
16 Correct 12 ms 23756 KB n=100
17 Correct 13 ms 23764 KB n=100
18 Correct 12 ms 23792 KB n=100
19 Correct 12 ms 23712 KB n=100
20 Correct 12 ms 23764 KB n=100
21 Correct 12 ms 23736 KB n=100
22 Correct 12 ms 23708 KB n=100
23 Correct 12 ms 23792 KB n=100
24 Correct 15 ms 23764 KB n=100
25 Correct 12 ms 23816 KB n=100
26 Correct 12 ms 23788 KB n=12
27 Correct 12 ms 23756 KB n=100
28 Correct 14 ms 23892 KB n=500
29 Correct 13 ms 23928 KB n=500
30 Correct 13 ms 23892 KB n=500
31 Correct 13 ms 23892 KB n=500
32 Correct 13 ms 23872 KB n=500
33 Correct 13 ms 23828 KB n=500
34 Correct 13 ms 23892 KB n=500
35 Correct 12 ms 23892 KB n=500
36 Correct 13 ms 23928 KB n=500
37 Correct 14 ms 24020 KB n=500
38 Correct 13 ms 23892 KB n=500
39 Correct 13 ms 23892 KB n=500
40 Correct 12 ms 23892 KB n=500
41 Correct 13 ms 23892 KB n=500
42 Correct 13 ms 23936 KB n=500
43 Correct 13 ms 23884 KB n=500
44 Correct 13 ms 23892 KB n=500
45 Correct 14 ms 23932 KB n=500
46 Correct 15 ms 23928 KB n=500
47 Correct 13 ms 23892 KB n=500
48 Correct 15 ms 23924 KB n=500
49 Correct 13 ms 23892 KB n=500
50 Correct 13 ms 23892 KB n=500
51 Correct 13 ms 23892 KB n=500
52 Correct 13 ms 23892 KB n=500
53 Correct 13 ms 23860 KB n=500
54 Correct 13 ms 23924 KB n=500
55 Correct 13 ms 23892 KB n=278
56 Correct 13 ms 23896 KB n=500
57 Correct 13 ms 23892 KB n=500
58 Correct 13 ms 23932 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB n=5
2 Correct 12 ms 23796 KB n=100
3 Correct 12 ms 23764 KB n=100
4 Correct 12 ms 23716 KB n=100
5 Correct 13 ms 23928 KB n=100
6 Correct 12 ms 23764 KB n=100
7 Correct 12 ms 23728 KB n=100
8 Correct 12 ms 23792 KB n=100
9 Correct 12 ms 23728 KB n=100
10 Correct 13 ms 23764 KB n=100
11 Correct 12 ms 23796 KB n=100
12 Correct 12 ms 23796 KB n=100
13 Correct 12 ms 23764 KB n=100
14 Correct 11 ms 23764 KB n=100
15 Correct 13 ms 23764 KB n=100
16 Correct 12 ms 23756 KB n=100
17 Correct 13 ms 23764 KB n=100
18 Correct 12 ms 23792 KB n=100
19 Correct 12 ms 23712 KB n=100
20 Correct 12 ms 23764 KB n=100
21 Correct 12 ms 23736 KB n=100
22 Correct 12 ms 23708 KB n=100
23 Correct 12 ms 23792 KB n=100
24 Correct 15 ms 23764 KB n=100
25 Correct 12 ms 23816 KB n=100
26 Correct 12 ms 23788 KB n=12
27 Correct 12 ms 23756 KB n=100
28 Correct 14 ms 23892 KB n=500
29 Correct 13 ms 23928 KB n=500
30 Correct 13 ms 23892 KB n=500
31 Correct 13 ms 23892 KB n=500
32 Correct 13 ms 23872 KB n=500
33 Correct 13 ms 23828 KB n=500
34 Correct 13 ms 23892 KB n=500
35 Correct 12 ms 23892 KB n=500
36 Correct 13 ms 23928 KB n=500
37 Correct 14 ms 24020 KB n=500
38 Correct 13 ms 23892 KB n=500
39 Correct 13 ms 23892 KB n=500
40 Correct 12 ms 23892 KB n=500
41 Correct 13 ms 23892 KB n=500
42 Correct 13 ms 23936 KB n=500
43 Correct 13 ms 23884 KB n=500
44 Correct 13 ms 23892 KB n=500
45 Correct 14 ms 23932 KB n=500
46 Correct 15 ms 23928 KB n=500
47 Correct 13 ms 23892 KB n=500
48 Correct 15 ms 23924 KB n=500
49 Correct 13 ms 23892 KB n=500
50 Correct 13 ms 23892 KB n=500
51 Correct 13 ms 23892 KB n=500
52 Correct 13 ms 23892 KB n=500
53 Correct 13 ms 23860 KB n=500
54 Correct 13 ms 23924 KB n=500
55 Correct 13 ms 23892 KB n=278
56 Correct 13 ms 23896 KB n=500
57 Correct 13 ms 23892 KB n=500
58 Correct 13 ms 23932 KB n=500
59 Correct 16 ms 24316 KB n=2000
60 Correct 15 ms 24404 KB n=2000
61 Correct 15 ms 24324 KB n=2000
62 Correct 15 ms 24320 KB n=2000
63 Correct 16 ms 24276 KB n=2000
64 Correct 16 ms 24316 KB n=2000
65 Correct 16 ms 24276 KB n=2000
66 Correct 16 ms 24284 KB n=2000
67 Correct 17 ms 24240 KB n=2000
68 Correct 20 ms 24392 KB n=2000
69 Correct 15 ms 24276 KB n=2000
70 Correct 16 ms 24396 KB n=2000
71 Correct 15 ms 24412 KB n=2000
72 Correct 16 ms 24276 KB n=2000
73 Correct 16 ms 24276 KB n=2000
74 Correct 16 ms 24316 KB n=1844
75 Correct 16 ms 24276 KB n=2000
76 Correct 16 ms 24320 KB n=2000
77 Correct 17 ms 24316 KB n=2000
78 Correct 17 ms 24276 KB n=2000
79 Correct 16 ms 24276 KB n=2000
80 Correct 15 ms 24316 KB n=2000
81 Correct 17 ms 24284 KB n=2000
82 Correct 18 ms 24276 KB n=2000
83 Correct 15 ms 24284 KB n=2000
84 Correct 17 ms 24396 KB n=2000
85 Correct 16 ms 24276 KB n=2000
86 Correct 16 ms 24404 KB n=2000
87 Correct 15 ms 24288 KB n=2000
88 Correct 14 ms 24396 KB n=2000
89 Correct 15 ms 24404 KB n=2000
90 Correct 14 ms 24404 KB n=2000
91 Correct 16 ms 24308 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB n=5
2 Correct 12 ms 23796 KB n=100
3 Correct 12 ms 23764 KB n=100
4 Correct 12 ms 23716 KB n=100
5 Correct 13 ms 23928 KB n=100
6 Correct 12 ms 23764 KB n=100
7 Correct 12 ms 23728 KB n=100
8 Correct 12 ms 23792 KB n=100
9 Correct 12 ms 23728 KB n=100
10 Correct 13 ms 23764 KB n=100
11 Correct 12 ms 23796 KB n=100
12 Correct 12 ms 23796 KB n=100
13 Correct 12 ms 23764 KB n=100
14 Correct 11 ms 23764 KB n=100
15 Correct 13 ms 23764 KB n=100
16 Correct 12 ms 23756 KB n=100
17 Correct 13 ms 23764 KB n=100
18 Correct 12 ms 23792 KB n=100
19 Correct 12 ms 23712 KB n=100
20 Correct 12 ms 23764 KB n=100
21 Correct 12 ms 23736 KB n=100
22 Correct 12 ms 23708 KB n=100
23 Correct 12 ms 23792 KB n=100
24 Correct 15 ms 23764 KB n=100
25 Correct 12 ms 23816 KB n=100
26 Correct 12 ms 23788 KB n=12
27 Correct 12 ms 23756 KB n=100
28 Correct 14 ms 23892 KB n=500
29 Correct 13 ms 23928 KB n=500
30 Correct 13 ms 23892 KB n=500
31 Correct 13 ms 23892 KB n=500
32 Correct 13 ms 23872 KB n=500
33 Correct 13 ms 23828 KB n=500
34 Correct 13 ms 23892 KB n=500
35 Correct 12 ms 23892 KB n=500
36 Correct 13 ms 23928 KB n=500
37 Correct 14 ms 24020 KB n=500
38 Correct 13 ms 23892 KB n=500
39 Correct 13 ms 23892 KB n=500
40 Correct 12 ms 23892 KB n=500
41 Correct 13 ms 23892 KB n=500
42 Correct 13 ms 23936 KB n=500
43 Correct 13 ms 23884 KB n=500
44 Correct 13 ms 23892 KB n=500
45 Correct 14 ms 23932 KB n=500
46 Correct 15 ms 23928 KB n=500
47 Correct 13 ms 23892 KB n=500
48 Correct 15 ms 23924 KB n=500
49 Correct 13 ms 23892 KB n=500
50 Correct 13 ms 23892 KB n=500
51 Correct 13 ms 23892 KB n=500
52 Correct 13 ms 23892 KB n=500
53 Correct 13 ms 23860 KB n=500
54 Correct 13 ms 23924 KB n=500
55 Correct 13 ms 23892 KB n=278
56 Correct 13 ms 23896 KB n=500
57 Correct 13 ms 23892 KB n=500
58 Correct 13 ms 23932 KB n=500
59 Correct 16 ms 24316 KB n=2000
60 Correct 15 ms 24404 KB n=2000
61 Correct 15 ms 24324 KB n=2000
62 Correct 15 ms 24320 KB n=2000
63 Correct 16 ms 24276 KB n=2000
64 Correct 16 ms 24316 KB n=2000
65 Correct 16 ms 24276 KB n=2000
66 Correct 16 ms 24284 KB n=2000
67 Correct 17 ms 24240 KB n=2000
68 Correct 20 ms 24392 KB n=2000
69 Correct 15 ms 24276 KB n=2000
70 Correct 16 ms 24396 KB n=2000
71 Correct 15 ms 24412 KB n=2000
72 Correct 16 ms 24276 KB n=2000
73 Correct 16 ms 24276 KB n=2000
74 Correct 16 ms 24316 KB n=1844
75 Correct 16 ms 24276 KB n=2000
76 Correct 16 ms 24320 KB n=2000
77 Correct 17 ms 24316 KB n=2000
78 Correct 17 ms 24276 KB n=2000
79 Correct 16 ms 24276 KB n=2000
80 Correct 15 ms 24316 KB n=2000
81 Correct 17 ms 24284 KB n=2000
82 Correct 18 ms 24276 KB n=2000
83 Correct 15 ms 24284 KB n=2000
84 Correct 17 ms 24396 KB n=2000
85 Correct 16 ms 24276 KB n=2000
86 Correct 16 ms 24404 KB n=2000
87 Correct 15 ms 24288 KB n=2000
88 Correct 14 ms 24396 KB n=2000
89 Correct 15 ms 24404 KB n=2000
90 Correct 14 ms 24404 KB n=2000
91 Correct 16 ms 24308 KB n=2000
92 Correct 702 ms 79960 KB n=200000
93 Correct 736 ms 84812 KB n=200000
94 Correct 575 ms 88264 KB n=200000
95 Correct 692 ms 79676 KB n=200000
96 Correct 663 ms 79904 KB n=200000
97 Correct 787 ms 84012 KB n=200000
98 Correct 679 ms 79912 KB n=200000
99 Correct 798 ms 80000 KB n=200000
100 Correct 700 ms 79884 KB n=200000
101 Correct 565 ms 89612 KB n=200000
102 Correct 456 ms 80972 KB n=200000
103 Correct 448 ms 80972 KB n=200000
104 Correct 457 ms 80932 KB n=200000
105 Correct 457 ms 81536 KB n=200000
106 Correct 459 ms 81448 KB n=200000
107 Correct 464 ms 81412 KB n=200000
108 Correct 836 ms 80028 KB n=200000
109 Correct 916 ms 80072 KB n=200000
110 Correct 871 ms 79836 KB n=200000
111 Correct 893 ms 79292 KB n=200000
112 Correct 687 ms 88428 KB n=200000
113 Correct 846 ms 84048 KB n=200000
114 Correct 790 ms 79368 KB n=200000
115 Correct 1030 ms 81832 KB n=200000
116 Correct 731 ms 79940 KB n=200000
117 Correct 597 ms 88832 KB n=200000
118 Correct 907 ms 82764 KB n=200000
119 Correct 698 ms 79924 KB n=200000
120 Correct 523 ms 88384 KB n=200000
121 Correct 535 ms 88392 KB n=200000
122 Correct 522 ms 88836 KB n=200000
123 Correct 485 ms 81244 KB n=200000
124 Correct 163 ms 39628 KB n=25264