Submission #89637

# Submission time Handle Problem Language Result Execution time Memory
89637 2018-12-17T17:25:26 Z Vardanyan Birthday gift (IZhO18_treearray) C++14
100 / 100
1693 ms 99800 KB
#include <bits/stdc++.h>
using namespace std;
const int N = 2*1000*100+5;
int a[N];
vector<int> g[N];
int colour[N];
int par[N];
int dp[N][20];
int depth[N];
void kaxel(int v,int dep = 0,int p = -1){
    par[v] = p;
    dp[v][0] = p;
    depth[v] = dep;
    for(int i = 0;i<g[v].size();i++){
        int to = g[v][i];
        if(to == p) continue;
        kaxel(to,dep+1,v);
    }
}
int lca(int a,int b){
    if(depth[a]<depth[b]) swap(a,b);
    if(a == b) return a;
    int dif = depth[a]-depth[b];
    for(int i = 19;i>=0;i--){
        if((dif>>i)&1) a = dp[a][i];
    }
//    cout<<a<<" "<<b<<endl;
    if(a == b) return a;
    for(int i = 19;i>=0;i--){
        if(dp[a][i] == -1 || dp[b][i] == -1) continue;
        if(dp[a][i] == dp[b][i]) continue;
        a = dp[a][i];
        b = dp[b][i];
    }
    if(a == b) return a;
    a = dp[a][0];
    return a;
}
set<int> ANS[N];
set<int> A[N];
int main(){
    int n,m,q;
    scanf("%d%d%d",&n,&m,&q);
    for(int i = 1;i<=n-1;i++){
        int x,y;
        scanf("%d%d",&x,&y);
        g[x].push_back(y);
        g[y].push_back(x);
    }
    memset(dp,-1,sizeof dp);
    kaxel(1);
    for(int j = 1;j<=19;j++){
        for(int i = 1;i<=n;i++){
            if(dp[i][j-1] == -1) dp[i][j] = -1;
           else dp[i][j] = dp[dp[i][j-1]][j-1];
        }
    }
 
    /*int x,y;
    while(cin>>x>>y){
            cout<<lca(x,y)<<endl;
    }*/
    /*for(int i = 1;i<=n;i++){
        for(int j = 0;j<=4;j++){
            cout<<dp[i][j]<<" ";
        }
        cout<<endl;
    }*/
    for(int i = 1;i<=m;i++) scanf("%d",&a[i]);
    for(int i = 1;i<=m;i++){
        A[a[i]].insert(i);
        if(i == m) continue;
        ANS[lca(a[i],a[i+1])].insert(i);
    }
    while(q--){
        int tp;
        scanf("%d",&tp);
        if(tp == 1){
            int pos,val;
            scanf("%d%d",&pos,&val);
            int now = a[pos];
            A[a[pos]].erase(pos);
            if(pos>1){
              ANS[lca(a[pos],a[pos-1])].erase(pos-1);
            }
            if(pos<m){
              ANS[lca(a[pos],a[pos+1])].erase(pos);
            }
            a[pos] = val;
            A[val].insert(pos);
         if(pos-1>0)   ANS[lca(a[pos],a[pos-1])].insert(pos-1);
         if(pos+1<=m)   ANS[lca(a[pos],a[pos+1])].insert(pos);
        }
        else{
            int l,r,v;
            scanf("%d%d%d",&l,&r,&v);
            multiset<int>::iterator it = ANS[v].lower_bound(l);
            multiset<int>::iterator it1 = A[v].lower_bound(l);
            if(it1!=A[v].end()){
                int pos = *it1;
                if(pos<=r){
                    printf("%d %d\n",pos,pos);
                    continue;
                }
            }
            if(it == ANS[v].end()){
                printf("-1 -1\n");
                continue;
            }
            int pos = *it;
            if(pos>=r){
                printf("-1 -1\n");
                continue;
            }
            printf("%d %d\n",pos,pos+1);
        }
    }
 
    return 0;
}

Compilation message

