Submission #378978

# Submission time Handle Problem Language Result Execution time Memory
378978 2021-03-17T08:09:19 Z daniel920712 Birthday gift (IZhO18_treearray) C++14
56 / 100
611 ms 262148 KB
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>

using namespace std;
int deg[200005];
vector < int > Next[200005];
int Father[30][200005]={0};
int all[200005];
int now=1;
struct A
{
    int l,r;
    int nxl,nxr;
    int ans;
}Node[200005];
void F(int here,int fa,int deg)
{
    int j;
    ::deg[here]=deg;
    for(auto i:Next[here])
    {
        if(i==fa) continue;
        Father[0][i]=here;
        for(j=1;j<25;j++) Father[j][i]=Father[j-1][Father[j-1][i]];
        F(i,here,deg+1);
    }
}
int LCA(int a,int b)
{
    int i;
    if(deg[a]<deg[b]) swap(a,b);
    for(i=20;i>=0;i--) if(deg[a]>deg[b]&&deg[a]-(1<<i)>=deg[b]) a=Father[i][a];
    for(i=20;i>=0;i--)
    {
        if(deg[a]==deg[b]&&Father[i][a]!=Father[i][b])
        {
            a=Father[i][a];
            b=Father[i][b];
        }
    }
    if(a==b) return a;
    if(Father[0][a]==Father[0][b]) return Father[0][a];
}
void build(int l,int r,int here)
{
    Node[here].l=l;
    Node[here].r=r;
    if(l==r)
    {
        Node[here].ans=all[l];
        return;
    }
    Node[here].nxl=now++;
    Node[here].nxr=now++;
    build(l,(l+r)/2,Node[here].nxl);
    build((l+r)/2+1,r,Node[here].nxr);
    Node[here].ans=LCA(Node[Node[here].nxl].ans,Node[Node[here].nxr].ans);
}
void cha(int where,int con,int here)
{
    if(where==Node[here].l&&where==Node[here].r)
    {
        Node[here].ans=con;
        return;
    }
    if(where<=(Node[here].l+Node[here].r)/2) cha(where,con,Node[here].nxl);
    else cha(where,con,Node[here].nxr);
    Node[here].ans=LCA(Node[Node[here].nxl].ans,Node[Node[here].nxr].ans);
}
int Find(int l,int r,int here)
{
    if(Node[here].l==l&&Node[here].r==r) return Node[here].ans;
    if(r<=(Node[here].l+Node[here].r)/2) return Find(l,r,Node[here].nxl);
    else if(l>(Node[here].l+Node[here].r)/2) return Find(l,r,Node[here].nxr);
    else return LCA(Find(l,(Node[here].l+Node[here].r)/2,Node[here].nxl),Find((Node[here].l+Node[here].r)/2+1,r,Node[here].nxr));
}
int main()
{
    int N,M,Q,i,j,a,b,l,r,t,ok=0,tt,ll,rr;
    scanf("%d %d %d",&N,&M,&Q);
    for(i=1;i<N;i++)
    {
        scanf("%d %d",&a,&b);
        Next[a].push_back(b);
        Next[b].push_back(a);
    }

    F(1,-1,0);
    for(i=1;i<=M;i++) scanf("%d",&all[i]);
    build(1,M,0);
    while(Q--)
    {
        scanf("%d",&t);
        if(t==1)
        {
            scanf("%d %d",&l,&r);
            all[l]=r;
            cha(l,r,0);
        }
        else
        {
            scanf("%d %d %d",&l,&r,&t);
            ok=0;
            for(i=l;i<=r;i++)
            {
                if(all[i]==t)
                {
                    printf("%d %d\n",i,i);
                    ok=1;
                    break;
                }
            }
            if(!ok)
            {
                ll=l;
                rr=l;
                now=all[l];
                while(rr<r)
                {
                    rr++;
                    now=LCA(now,all[rr]);
                    if(now==t)
                    {
                        printf("%d %d\n",ll,rr);
                        ok=1;
                        break;
                    }
                    while(ll<rr&&deg[now]<=deg[t])
                    {
                        ll++;
                        now=Find(ll,rr,0);
                    }
                }
            }
            if(!ok) printf("-1 -1\n");
        }
    }
    return 0;
}
/*
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:81:17: warning: unused variable 'j' [-Wunused-variable]
   81 |     int N,M,Q,i,j,a,b,l,r,t,ok=0,tt,ll,rr;
      |                 ^
treearray.cpp:81:34: warning: unused variable 'tt' [-Wunused-variable]
   81 |     int N,M,Q,i,j,a,b,l,r,t,ok=0,tt,ll,rr;
      |                                  ^~
treearray.cpp: In function 'int LCA(int, int)':
treearray.cpp:45:1: warning: control reaches end of non-void function [-Wreturn-type]
   45 | }
      | ^
treearray.cpp: In function 'int main()':
treearray.cpp:82:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   82 |     scanf("%d %d %d",&N,&M,&Q);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~
treearray.cpp:85:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   85 |         scanf("%d %d",&a,&b);
      |         ~~~~~^~~~~~~~~~~~~~~
treearray.cpp:91:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   91 |     for(i=1;i<=M;i++) scanf("%d",&all[i]);
      |                       ~~~~~^~~~~~~~~~~~~~
treearray.cpp:95:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   95 |         scanf("%d",&t);
      |         ~~~~~^~~~~~~~~
treearray.cpp:98:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   98 |             scanf("%d %d",&l,&r);
      |             ~~~~~^~~~~~~~~~~~~~~
treearray.cpp:104:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  104 |             scanf("%d %d %d",&l,&r,&t);
      |             ~~~~~^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5228 KB n=5
2 Correct 5 ms 5228 KB n=100
3 Correct 5 ms 5228 KB n=100
4 Correct 4 ms 5228 KB n=100
5 Correct 4 ms 5228 KB n=100
6 Correct 5 ms 5228 KB n=100
7 Correct 8 ms 5228 KB n=100
8 Correct 4 ms 5228 KB n=100
9 Correct 4 ms 5228 KB n=100
10 Correct 4 ms 5228 KB n=100
11 Correct 5 ms 5228 KB n=100
12 Correct 4 ms 5228 KB n=100
13 Correct 4 ms 5228 KB n=100
14 Correct 4 ms 5228 KB n=100
15 Correct 4 ms 5228 KB n=100
16 Correct 4 ms 5228 KB n=100
17 Correct 4 ms 5228 KB n=100
18 Correct 4 ms 5228 KB n=100
19 Correct 5 ms 5228 KB n=100
20 Correct 4 ms 5228 KB n=100
21 Correct 4 ms 5228 KB n=100
22 Correct 4 ms 5228 KB n=100
23 Correct 5 ms 5228 KB n=100
24 Correct 5 ms 5228 KB n=100
25 Correct 5 ms 5376 KB n=100
26 Correct 4 ms 5228 KB n=12
27 Correct 5 ms 5228 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5228 KB n=5
2 Correct 5 ms 5228 KB n=100
3 Correct 5 ms 5228 KB n=100
4 Correct 4 ms 5228 KB n=100
5 Correct 4 ms 5228 KB n=100
6 Correct 5 ms 5228 KB n=100
7 Correct 8 ms 5228 KB n=100
8 Correct 4 ms 5228 KB n=100
9 Correct 4 ms 5228 KB n=100
10 Correct 4 ms 5228 KB n=100
11 Correct 5 ms 5228 KB n=100
12 Correct 4 ms 5228 KB n=100
13 Correct 4 ms 5228 KB n=100
14 Correct 4 ms 5228 KB n=100
15 Correct 4 ms 5228 KB n=100
16 Correct 4 ms 5228 KB n=100
17 Correct 4 ms 5228 KB n=100
18 Correct 4 ms 5228 KB n=100
19 Correct 5 ms 5228 KB n=100
20 Correct 4 ms 5228 KB n=100
21 Correct 4 ms 5228 KB n=100
22 Correct 4 ms 5228 KB n=100
23 Correct 5 ms 5228 KB n=100
24 Correct 5 ms 5228 KB n=100
25 Correct 5 ms 5376 KB n=100
26 Correct 4 ms 5228 KB n=12
27 Correct 5 ms 5228 KB n=100
28 Correct 5 ms 5228 KB n=500
29 Correct 6 ms 5228 KB n=500
30 Correct 7 ms 5228 KB n=500
31 Correct 8 ms 5228 KB n=500
32 Correct 5 ms 5228 KB n=500
33 Correct 7 ms 5228 KB n=500
34 Correct 5 ms 5228 KB n=500
35 Correct 7 ms 5228 KB n=500
36 Correct 34 ms 5228 KB n=500
37 Correct 42 ms 5324 KB n=500
38 Correct 42 ms 5228 KB n=500
39 Correct 24 ms 5228 KB n=500
40 Correct 20 ms 5228 KB n=500
41 Correct 19 ms 5228 KB n=500
42 Correct 21 ms 5228 KB n=500
43 Correct 21 ms 5228 KB n=500
44 Correct 20 ms 5228 KB n=500
45 Correct 5 ms 5228 KB n=500
46 Correct 7 ms 5228 KB n=500
47 Correct 7 ms 5228 KB n=500
48 Correct 4 ms 5228 KB n=500
49 Correct 7 ms 5228 KB n=500
50 Correct 10 ms 5228 KB n=500
51 Correct 14 ms 5228 KB n=500
52 Correct 16 ms 5228 KB n=500
53 Correct 14 ms 5228 KB n=500
54 Correct 15 ms 5228 KB n=500
55 Correct 4 ms 5228 KB n=278
56 Correct 23 ms 5228 KB n=500
57 Correct 23 ms 5228 KB n=500
58 Correct 32 ms 5376 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5228 KB n=5
2 Correct 5 ms 5228 KB n=100
3 Correct 5 ms 5228 KB n=100
4 Correct 4 ms 5228 KB n=100
5 Correct 4 ms 5228 KB n=100
6 Correct 5 ms 5228 KB n=100
7 Correct 8 ms 5228 KB n=100
8 Correct 4 ms 5228 KB n=100
9 Correct 4 ms 5228 KB n=100
10 Correct 4 ms 5228 KB n=100
11 Correct 5 ms 5228 KB n=100
12 Correct 4 ms 5228 KB n=100
13 Correct 4 ms 5228 KB n=100
14 Correct 4 ms 5228 KB n=100
15 Correct 4 ms 5228 KB n=100
16 Correct 4 ms 5228 KB n=100
17 Correct 4 ms 5228 KB n=100
18 Correct 4 ms 5228 KB n=100
19 Correct 5 ms 5228 KB n=100
20 Correct 4 ms 5228 KB n=100
21 Correct 4 ms 5228 KB n=100
22 Correct 4 ms 5228 KB n=100
23 Correct 5 ms 5228 KB n=100
24 Correct 5 ms 5228 KB n=100
25 Correct 5 ms 5376 KB n=100
26 Correct 4 ms 5228 KB n=12
27 Correct 5 ms 5228 KB n=100
28 Correct 5 ms 5228 KB n=500
29 Correct 6 ms 5228 KB n=500
30 Correct 7 ms 5228 KB n=500
31 Correct 8 ms 5228 KB n=500
32 Correct 5 ms 5228 KB n=500
33 Correct 7 ms 5228 KB n=500
34 Correct 5 ms 5228 KB n=500
35 Correct 7 ms 5228 KB n=500
36 Correct 34 ms 5228 KB n=500
37 Correct 42 ms 5324 KB n=500
38 Correct 42 ms 5228 KB n=500
39 Correct 24 ms 5228 KB n=500
40 Correct 20 ms 5228 KB n=500
41 Correct 19 ms 5228 KB n=500
42 Correct 21 ms 5228 KB n=500
43 Correct 21 ms 5228 KB n=500
44 Correct 20 ms 5228 KB n=500
45 Correct 5 ms 5228 KB n=500
46 Correct 7 ms 5228 KB n=500
47 Correct 7 ms 5228 KB n=500
48 Correct 4 ms 5228 KB n=500
49 Correct 7 ms 5228 KB n=500
50 Correct 10 ms 5228 KB n=500
51 Correct 14 ms 5228 KB n=500
52 Correct 16 ms 5228 KB n=500
53 Correct 14 ms 5228 KB n=500
54 Correct 15 ms 5228 KB n=500
55 Correct 4 ms 5228 KB n=278
56 Correct 23 ms 5228 KB n=500
57 Correct 23 ms 5228 KB n=500
58 Correct 32 ms 5376 KB n=500
59 Correct 10 ms 5612 KB n=2000
60 Correct 54 ms 5744 KB n=2000
61 Correct 56 ms 5612 KB n=2000
62 Correct 53 ms 5740 KB n=2000
63 Correct 9 ms 5484 KB n=2000
64 Correct 60 ms 5760 KB n=2000
65 Correct 10 ms 5484 KB n=2000
66 Correct 65 ms 5612 KB n=2000
67 Correct 15 ms 5484 KB n=2000
68 Correct 61 ms 5740 KB n=2000
69 Correct 566 ms 5740 KB n=2000
70 Correct 611 ms 5612 KB n=2000
71 Correct 579 ms 5484 KB n=2000
72 Correct 292 ms 5740 KB n=2000
73 Correct 289 ms 5740 KB n=2000
74 Correct 8 ms 5484 KB n=1844
75 Correct 299 ms 5580 KB n=2000
76 Correct 313 ms 5612 KB n=2000
77 Correct 320 ms 5612 KB n=2000
78 Correct 330 ms 5484 KB n=2000
79 Correct 8 ms 5484 KB n=2000
80 Correct 53 ms 5612 KB n=2000
81 Correct 54 ms 5612 KB n=2000
82 Correct 8 ms 5484 KB n=2000
83 Correct 50 ms 5612 KB n=2000
84 Correct 115 ms 5612 KB n=2000
85 Correct 166 ms 5760 KB n=2000
86 Correct 170 ms 5612 KB n=2000
87 Correct 130 ms 5592 KB n=2000
88 Correct 382 ms 5760 KB n=2000
89 Correct 371 ms 5704 KB n=2000
90 Correct 203 ms 5760 KB n=2000
91 Correct 497 ms 5612 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5228 KB n=5
2 Correct 5 ms 5228 KB n=100
3 Correct 5 ms 5228 KB n=100
4 Correct 4 ms 5228 KB n=100
5 Correct 4 ms 5228 KB n=100
6 Correct 5 ms 5228 KB n=100
7 Correct 8 ms 5228 KB n=100
8 Correct 4 ms 5228 KB n=100
9 Correct 4 ms 5228 KB n=100
10 Correct 4 ms 5228 KB n=100
11 Correct 5 ms 5228 KB n=100
12 Correct 4 ms 5228 KB n=100
13 Correct 4 ms 5228 KB n=100
14 Correct 4 ms 5228 KB n=100
15 Correct 4 ms 5228 KB n=100
16 Correct 4 ms 5228 KB n=100
17 Correct 4 ms 5228 KB n=100
18 Correct 4 ms 5228 KB n=100
19 Correct 5 ms 5228 KB n=100
20 Correct 4 ms 5228 KB n=100
21 Correct 4 ms 5228 KB n=100
22 Correct 4 ms 5228 KB n=100
23 Correct 5 ms 5228 KB n=100
24 Correct 5 ms 5228 KB n=100
25 Correct 5 ms 5376 KB n=100
26 Correct 4 ms 5228 KB n=12
27 Correct 5 ms 5228 KB n=100
28 Correct 5 ms 5228 KB n=500
29 Correct 6 ms 5228 KB n=500
30 Correct 7 ms 5228 KB n=500
31 Correct 8 ms 5228 KB n=500
32 Correct 5 ms 5228 KB n=500
33 Correct 7 ms 5228 KB n=500
34 Correct 5 ms 5228 KB n=500
35 Correct 7 ms 5228 KB n=500
36 Correct 34 ms 5228 KB n=500
37 Correct 42 ms 5324 KB n=500
38 Correct 42 ms 5228 KB n=500
39 Correct 24 ms 5228 KB n=500
40 Correct 20 ms 5228 KB n=500
41 Correct 19 ms 5228 KB n=500
42 Correct 21 ms 5228 KB n=500
43 Correct 21 ms 5228 KB n=500
44 Correct 20 ms 5228 KB n=500
45 Correct 5 ms 5228 KB n=500
46 Correct 7 ms 5228 KB n=500
47 Correct 7 ms 5228 KB n=500
48 Correct 4 ms 5228 KB n=500
49 Correct 7 ms 5228 KB n=500
50 Correct 10 ms 5228 KB n=500
51 Correct 14 ms 5228 KB n=500
52 Correct 16 ms 5228 KB n=500
53 Correct 14 ms 5228 KB n=500
54 Correct 15 ms 5228 KB n=500
55 Correct 4 ms 5228 KB n=278
56 Correct 23 ms 5228 KB n=500
57 Correct 23 ms 5228 KB n=500
58 Correct 32 ms 5376 KB n=500
59 Correct 10 ms 5612 KB n=2000
60 Correct 54 ms 5744 KB n=2000
61 Correct 56 ms 5612 KB n=2000
62 Correct 53 ms 5740 KB n=2000
63 Correct 9 ms 5484 KB n=2000
64 Correct 60 ms 5760 KB n=2000
65 Correct 10 ms 5484 KB n=2000
66 Correct 65 ms 5612 KB n=2000
67 Correct 15 ms 5484 KB n=2000
68 Correct 61 ms 5740 KB n=2000
69 Correct 566 ms 5740 KB n=2000
70 Correct 611 ms 5612 KB n=2000
71 Correct 579 ms 5484 KB n=2000
72 Correct 292 ms 5740 KB n=2000
73 Correct 289 ms 5740 KB n=2000
74 Correct 8 ms 5484 KB n=1844
75 Correct 299 ms 5580 KB n=2000
76 Correct 313 ms 5612 KB n=2000
77 Correct 320 ms 5612 KB n=2000
78 Correct 330 ms 5484 KB n=2000
79 Correct 8 ms 5484 KB n=2000
80 Correct 53 ms 5612 KB n=2000
81 Correct 54 ms 5612 KB n=2000
82 Correct 8 ms 5484 KB n=2000
83 Correct 50 ms 5612 KB n=2000
84 Correct 115 ms 5612 KB n=2000
85 Correct 166 ms 5760 KB n=2000
86 Correct 170 ms 5612 KB n=2000
87 Correct 130 ms 5592 KB n=2000
88 Correct 382 ms 5760 KB n=2000
89 Correct 371 ms 5704 KB n=2000
90 Correct 203 ms 5760 KB n=2000
91 Correct 497 ms 5612 KB n=2000
92 Runtime error 518 ms 262148 KB Execution killed with signal 9
93 Halted 0 ms 0 KB -