답안 #219826

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
219826 2020-04-06T12:59:51 Z XmtosX Birthday gift (IZhO18_treearray) C++17
100 / 100
1564 ms 72596 KB
#include <bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int n,m,q,sprs[N][20],a[N],lvl[N];
vector <int> v[N];
set<int> s[N];
void dfs (int x,int p)
{
    sprs[x][0]=p;
    lvl[x]=lvl[p]+1;
    for (int i=1;i<20;i++)
        sprs[x][i]=sprs[sprs[x][i-1]][i-1];
    for (int i=0;i<v[x].size();i++)
    {
        if (v[x][i]!=p)
            dfs (v[x][i],x);
    }
}
int lca (int x,int y)
{
    if (lvl[x]<lvl[y])
        swap(x,y);
    for (int i=19;i>=0;i--)
    {
        if (lvl[sprs[x][i]]>=lvl[y])
            x=sprs[x][i];
    }
    if (x==y)
        return x;
    for (int i=19;i>=0;i--)
    {
        if (sprs[x][i]!=sprs[y][i])
            x=sprs[x][i],y=sprs[y][i];
    }
    return sprs[x][0];
}
int main()
{
    scanf("%d%d%d",&n,&m,&q);
    for (int i=0;i<n-1;i++)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        v[a].push_back(b);
        v[b].push_back(a);
    }
    dfs(1,0);
    for (int i=1;i<=m;i++)
    {
        scanf("%d",&a[i]);
        s[a[i]].insert(i);
        if (i>1)
        {
            int x= lca(a[i],a[i-1]);
            s[x].insert(i-1);
        }
    }
    for (int i=1;i<=n;i++)
        s[i].insert(m+1);
    while (q--)
    {
        int t;
        scanf("%d",&t);
        if (t==1)
        {
            int pos,x;
            scanf("%d%d",&pos,&x);
            s[a[pos]].erase(pos);
            if (pos>1)
            {
                s[lca(a[pos],a[pos-1])].erase(pos-1);
            }
            if (pos<m)
            {
                s[lca(a[pos],a[pos+1])].erase(pos);
            }
            a[pos]=x;
            s[a[pos]].insert(pos);
            if (pos>1)
            {
                s[lca(a[pos],a[pos-1]   )].insert(pos-1);
                s[a[pos-1]].insert(pos-1);
            }
            if (pos<m)
            {
                s[lca(a[pos],a[pos+1])].insert(pos);
                s[a[pos+1]].insert(pos+1);
            }
        }
        else
        {
            int l,r,x;
            scanf("%d%d%d",&l,&r,&x);
            int idx= *s[x].lower_bound(l);
            int o=0;
            if (a[idx]!=x)
                r--,o++;
            if (idx<=r)
                printf("%d %d\n",idx,idx+o);
            else
                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 'void dfs(int, int)':
treearray.cpp:13:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i=0;i<v[x].size();i++)
                  ~^~~~~~~~~~~~
treearray.cpp: In function 'int main()':
treearray.cpp:39: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:43:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d",&a,&b);
         ~~~~~^~~~~~~~~~~~~~
treearray.cpp:50:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d",&a[i]);
         ~~~~~^~~~~~~~~~~~
treearray.cpp:63:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d",&t);
         ~~~~~^~~~~~~~~
treearray.cpp:67:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d%d",&pos,&x);
             ~~~~~^~~~~~~~~~~~~~~~
treearray.cpp:93:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d%d%d",&l,&r,&x);
             ~~~~~^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 14464 KB n=5
2 Correct 13 ms 14464 KB n=100
3 Correct 13 ms 14464 KB n=100
4 Correct 13 ms 14464 KB n=100
5 Correct 14 ms 14464 KB n=100
6 Correct 13 ms 14464 KB n=100
7 Correct 14 ms 14464 KB n=100
8 Correct 13 ms 14464 KB n=100
9 Correct 14 ms 14464 KB n=100
10 Correct 14 ms 14464 KB n=100
11 Correct 13 ms 14464 KB n=100
12 Correct 13 ms 14464 KB n=100
13 Correct 14 ms 14464 KB n=100
14 Correct 13 ms 14464 KB n=100
15 Correct 13 ms 14464 KB n=100
16 Correct 13 ms 14464 KB n=100
17 Correct 14 ms 14464 KB n=100
18 Correct 14 ms 14464 KB n=100
19 Correct 13 ms 14464 KB n=100
20 Correct 13 ms 14464 KB n=100
21 Correct 13 ms 14464 KB n=100
22 Correct 13 ms 14464 KB n=100
23 Correct 14 ms 14464 KB n=100
24 Correct 13 ms 14464 KB n=100
25 Correct 14 ms 14464 KB n=100
26 Correct 13 ms 14464 KB n=12
27 Correct 13 ms 14464 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 14464 KB n=5
2 Correct 13 ms 14464 KB n=100
3 Correct 13 ms 14464 KB n=100
4 Correct 13 ms 14464 KB n=100
5 Correct 14 ms 14464 KB n=100
6 Correct 13 ms 14464 KB n=100
7 Correct 14 ms 14464 KB n=100
8 Correct 13 ms 14464 KB n=100
9 Correct 14 ms 14464 KB n=100
10 Correct 14 ms 14464 KB n=100
11 Correct 13 ms 14464 KB n=100
12 Correct 13 ms 14464 KB n=100
13 Correct 14 ms 14464 KB n=100
14 Correct 13 ms 14464 KB n=100
15 Correct 13 ms 14464 KB n=100
16 Correct 13 ms 14464 KB n=100
17 Correct 14 ms 14464 KB n=100
18 Correct 14 ms 14464 KB n=100
19 Correct 13 ms 14464 KB n=100
20 Correct 13 ms 14464 KB n=100
21 Correct 13 ms 14464 KB n=100
22 Correct 13 ms 14464 KB n=100
23 Correct 14 ms 14464 KB n=100
24 Correct 13 ms 14464 KB n=100
25 Correct 14 ms 14464 KB n=100
26 Correct 13 ms 14464 KB n=12
27 Correct 13 ms 14464 KB n=100
28 Correct 32 ms 14592 KB n=500
29 Correct 16 ms 14720 KB n=500
30 Correct 14 ms 14592 KB n=500
31 Correct 14 ms 14592 KB n=500
32 Correct 15 ms 14592 KB n=500
33 Correct 14 ms 14592 KB n=500
34 Correct 15 ms 14592 KB n=500
35 Correct 14 ms 14592 KB n=500
36 Correct 14 ms 14592 KB n=500
37 Correct 14 ms 14592 KB n=500
38 Correct 14 ms 14592 KB n=500
39 Correct 14 ms 14592 KB n=500
40 Correct 14 ms 14592 KB n=500
41 Correct 14 ms 14592 KB n=500
42 Correct 15 ms 14592 KB n=500
43 Correct 14 ms 14592 KB n=500
44 Correct 15 ms 14592 KB n=500
45 Correct 15 ms 14592 KB n=500
46 Correct 14 ms 14592 KB n=500
47 Correct 14 ms 14592 KB n=500
48 Correct 15 ms 14592 KB n=500
49 Correct 14 ms 14592 KB n=500
50 Correct 14 ms 14592 KB n=500
51 Correct 14 ms 14592 KB n=500
52 Correct 14 ms 14592 KB n=500
53 Correct 14 ms 14592 KB n=500
54 Correct 14 ms 14592 KB n=500
55 Correct 13 ms 14464 KB n=278
56 Correct 14 ms 14592 KB n=500
57 Correct 14 ms 14592 KB n=500
58 Correct 13 ms 14592 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 14464 KB n=5
2 Correct 13 ms 14464 KB n=100
3 Correct 13 ms 14464 KB n=100
4 Correct 13 ms 14464 KB n=100
5 Correct 14 ms 14464 KB n=100
6 Correct 13 ms 14464 KB n=100
7 Correct 14 ms 14464 KB n=100
8 Correct 13 ms 14464 KB n=100
9 Correct 14 ms 14464 KB n=100
10 Correct 14 ms 14464 KB n=100
11 Correct 13 ms 14464 KB n=100
12 Correct 13 ms 14464 KB n=100
13 Correct 14 ms 14464 KB n=100
14 Correct 13 ms 14464 KB n=100
15 Correct 13 ms 14464 KB n=100
16 Correct 13 ms 14464 KB n=100
17 Correct 14 ms 14464 KB n=100
18 Correct 14 ms 14464 KB n=100
19 Correct 13 ms 14464 KB n=100
20 Correct 13 ms 14464 KB n=100
21 Correct 13 ms 14464 KB n=100
22 Correct 13 ms 14464 KB n=100
23 Correct 14 ms 14464 KB n=100
24 Correct 13 ms 14464 KB n=100
25 Correct 14 ms 14464 KB n=100
26 Correct 13 ms 14464 KB n=12
27 Correct 13 ms 14464 KB n=100
28 Correct 32 ms 14592 KB n=500
29 Correct 16 ms 14720 KB n=500
30 Correct 14 ms 14592 KB n=500
31 Correct 14 ms 14592 KB n=500
32 Correct 15 ms 14592 KB n=500
33 Correct 14 ms 14592 KB n=500
34 Correct 15 ms 14592 KB n=500
35 Correct 14 ms 14592 KB n=500
36 Correct 14 ms 14592 KB n=500
37 Correct 14 ms 14592 KB n=500
38 Correct 14 ms 14592 KB n=500
39 Correct 14 ms 14592 KB n=500
40 Correct 14 ms 14592 KB n=500
41 Correct 14 ms 14592 KB n=500
42 Correct 15 ms 14592 KB n=500
43 Correct 14 ms 14592 KB n=500
44 Correct 15 ms 14592 KB n=500
45 Correct 15 ms 14592 KB n=500
46 Correct 14 ms 14592 KB n=500
47 Correct 14 ms 14592 KB n=500
48 Correct 15 ms 14592 KB n=500
49 Correct 14 ms 14592 KB n=500
50 Correct 14 ms 14592 KB n=500
51 Correct 14 ms 14592 KB n=500
52 Correct 14 ms 14592 KB n=500
53 Correct 14 ms 14592 KB n=500
54 Correct 14 ms 14592 KB n=500
55 Correct 13 ms 14464 KB n=278
56 Correct 14 ms 14592 KB n=500
57 Correct 14 ms 14592 KB n=500
58 Correct 13 ms 14592 KB n=500
59 Correct 18 ms 14976 KB n=2000
60 Correct 18 ms 15104 KB n=2000
61 Correct 18 ms 15232 KB n=2000
62 Correct 18 ms 14976 KB n=2000
63 Correct 18 ms 14976 KB n=2000
64 Correct 19 ms 14976 KB n=2000
65 Correct 18 ms 14976 KB n=2000
66 Correct 18 ms 14976 KB n=2000
67 Correct 19 ms 14976 KB n=2000
68 Correct 18 ms 15104 KB n=2000
69 Correct 17 ms 14968 KB n=2000
70 Correct 17 ms 14976 KB n=2000
71 Correct 17 ms 14976 KB n=2000
72 Correct 16 ms 14976 KB n=2000
73 Correct 17 ms 14976 KB n=2000
74 Correct 17 ms 14976 KB n=1844
75 Correct 17 ms 15008 KB n=2000
76 Correct 18 ms 14976 KB n=2000
77 Correct 18 ms 14976 KB n=2000
78 Correct 19 ms 14976 KB n=2000
79 Correct 19 ms 14976 KB n=2000
80 Correct 18 ms 14976 KB n=2000
81 Correct 19 ms 15104 KB n=2000
82 Correct 18 ms 14976 KB n=2000
83 Correct 22 ms 14976 KB n=2000
84 Correct 18 ms 14976 KB n=2000
85 Correct 18 ms 14976 KB n=2000
86 Correct 19 ms 14976 KB n=2000
87 Correct 18 ms 14976 KB n=2000
88 Correct 17 ms 14976 KB n=2000
89 Correct 17 ms 14976 KB n=2000
90 Correct 18 ms 14976 KB n=2000
91 Correct 17 ms 14976 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 14464 KB n=5
2 Correct 13 ms 14464 KB n=100
3 Correct 13 ms 14464 KB n=100
4 Correct 13 ms 14464 KB n=100
5 Correct 14 ms 14464 KB n=100
6 Correct 13 ms 14464 KB n=100
7 Correct 14 ms 14464 KB n=100
8 Correct 13 ms 14464 KB n=100
9 Correct 14 ms 14464 KB n=100
10 Correct 14 ms 14464 KB n=100
11 Correct 13 ms 14464 KB n=100
12 Correct 13 ms 14464 KB n=100
13 Correct 14 ms 14464 KB n=100
14 Correct 13 ms 14464 KB n=100
15 Correct 13 ms 14464 KB n=100
16 Correct 13 ms 14464 KB n=100
17 Correct 14 ms 14464 KB n=100
18 Correct 14 ms 14464 KB n=100
19 Correct 13 ms 14464 KB n=100
20 Correct 13 ms 14464 KB n=100
21 Correct 13 ms 14464 KB n=100
22 Correct 13 ms 14464 KB n=100
23 Correct 14 ms 14464 KB n=100
24 Correct 13 ms 14464 KB n=100
25 Correct 14 ms 14464 KB n=100
26 Correct 13 ms 14464 KB n=12
27 Correct 13 ms 14464 KB n=100
28 Correct 32 ms 14592 KB n=500
29 Correct 16 ms 14720 KB n=500
30 Correct 14 ms 14592 KB n=500
31 Correct 14 ms 14592 KB n=500
32 Correct 15 ms 14592 KB n=500
33 Correct 14 ms 14592 KB n=500
34 Correct 15 ms 14592 KB n=500
35 Correct 14 ms 14592 KB n=500
36 Correct 14 ms 14592 KB n=500
37 Correct 14 ms 14592 KB n=500
38 Correct 14 ms 14592 KB n=500
39 Correct 14 ms 14592 KB n=500
40 Correct 14 ms 14592 KB n=500
41 Correct 14 ms 14592 KB n=500
42 Correct 15 ms 14592 KB n=500
43 Correct 14 ms 14592 KB n=500
44 Correct 15 ms 14592 KB n=500
45 Correct 15 ms 14592 KB n=500
46 Correct 14 ms 14592 KB n=500
47 Correct 14 ms 14592 KB n=500
48 Correct 15 ms 14592 KB n=500
49 Correct 14 ms 14592 KB n=500
50 Correct 14 ms 14592 KB n=500
51 Correct 14 ms 14592 KB n=500
52 Correct 14 ms 14592 KB n=500
53 Correct 14 ms 14592 KB n=500
54 Correct 14 ms 14592 KB n=500
55 Correct 13 ms 14464 KB n=278
56 Correct 14 ms 14592 KB n=500
57 Correct 14 ms 14592 KB n=500
58 Correct 13 ms 14592 KB n=500
59 Correct 18 ms 14976 KB n=2000
60 Correct 18 ms 15104 KB n=2000
61 Correct 18 ms 15232 KB n=2000
62 Correct 18 ms 14976 KB n=2000
63 Correct 18 ms 14976 KB n=2000
64 Correct 19 ms 14976 KB n=2000
65 Correct 18 ms 14976 KB n=2000
66 Correct 18 ms 14976 KB n=2000
67 Correct 19 ms 14976 KB n=2000
68 Correct 18 ms 15104 KB n=2000
69 Correct 17 ms 14968 KB n=2000
70 Correct 17 ms 14976 KB n=2000
71 Correct 17 ms 14976 KB n=2000
72 Correct 16 ms 14976 KB n=2000
73 Correct 17 ms 14976 KB n=2000
74 Correct 17 ms 14976 KB n=1844
75 Correct 17 ms 15008 KB n=2000
76 Correct 18 ms 14976 KB n=2000
77 Correct 18 ms 14976 KB n=2000
78 Correct 19 ms 14976 KB n=2000
79 Correct 19 ms 14976 KB n=2000
80 Correct 18 ms 14976 KB n=2000
81 Correct 19 ms 15104 KB n=2000
82 Correct 18 ms 14976 KB n=2000
83 Correct 22 ms 14976 KB n=2000
84 Correct 18 ms 14976 KB n=2000
85 Correct 18 ms 14976 KB n=2000
86 Correct 19 ms 14976 KB n=2000
87 Correct 18 ms 14976 KB n=2000
88 Correct 17 ms 14976 KB n=2000
89 Correct 17 ms 14976 KB n=2000
90 Correct 18 ms 14976 KB n=2000
91 Correct 17 ms 14976 KB n=2000
92 Correct 1058 ms 69228 KB n=200000
93 Correct 1433 ms 70264 KB n=200000
94 Correct 1365 ms 72004 KB n=200000
95 Correct 1030 ms 69012 KB n=200000
96 Correct 1035 ms 69156 KB n=200000
97 Correct 1477 ms 69628 KB n=200000
98 Correct 1032 ms 69352 KB n=200000
99 Correct 1317 ms 68344 KB n=200000
100 Correct 1045 ms 68592 KB n=200000
101 Correct 1377 ms 72596 KB n=200000
102 Correct 593 ms 69496 KB n=200000
103 Correct 614 ms 69672 KB n=200000
104 Correct 619 ms 69496 KB n=200000
105 Correct 609 ms 69112 KB n=200000
106 Correct 622 ms 69240 KB n=200000
107 Correct 622 ms 69116 KB n=200000
108 Correct 1159 ms 68216 KB n=200000
109 Correct 1150 ms 68088 KB n=200000
110 Correct 1181 ms 67948 KB n=200000
111 Correct 1052 ms 68200 KB n=200000
112 Correct 1351 ms 71288 KB n=200000
113 Correct 1460 ms 68908 KB n=200000
114 Correct 1046 ms 68328 KB n=200000
115 Correct 1564 ms 68448 KB n=200000
116 Correct 999 ms 68204 KB n=200000
117 Correct 1331 ms 71172 KB n=200000
118 Correct 1487 ms 67832 KB n=200000
119 Correct 997 ms 67868 KB n=200000
120 Correct 1310 ms 71596 KB n=200000
121 Correct 1278 ms 71388 KB n=200000
122 Correct 1264 ms 72056 KB n=200000
123 Correct 622 ms 68344 KB n=200000
124 Correct 278 ms 26744 KB n=25264