treearray.cpp: In function 'void kaxel(int, int, int)':
treearray.cpp:14:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0;i<g[v].size();i++){
                   ~^~~~~~~~~~~~
treearray.cpp: In function 'int main()':
treearray.cpp:81:17: warning: unused variable 'now' [-Wunused-variable]
             int now = a[pos];
                 ^~~
treearray.cpp:43:10: 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:46:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d",&x,&y);
         ~~~~~^~~~~~~~~~~~~~
treearray.cpp:69:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(int i = 1;i<=m;i++) scanf("%d",&a[i]);
                             ~~~~~^~~~~~~~~~~~
treearray.cpp:77:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d",&tp);
         ~~~~~^~~~~~~~~~
treearray.cpp:80:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d%d",&pos,&val);
             ~~~~~^~~~~~~~~~~~~~~~~~
treearray.cpp:96:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d%d%d",&l,&r,&v);
             ~~~~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 33 ms 39544 KB n=5
2 Correct 34 ms 39660 KB n=100
3 Correct 34 ms 39660 KB n=100
4 Correct 35 ms 39660 KB n=100
5 Correct 39 ms 39764 KB n=100
6 Correct 41 ms 39764 KB n=100
7 Correct 39 ms 39764 KB n=100
8 Correct 34 ms 39764 KB n=100
9 Correct 34 ms 39764 KB n=100
10 Correct 34 ms 39764 KB n=100
11 Correct 34 ms 39764 KB n=100
12 Correct 35 ms 39764 KB n=100
13 Correct 34 ms 39764 KB n=100
14 Correct 41 ms 39764 KB n=100
15 Correct 40 ms 39764 KB n=100
16 Correct 39 ms 39764 KB n=100
17 Correct 40 ms 39764 KB n=100
18 Correct 40 ms 39764 KB n=100
19 Correct 36 ms 39764 KB n=100
20 Correct 34 ms 39764 KB n=100
21 Correct 34 ms 39764 KB n=100
22 Correct 39 ms 39764 KB n=100
23 Correct 39 ms 39764 KB n=100
24 Correct 41 ms 39764 KB n=100
25 Correct 39 ms 39764 KB n=100
26 Correct 39 ms 39764 KB n=12
27 Correct 39 ms 39764 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 33 ms 39544 KB n=5
2 Correct 34 ms 39660 KB n=100
3 Correct 34 ms 39660 KB n=100
4 Correct 35 ms 39660 KB n=100
5 Correct 39 ms 39764 KB n=100
6 Correct 41 ms 39764 KB n=100
7 Correct 39 ms 39764 KB n=100
8 Correct 34 ms 39764 KB n=100
9 Correct 34 ms 39764 KB n=100
10 Correct 34 ms 39764 KB n=100
11 Correct 34 ms 39764 KB n=100
12 Correct 35 ms 39764 KB n=100
13 Correct 34 ms 39764 KB n=100
14 Correct 41 ms 39764 KB n=100
15 Correct 40 ms 39764 KB n=100
16 Correct 39 ms 39764 KB n=100
17 Correct 40 ms 39764 KB n=100
18 Correct 40 ms 39764 KB n=100
19 Correct 36 ms 39764 KB n=100
20 Correct 34 ms 39764 KB n=100
21 Correct 34 ms 39764 KB n=100
22 Correct 39 ms 39764 KB n=100
23 Correct 39 ms 39764 KB n=100
24 Correct 41 ms 39764 KB n=100
25 Correct 39 ms 39764 KB n=100
26 Correct 39 ms 39764 KB n=12
27 Correct 39 ms 39764 KB n=100
28 Correct 41 ms 39804 KB n=500
29 Correct 41 ms 39804 KB n=500
30 Correct 44 ms 39804 KB n=500
31 Correct 39 ms 39804 KB n=500
32 Correct 40 ms 39804 KB n=500
33 Correct 40 ms 39804 KB n=500
34 Correct 40 ms 39888 KB n=500
35 Correct 39 ms 39888 KB n=500
36 Correct 41 ms 39888 KB n=500
37 Correct 41 ms 39888 KB n=500
38 Correct 41 ms 39888 KB n=500
39 Correct 40 ms 39888 KB n=500
40 Correct 39 ms 39888 KB n=500
41 Correct 39 ms 39888 KB n=500
42 Correct 40 ms 39888 KB n=500
43 Correct 40 ms 39888 KB n=500
44 Correct 40 ms 39916 KB n=500
45 Correct 40 ms 39916 KB n=500
46 Correct 40 ms 39916 KB n=500
47 Correct 40 ms 39916 KB n=500
48 Correct 40 ms 39916 KB n=500
49 Correct 40 ms 39916 KB n=500
50 Correct 41 ms 39916 KB n=500
51 Correct 41 ms 39916 KB n=500
52 Correct 41 ms 39916 KB n=500
53 Correct 40 ms 39916 KB n=500
54 Correct 41 ms 39916 KB n=500
55 Correct 40 ms 39916 KB n=278
56 Correct 41 ms 39916 KB n=500
57 Correct 40 ms 39916 KB n=500
58 Correct 40 ms 39916 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 33 ms 39544 KB n=5
2 Correct 34 ms 39660 KB n=100
3 Correct 34 ms 39660 KB n=100
4 Correct 35 ms 39660 KB n=100
5 Correct 39 ms 39764 KB n=100
6 Correct 41 ms 39764 KB n=100
7 Correct 39 ms 39764 KB n=100
8 Correct 34 ms 39764 KB n=100
9 Correct 34 ms 39764 KB n=100
10 Correct 34 ms 39764 KB n=100
11 Correct 34 ms 39764 KB n=100
12 Correct 35 ms 39764 KB n=100
13 Correct 34 ms 39764 KB n=100
14 Correct 41 ms 39764 KB n=100
15 Correct 40 ms 39764 KB n=100
16 Correct 39 ms 39764 KB n=100
17 Correct 40 ms 39764 KB n=100
18 Correct 40 ms 39764 KB n=100
19 Correct 36 ms 39764 KB n=100
20 Correct 34 ms 39764 KB n=100
21 Correct 34 ms 39764 KB n=100
22 Correct 39 ms 39764 KB n=100
23 Correct 39 ms 39764 KB n=100
24 Correct 41 ms 39764 KB n=100
25 Correct 39 ms 39764 KB n=100
26 Correct 39 ms 39764 KB n=12
27 Correct 39 ms 39764 KB n=100
28 Correct 41 ms 39804 KB n=500
29 Correct 41 ms 39804 KB n=500
30 Correct 44 ms 39804 KB n=500
31 Correct 39 ms 39804 KB n=500
32 Correct 40 ms 39804 KB n=500
33 Correct 40 ms 39804 KB n=500
34 Correct 40 ms 39888 KB n=500
35 Correct 39 ms 39888 KB n=500
36 Correct 41 ms 39888 KB n=500
37 Correct 41 ms 39888 KB n=500
38 Correct 41 ms 39888 KB n=500
39 Correct 40 ms 39888 KB n=500
40 Correct 39 ms 39888 KB n=500
41 Correct 39 ms 39888 KB n=500
42 Correct 40 ms 39888 KB n=500
43 Correct 40 ms 39888 KB n=500
44 Correct 40 ms 39916 KB n=500
45 Correct 40 ms 39916 KB n=500
46 Correct 40 ms 39916 KB n=500
47 Correct 40 ms 39916 KB n=500
48 Correct 40 ms 39916 KB n=500
49 Correct 40 ms 39916 KB n=500
50 Correct 41 ms 39916 KB n=500
51 Correct 41 ms 39916 KB n=500
52 Correct 41 ms 39916 KB n=500
53 Correct 40 ms 39916 KB n=500
54 Correct 41 ms 39916 KB n=500
55 Correct 40 ms 39916 KB n=278
56 Correct 41 ms 39916 KB n=500
57 Correct 40 ms 39916 KB n=500
58 Correct 40 ms 39916 KB n=500
59 Correct 43 ms 40044 KB n=2000
60 Correct 43 ms 40044 KB n=2000
61 Correct 44 ms 40060 KB n=2000
62 Correct 44 ms 40128 KB n=2000
63 Correct 39 ms 40128 KB n=2000
64 Correct 45 ms 40128 KB n=2000
65 Correct 44 ms 40128 KB n=2000
66 Correct 37 ms 40244 KB n=2000
67 Correct 40 ms 40244 KB n=2000
68 Correct 39 ms 40244 KB n=2000
69 Correct 43 ms 40244 KB n=2000
70 Correct 44 ms 40244 KB n=2000
71 Correct 44 ms 40244 KB n=2000
72 Correct 43 ms 40244 KB n=2000
73 Correct 44 ms 40244 KB n=2000
74 Correct 44 ms 40244 KB n=1844
75 Correct 42 ms 40244 KB n=2000
76 Correct 44 ms 40244 KB n=2000
77 Correct 44 ms 40244 KB n=2000
78 Correct 39 ms 40244 KB n=2000
79 Correct 39 ms 40244 KB n=2000
80 Correct 38 ms 40244 KB n=2000
81 Correct 38 ms 40244 KB n=2000
82 Correct 38 ms 40244 KB n=2000
83 Correct 39 ms 40244 KB n=2000
84 Correct 38 ms 40244 KB n=2000
85 Correct 47 ms 40244 KB n=2000
86 Correct 38 ms 40244 KB n=2000
87 Correct 38 ms 40244 KB n=2000
88 Correct 43 ms 40244 KB n=2000
89 Correct 45 ms 40244 KB n=2000
90 Correct 43 ms 40244 KB n=2000
91 Correct 43 ms 40244 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 33 ms 39544 KB n=5
2 Correct 34 ms 39660 KB n=100
3 Correct 34 ms 39660 KB n=100
4 Correct 35 ms 39660 KB n=100
5 Correct 39 ms 39764 KB n=100
6 Correct 41 ms 39764 KB n=100
7 Correct 39 ms 39764 KB n=100
8 Correct 34 ms 39764 KB n=100
9 Correct 34 ms 39764 KB n=100
10 Correct 34 ms 39764 KB n=100
11 Correct 34 ms 39764 KB n=100
12 Correct 35 ms 39764 KB n=100
13 Correct 34 ms 39764 KB n=100
14 Correct 41 ms 39764 KB n=100
15 Correct 40 ms 39764 KB n=100
16 Correct 39 ms 39764 KB n=100
17 Correct 40 ms 39764 KB n=100
18 Correct 40 ms 39764 KB n=100
19 Correct 36 ms 39764 KB n=100
20 Correct 34 ms 39764 KB n=100
21 Correct 34 ms 39764 KB n=100
22 Correct 39 ms 39764 KB n=100
23 Correct 39 ms 39764 KB n=100
24 Correct 41 ms 39764 KB n=100
25 Correct 39 ms 39764 KB n=100
26 Correct 39 ms 39764 KB n=12
27 Correct 39 ms 39764 KB n=100
28 Correct 41 ms 39804 KB n=500
29 Correct 41 ms 39804 KB n=500
30 Correct 44 ms 39804 KB n=500
31 Correct 39 ms 39804 KB n=500
32 Correct 40 ms 39804 KB n=500
33 Correct 40 ms 39804 KB n=500
34 Correct 40 ms 39888 KB n=500
35 Correct 39 ms 39888 KB n=500
36 Correct 41 ms 39888 KB n=500
37 Correct 41 ms 39888 KB n=500
38 Correct 41 ms 39888 KB n=500
39 Correct 40 ms 39888 KB n=500
40 Correct 39 ms 39888 KB n=500
41 Correct 39 ms 39888 KB n=500
42 Correct 40 ms 39888 KB n=500
43 Correct 40 ms 39888 KB n=500
44 Correct 40 ms 39916 KB n=500
45 Correct 40 ms 39916 KB n=500
46 Correct 40 ms 39916 KB n=500
47 Correct 40 ms 39916 KB n=500
48 Correct 40 ms 39916 KB n=500
49 Correct 40 ms 39916 KB n=500
50 Correct 41 ms 39916 KB n=500
51 Correct 41 ms 39916 KB n=500
52 Correct 41 ms 39916 KB n=500
53 Correct 40 ms 39916 KB n=500
54 Correct 41 ms 39916 KB n=500
55 Correct 40 ms 39916 KB n=278
56 Correct 41 ms 39916 KB n=500
57 Correct 40 ms 39916 KB n=500
58 Correct 40 ms 39916 KB n=500
59 Correct 43 ms 40044 KB n=2000
60 Correct 43 ms 40044 KB n=2000
61 Correct 44 ms 40060 KB n=2000
62 Correct 44 ms 40128 KB n=2000
63 Correct 39 ms 40128 KB n=2000
64 Correct 45 ms 40128 KB n=2000
65 Correct 44 ms 40128 KB n=2000
66 Correct 37 ms 40244 KB n=2000
67 Correct 40 ms 40244 KB n=2000
68 Correct 39 ms 40244 KB n=2000
69 Correct 43 ms 40244 KB n=2000
70 Correct 44 ms 40244 KB n=2000
71 Correct 44 ms 40244 KB n=2000
72 Correct 43 ms 40244 KB n=2000
73 Correct 44 ms 40244 KB n=2000
74 Correct 44 ms 40244 KB n=1844
75 Correct 42 ms 40244 KB n=2000
76 Correct 44 ms 40244 KB n=2000
77 Correct 44 ms 40244 KB n=2000
78 Correct 39 ms 40244 KB n=2000
79 Correct 39 ms 40244 KB n=2000
80 Correct 38 ms 40244 KB n=2000
81 Correct 38 ms 40244 KB n=2000
82 Correct 38 ms 40244 KB n=2000
83 Correct 39 ms 40244 KB n=2000
84 Correct 38 ms 40244 KB n=2000
85 Correct 47 ms 40244 KB n=2000
86 Correct 38 ms 40244 KB n=2000
87 Correct 38 ms 40244 KB n=2000
88 Correct 43 ms 40244 KB n=2000
89 Correct 45 ms 40244 KB n=2000
90 Correct 43 ms 40244 KB n=2000
91 Correct 43 ms 40244 KB n=2000
92 Correct 947 ms 69292 KB n=200000
93 Correct 1550 ms 76460 KB n=200000
94 Correct 1534 ms 80192 KB n=200000
95 Correct 1193 ms 80192 KB n=200000
96 Correct 1169 ms 80192 KB n=200000
97 Correct 1534 ms 80192 KB n=200000
98 Correct 1137 ms 80192 KB n=200000
99 Correct 1336 ms 80192 KB n=200000
100 Correct 1071 ms 80192 KB n=200000
101 Correct 1442 ms 81484 KB n=200000
102 Correct 697 ms 81484 KB n=200000
103 Correct 626 ms 81484 KB n=200000
104 Correct 595 ms 81484 KB n=200000
105 Correct 578 ms 81484 KB n=200000
106 Correct 585 ms 81484 KB n=200000
107 Correct 584 ms 81484 KB n=200000
108 Correct 1036 ms 81484 KB n=200000
109 Correct 1182 ms 81484 KB n=200000
110 Correct 1042 ms 81484 KB n=200000
111 Correct 982 ms 81484 KB n=200000
112 Correct 1088 ms 81484 KB n=200000
113 Correct 1359 ms 81484 KB n=200000
114 Correct 1228 ms 81484 KB n=200000
115 Correct 1693 ms 81484 KB n=200000
116 Correct 1057 ms 81484 KB n=200000
117 Correct 1333 ms 81484 KB n=200000
118 Correct 1432 ms 81484 KB n=200000
119 Correct 877 ms 81484 KB n=200000
120 Correct 994 ms 85420 KB n=200000
121 Correct 1167 ms 92624 KB n=200000
122 Correct 1370 ms 99800 KB n=200000
123 Correct 664 ms 99800 KB n=200000
124 Correct 326 ms 99800 KB n=25264