Submission #207465

# Submission time Handle Problem Language Result Execution time Memory
207465 2020-03-07T15:11:03 Z mohamedsobhi777 Birthday gift (IZhO18_treearray) C++14
100 / 100
1934 ms 88828 KB
#include<bits/stdc++.h> 
 
using namespace std ;

const int N = 2e5 + 7 ; 

int n , t , m, q; 
vector<int> adj[N] ; 

int st[N] , en[N] ; 
int a[N] , lcaA[N]; 
int col[N] , up[N][24]; 
set<int> lcas[N] , vls[N];


int isp(int x , int y )
{
    return (st[y]>=st[x] &&  en[y] <=en[x]);
}

void dfs(int x , int p)
{   
    st[x] = t++ ; 
    up[x][0] = p ; 
    for(int i = 1 ; i < 24 ; i++)
        up[x][i] = up[ up[x][i-1] ][i-1];
    for(auto u : adj[x])
    {       
        if(u==p)
        continue;
        dfs(u , x) ; 
    }
    en[x] = t++; 
}

bool inside(int L, int R , int l , int r)
{
    return (l>=L && l<=R && r>=L && r<=R);
}

int lca(int x , int y)
{
    if(isp(x , y))
        return x ; 
    if(isp(y , x))
        return y ; 
    for(int i = 23 ; i>=0 ; i--)
    {
        if(!isp(up[x][i] , y))
            x = up[x][i] ; 
    }
    return up[x][0] ; 
}

int main() 
{
    //freopen("in.in" , "r" , stdin) ; 
    cin>>n>>m>>q; 
    for(int i = 0 ; i < n-1 ; i++)
    {
        int a, b ; 
        cin>>a>>b ; 
        adj[a].push_back(b) ; 
        adj[b].push_back(a) ; 
    }
    dfs(1 , 1) ; 
    for(int i = 0 ; i<m ; i++)
    {
        cin>>a[i] ; 
        vls[a[i]] .insert(i) ; 
        if(i) 
        {
            int lc = lca(a[i] , a[i-1] ) ;
            lcaA[i] = lc; 
            lcas[lc].insert(i); 
        }
    }
    while(q--)
    {
        int idx , A , B; 
        cin>>idx>>A>>B ; 

        if(idx==1)
        {   
             vls[a[A-1]].erase( A-1 );
            a[A-1] = B; 
            vls[B].insert(A-1);
            if(A>1)
            {
                lcas[ lcaA[ A-1 ] ] .erase(A-1) ; 
                int lc = lca(a[A-1] , a[A-2]) ; 
                lcaA[A-1] = lca(a[A-1] , a[A-2]) ; 
                lcas[lc].insert(A-1) ; 
            }
            if(A<m)
            {
                lcas[lcaA[A]].erase(A);
                int lc = lca(a[A] , a[A-1]) ; 
                lcas[lc].insert(A) ; 
                lcaA[A] = lca(a[A] , a[A-1]) ;
            }
        }
        else
        {
            int C; 
            cin>>C ; 
            int l = st[C] , r = en[C] ;  
            int nodeA = -2 , nodeB = -2;
            set<int>::iterator u = lcas[C].lower_bound(A) ;
            set<int>::iterator u1 = vls[C].lower_bound(A-1) ;
            if(u!=lcas[C].end())
            {
                if(*u<=B-1)
                {
                    nodeA = (*u) -1 ;
                    nodeB = *u;
                }
            }
            if(u1!=vls[C].end())
            {
                if(*u1<=B-1)
                {
                    nodeA = nodeB = (*u1) ;
                }
            }   
            cout<<nodeA+1<<" " <<nodeB+1<<"\n" ; 
        }
        
    }
    return 0 ;  
}

Compilation message

treearray.cpp: In function 'int main()':
treearray.cpp:107:17: warning: unused variable 'l' [-Wunused-variable]
             int l = st[C] , r = en[C] ;  
                 ^
treearray.cpp:107:29: warning: unused variable 'r' [-Wunused-variable]
             int l = st[C] , r = en[C] ;  
                             ^
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23800 KB n=5
2 Correct 18 ms 23800 KB n=100
3 Correct 20 ms 23800 KB n=100
4 Correct 18 ms 23800 KB n=100
5 Correct 19 ms 23800 KB n=100
6 Correct 19 ms 23800 KB n=100
7 Correct 19 ms 23800 KB n=100
8 Correct 19 ms 23928 KB n=100
9 Correct 18 ms 23804 KB n=100
10 Correct 18 ms 23928 KB n=100
11 Correct 18 ms 23800 KB n=100
12 Correct 19 ms 23928 KB n=100
13 Correct 18 ms 23800 KB n=100
14 Correct 18 ms 23800 KB n=100
15 Correct 18 ms 23800 KB n=100
16 Correct 19 ms 23800 KB n=100
17 Correct 19 ms 23932 KB n=100
18 Correct 19 ms 23800 KB n=100
19 Correct 19 ms 23804 KB n=100
20 Correct 19 ms 23800 KB n=100
21 Correct 18 ms 23800 KB n=100
22 Correct 20 ms 23800 KB n=100
23 Correct 18 ms 23804 KB n=100
24 Correct 19 ms 23800 KB n=100
25 Correct 19 ms 23800 KB n=100
26 Correct 18 ms 23928 KB n=12
27 Correct 18 ms 23800 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23800 KB n=5
2 Correct 18 ms 23800 KB n=100
3 Correct 20 ms 23800 KB n=100
4 Correct 18 ms 23800 KB n=100
5 Correct 19 ms 23800 KB n=100
6 Correct 19 ms 23800 KB n=100
7 Correct 19 ms 23800 KB n=100
8 Correct 19 ms 23928 KB n=100
9 Correct 18 ms 23804 KB n=100
10 Correct 18 ms 23928 KB n=100
11 Correct 18 ms 23800 KB n=100
12 Correct 19 ms 23928 KB n=100
13 Correct 18 ms 23800 KB n=100
14 Correct 18 ms 23800 KB n=100
15 Correct 18 ms 23800 KB n=100
16 Correct 19 ms 23800 KB n=100
17 Correct 19 ms 23932 KB n=100
18 Correct 19 ms 23800 KB n=100
19 Correct 19 ms 23804 KB n=100
20 Correct 19 ms 23800 KB n=100
21 Correct 18 ms 23800 KB n=100
22 Correct 20 ms 23800 KB n=100
23 Correct 18 ms 23804 KB n=100
24 Correct 19 ms 23800 KB n=100
25 Correct 19 ms 23800 KB n=100
26 Correct 18 ms 23928 KB n=12
27 Correct 18 ms 23800 KB n=100
28 Correct 20 ms 23928 KB n=500
29 Correct 20 ms 23928 KB n=500
30 Correct 20 ms 23928 KB n=500
31 Correct 21 ms 23928 KB n=500
32 Correct 21 ms 23928 KB n=500
33 Correct 20 ms 23928 KB n=500
34 Correct 21 ms 23928 KB n=500
35 Correct 20 ms 23928 KB n=500
36 Correct 20 ms 23928 KB n=500
37 Correct 21 ms 23928 KB n=500
38 Correct 20 ms 23928 KB n=500
39 Correct 24 ms 23932 KB n=500
40 Correct 20 ms 23928 KB n=500
41 Correct 20 ms 23928 KB n=500
42 Correct 21 ms 24056 KB n=500
43 Correct 21 ms 23928 KB n=500
44 Correct 20 ms 23928 KB n=500
45 Correct 21 ms 23928 KB n=500
46 Correct 21 ms 24056 KB n=500
47 Correct 20 ms 23928 KB n=500
48 Correct 21 ms 23928 KB n=500
49 Correct 20 ms 23928 KB n=500
50 Correct 20 ms 23928 KB n=500
51 Correct 20 ms 23928 KB n=500
52 Correct 19 ms 23928 KB n=500
53 Correct 21 ms 23928 KB n=500
54 Correct 20 ms 23928 KB n=500
55 Correct 19 ms 23928 KB n=278
56 Correct 20 ms 23928 KB n=500
57 Correct 20 ms 23928 KB n=500
58 Correct 21 ms 23932 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23800 KB n=5
2 Correct 18 ms 23800 KB n=100
3 Correct 20 ms 23800 KB n=100
4 Correct 18 ms 23800 KB n=100
5 Correct 19 ms 23800 KB n=100
6 Correct 19 ms 23800 KB n=100
7 Correct 19 ms 23800 KB n=100
8 Correct 19 ms 23928 KB n=100
9 Correct 18 ms 23804 KB n=100
10 Correct 18 ms 23928 KB n=100
11 Correct 18 ms 23800 KB n=100
12 Correct 19 ms 23928 KB n=100
13 Correct 18 ms 23800 KB n=100
14 Correct 18 ms 23800 KB n=100
15 Correct 18 ms 23800 KB n=100
16 Correct 19 ms 23800 KB n=100
17 Correct 19 ms 23932 KB n=100
18 Correct 19 ms 23800 KB n=100
19 Correct 19 ms 23804 KB n=100
20 Correct 19 ms 23800 KB n=100
21 Correct 18 ms 23800 KB n=100
22 Correct 20 ms 23800 KB n=100
23 Correct 18 ms 23804 KB n=100
24 Correct 19 ms 23800 KB n=100
25 Correct 19 ms 23800 KB n=100
26 Correct 18 ms 23928 KB n=12
27 Correct 18 ms 23800 KB n=100
28 Correct 20 ms 23928 KB n=500
29 Correct 20 ms 23928 KB n=500
30 Correct 20 ms 23928 KB n=500
31 Correct 21 ms 23928 KB n=500
32 Correct 21 ms 23928 KB n=500
33 Correct 20 ms 23928 KB n=500
34 Correct 21 ms 23928 KB n=500
35 Correct 20 ms 23928 KB n=500
36 Correct 20 ms 23928 KB n=500
37 Correct 21 ms 23928 KB n=500
38 Correct 20 ms 23928 KB n=500
39 Correct 24 ms 23932 KB n=500
40 Correct 20 ms 23928 KB n=500
41 Correct 20 ms 23928 KB n=500
42 Correct 21 ms 24056 KB n=500
43 Correct 21 ms 23928 KB n=500
44 Correct 20 ms 23928 KB n=500
45 Correct 21 ms 23928 KB n=500
46 Correct 21 ms 24056 KB n=500
47 Correct 20 ms 23928 KB n=500
48 Correct 21 ms 23928 KB n=500
49 Correct 20 ms 23928 KB n=500
50 Correct 20 ms 23928 KB n=500
51 Correct 20 ms 23928 KB n=500
52 Correct 19 ms 23928 KB n=500
53 Correct 21 ms 23928 KB n=500
54 Correct 20 ms 23928 KB n=500
55 Correct 19 ms 23928 KB n=278
56 Correct 20 ms 23928 KB n=500
57 Correct 20 ms 23928 KB n=500
58 Correct 21 ms 23932 KB n=500
59 Correct 27 ms 24312 KB n=2000
60 Correct 26 ms 24440 KB n=2000
61 Correct 27 ms 24312 KB n=2000
62 Correct 27 ms 24312 KB n=2000
63 Correct 32 ms 24312 KB n=2000
64 Correct 28 ms 24312 KB n=2000
65 Correct 27 ms 24312 KB n=2000
66 Correct 27 ms 24440 KB n=2000
67 Correct 28 ms 24316 KB n=2000
68 Correct 26 ms 24312 KB n=2000
69 Correct 36 ms 24312 KB n=2000
70 Correct 28 ms 24312 KB n=2000
71 Correct 28 ms 24312 KB n=2000
72 Correct 29 ms 24312 KB n=2000
73 Correct 28 ms 24312 KB n=2000
74 Correct 26 ms 24316 KB n=1844
75 Correct 29 ms 24440 KB n=2000
76 Correct 27 ms 24312 KB n=2000
77 Correct 28 ms 24312 KB n=2000
78 Correct 28 ms 24312 KB n=2000
79 Correct 28 ms 24312 KB n=2000
80 Correct 27 ms 24316 KB n=2000
81 Correct 30 ms 24312 KB n=2000
82 Correct 28 ms 24312 KB n=2000
83 Correct 26 ms 24440 KB n=2000
84 Correct 28 ms 24312 KB n=2000
85 Correct 27 ms 24312 KB n=2000
86 Correct 28 ms 24312 KB n=2000
87 Correct 29 ms 24312 KB n=2000
88 Correct 27 ms 24440 KB n=2000
89 Correct 27 ms 24440 KB n=2000
90 Correct 26 ms 24440 KB n=2000
91 Correct 28 ms 24312 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23800 KB n=5
2 Correct 18 ms 23800 KB n=100
3 Correct 20 ms 23800 KB n=100
4 Correct 18 ms 23800 KB n=100
5 Correct 19 ms 23800 KB n=100
6 Correct 19 ms 23800 KB n=100
7 Correct 19 ms 23800 KB n=100
8 Correct 19 ms 23928 KB n=100
9 Correct 18 ms 23804 KB n=100
10 Correct 18 ms 23928 KB n=100
11 Correct 18 ms 23800 KB n=100
12 Correct 19 ms 23928 KB n=100
13 Correct 18 ms 23800 KB n=100
14 Correct 18 ms 23800 KB n=100
15 Correct 18 ms 23800 KB n=100
16 Correct 19 ms 23800 KB n=100
17 Correct 19 ms 23932 KB n=100
18 Correct 19 ms 23800 KB n=100
19 Correct 19 ms 23804 KB n=100
20 Correct 19 ms 23800 KB n=100
21 Correct 18 ms 23800 KB n=100
22 Correct 20 ms 23800 KB n=100
23 Correct 18 ms 23804 KB n=100
24 Correct 19 ms 23800 KB n=100
25 Correct 19 ms 23800 KB n=100
26 Correct 18 ms 23928 KB n=12
27 Correct 18 ms 23800 KB n=100
28 Correct 20 ms 23928 KB n=500
29 Correct 20 ms 23928 KB n=500
30 Correct 20 ms 23928 KB n=500
31 Correct 21 ms 23928 KB n=500
32 Correct 21 ms 23928 KB n=500
33 Correct 20 ms 23928 KB n=500
34 Correct 21 ms 23928 KB n=500
35 Correct 20 ms 23928 KB n=500
36 Correct 20 ms 23928 KB n=500
37 Correct 21 ms 23928 KB n=500
38 Correct 20 ms 23928 KB n=500
39 Correct 24 ms 23932 KB n=500
40 Correct 20 ms 23928 KB n=500
41 Correct 20 ms 23928 KB n=500
42 Correct 21 ms 24056 KB n=500
43 Correct 21 ms 23928 KB n=500
44 Correct 20 ms 23928 KB n=500
45 Correct 21 ms 23928 KB n=500
46 Correct 21 ms 24056 KB n=500
47 Correct 20 ms 23928 KB n=500
48 Correct 21 ms 23928 KB n=500
49 Correct 20 ms 23928 KB n=500
50 Correct 20 ms 23928 KB n=500
51 Correct 20 ms 23928 KB n=500
52 Correct 19 ms 23928 KB n=500
53 Correct 21 ms 23928 KB n=500
54 Correct 20 ms 23928 KB n=500
55 Correct 19 ms 23928 KB n=278
56 Correct 20 ms 23928 KB n=500
57 Correct 20 ms 23928 KB n=500
58 Correct 21 ms 23932 KB n=500
59 Correct 27 ms 24312 KB n=2000
60 Correct 26 ms 24440 KB n=2000
61 Correct 27 ms 24312 KB n=2000
62 Correct 27 ms 24312 KB n=2000
63 Correct 32 ms 24312 KB n=2000
64 Correct 28 ms 24312 KB n=2000
65 Correct 27 ms 24312 KB n=2000
66 Correct 27 ms 24440 KB n=2000
67 Correct 28 ms 24316 KB n=2000
68 Correct 26 ms 24312 KB n=2000
69 Correct 36 ms 24312 KB n=2000
70 Correct 28 ms 24312 KB n=2000
71 Correct 28 ms 24312 KB n=2000
72 Correct 29 ms 24312 KB n=2000
73 Correct 28 ms 24312 KB n=2000
74 Correct 26 ms 24316 KB n=1844
75 Correct 29 ms 24440 KB n=2000
76 Correct 27 ms 24312 KB n=2000
77 Correct 28 ms 24312 KB n=2000
78 Correct 28 ms 24312 KB n=2000
79 Correct 28 ms 24312 KB n=2000
80 Correct 27 ms 24316 KB n=2000
81 Correct 30 ms 24312 KB n=2000
82 Correct 28 ms 24312 KB n=2000
83 Correct 26 ms 24440 KB n=2000
84 Correct 28 ms 24312 KB n=2000
85 Correct 27 ms 24312 KB n=2000
86 Correct 28 ms 24312 KB n=2000
87 Correct 29 ms 24312 KB n=2000
88 Correct 27 ms 24440 KB n=2000
89 Correct 27 ms 24440 KB n=2000
90 Correct 26 ms 24440 KB n=2000
91 Correct 28 ms 24312 KB n=2000
92 Correct 1554 ms 72920 KB n=200000
93 Correct 1597 ms 76832 KB n=200000
94 Correct 1375 ms 87808 KB n=200000
95 Correct 1469 ms 79212 KB n=200000
96 Correct 1499 ms 79340 KB n=200000
97 Correct 1678 ms 83320 KB n=200000
98 Correct 1522 ms 79212 KB n=200000
99 Correct 1757 ms 79448 KB n=200000
100 Correct 1624 ms 79216 KB n=200000
101 Correct 1315 ms 88828 KB n=200000
102 Correct 1353 ms 80376 KB n=200000
103 Correct 1395 ms 80432 KB n=200000
104 Correct 1391 ms 80484 KB n=200000
105 Correct 1450 ms 80768 KB n=200000
106 Correct 1438 ms 80760 KB n=200000
107 Correct 1376 ms 80792 KB n=200000
108 Correct 1587 ms 79200 KB n=200000
109 Correct 1568 ms 79204 KB n=200000
110 Correct 1569 ms 79272 KB n=200000
111 Correct 1519 ms 78700 KB n=200000
112 Correct 1389 ms 87828 KB n=200000
113 Correct 1649 ms 83324 KB n=200000
114 Correct 1553 ms 78616 KB n=200000
115 Correct 1934 ms 81488 KB n=200000
116 Correct 1447 ms 79340 KB n=200000
117 Correct 1317 ms 88200 KB n=200000
118 Correct 1763 ms 82272 KB n=200000
119 Correct 1457 ms 79540 KB n=200000
120 Correct 1216 ms 87928 KB n=200000
121 Correct 1241 ms 87928 KB n=200000
122 Correct 1241 ms 88228 KB n=200000
123 Correct 1404 ms 80620 KB n=200000
124 Correct 413 ms 39544 KB n=25264