Submission #41554

# Submission time Handle Problem Language Result Execution time Memory
41554 2018-02-18T21:07:44 Z Pajaraja Birthday gift (IZhO18_treearray) C++14
100 / 100
1479 ms 262144 KB
#include <bits/stdc++.h>
#define MAXN 200007
#define MAXL 22
using namespace std;
vector<int> g[MAXN];
multiset<int> k[MAXN],km[MAXN];
multiset<int>::iterator it;
int p[MAXL][MAXN],a[MAXN],b[MAXN],in[MAXN],out[MAXN],n,cnt;
void dfs(int s,int f)
{
	p[0][s]=f;
	in[s]=cnt++;
	for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) dfs(g[s][i],s);
	out[s]=cnt++;
}
bool insub(int x,int y) {return ((in[x]<=in[y])&&(out[x]>=out[y]));}
void lcainit()
{
	dfs(1,0);
	in[0]=-1;
	out[0]=2*MAXN;
	for(int i=1;i<MAXL;i++) for(int j=1;j<=n;j++) p[i][j]=p[i-1][p[i-1][j]];
}
int lca(int x,int y)
{
	if(insub(x,y)) return x;
	if(insub(y,x)) return y;
	for(int i=MAXL-1;i>=0;i--) if(!insub(p[i][x],y)) x=p[i][x];
	return p[0][x];
}
int main()
{
	int m,q;
	scanf("%d%d%d",&n,&m,&q);
	for(int i=0;i<n-1;i++)
	{
		int t1,t2;
		scanf("%d%d",&t1,&t2);
		g[t1].push_back(t2);
		g[t2].push_back(t1);
	}
	lcainit();
	for(int i=1;i<=m;i++) scanf("%d",&a[i]);
	for(int i=1;i<m;i++) b[i]=lca(a[i],a[i+1]);
	for(int i=1;i<=m;i++) k[a[i]].insert(i);
	for(int i=1;i<m;i++) km[b[i]].insert(i);
	while(q--)
	{
		int t;
		scanf("%d",&t);
		if(t==1)
		{
			int t1,t2;
			scanf("%d%d",&t1,&t2);
			if(t1>1)
			{
				int u=lca(t2,a[t1-1]);
				km[b[t1-1]].erase(t1-1);	
				km[u].insert(t1-1);
				b[t1-1]=u;
			}
			if(t1<m)
			{
				int u=lca(t2,a[t1+1]);
				km[b[t1]].erase(t1);	
				km[u].insert(t1);
				b[t1]=u;
			}
			k[a[t1]].erase(t1);
			a[t1]=t2;
			k[a[t1]].insert(t1);
		}
		else
		{
			int l,r,u;
			scanf("%d%d%d",&l,&r,&u);
			it=k[u].lower_bound(l);
			if(it!=k[u].end() && *it<=r) {printf("%d %d\n",*it,*it); continue;}
			it=km[u].lower_bound(l);
			if(it!=km[u].end() && *it<r) {printf("%d %d\n",*it,*it+1); continue;}
			printf("-1 -1\n");
		}
	}
}

Compilation message

treearray.cpp: In function 'void dfs(int, int)':
treearray.cpp:13:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) dfs(g[s][i],s);
               ^
treearray.cpp: In function 'int main()':
treearray.cpp:34:26: 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:38:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d",&t1,&t2);
                        ^
treearray.cpp:43:41: 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:50:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&t);
                 ^
treearray.cpp:54:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d%d",&t1,&t2);
                         ^
treearray.cpp:76:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d%d%d",&l,&r,&u);
                            ^
# Verdict Execution time Memory Grader output
1 Correct 21 ms 23928 KB n=5
2 Correct 21 ms 24032 KB n=100
3 Correct 21 ms 24068 KB n=100
4 Correct 22 ms 24172 KB n=100
5 Correct 23 ms 24172 KB n=100
6 Correct 23 ms 24172 KB n=100
7 Correct 27 ms 24212 KB n=100
8 Correct 23 ms 24212 KB n=100
9 Correct 20 ms 24212 KB n=100
10 Correct 20 ms 24212 KB n=100
11 Correct 26 ms 24212 KB n=100
12 Correct 21 ms 24212 KB n=100
13 Correct 20 ms 24212 KB n=100
14 Correct 22 ms 24212 KB n=100
15 Correct 21 ms 24212 KB n=100
16 Correct 21 ms 24212 KB n=100
17 Correct 21 ms 24212 KB n=100
18 Correct 20 ms 24212 KB n=100
19 Correct 20 ms 24212 KB n=100
20 Correct 20 ms 24212 KB n=100
21 Correct 21 ms 24212 KB n=100
22 Correct 20 ms 24212 KB n=100
23 Correct 22 ms 24212 KB n=100
24 Correct 22 ms 24212 KB n=100
25 Correct 20 ms 24236 KB n=100
26 Correct 20 ms 24240 KB n=12
27 Correct 22 ms 24372 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 21 ms 23928 KB n=5
2 Correct 21 ms 24032 KB n=100
3 Correct 21 ms 24068 KB n=100
4 Correct 22 ms 24172 KB n=100
5 Correct 23 ms 24172 KB n=100
6 Correct 23 ms 24172 KB n=100
7 Correct 27 ms 24212 KB n=100
8 Correct 23 ms 24212 KB n=100
9 Correct 20 ms 24212 KB n=100
10 Correct 20 ms 24212 KB n=100
11 Correct 26 ms 24212 KB n=100
12 Correct 21 ms 24212 KB n=100
13 Correct 20 ms 24212 KB n=100
14 Correct 22 ms 24212 KB n=100
15 Correct 21 ms 24212 KB n=100
16 Correct 21 ms 24212 KB n=100
17 Correct 21 ms 24212 KB n=100
18 Correct 20 ms 24212 KB n=100
19 Correct 20 ms 24212 KB n=100
20 Correct 20 ms 24212 KB n=100
21 Correct 21 ms 24212 KB n=100
22 Correct 20 ms 24212 KB n=100
23 Correct 22 ms 24212 KB n=100
24 Correct 22 ms 24212 KB n=100
25 Correct 20 ms 24236 KB n=100
26 Correct 20 ms 24240 KB n=12
27 Correct 22 ms 24372 KB n=100
28 Correct 21 ms 24408 KB n=500
29 Correct 21 ms 24420 KB n=500
30 Correct 21 ms 24452 KB n=500
31 Correct 22 ms 24484 KB n=500
32 Correct 24 ms 24500 KB n=500
33 Correct 23 ms 24512 KB n=500
34 Correct 21 ms 24528 KB n=500
35 Correct 21 ms 24640 KB n=500
36 Correct 21 ms 24640 KB n=500
37 Correct 21 ms 24640 KB n=500
38 Correct 20 ms 24640 KB n=500
39 Correct 23 ms 24640 KB n=500
40 Correct 21 ms 24640 KB n=500
41 Correct 21 ms 24656 KB n=500
42 Correct 22 ms 24800 KB n=500
43 Correct 20 ms 24800 KB n=500
44 Correct 22 ms 24800 KB n=500
45 Correct 22 ms 24800 KB n=500
46 Correct 22 ms 24800 KB n=500
47 Correct 21 ms 24800 KB n=500
48 Correct 20 ms 24800 KB n=500
49 Correct 21 ms 24800 KB n=500
50 Correct 28 ms 24800 KB n=500
51 Correct 21 ms 24800 KB n=500
52 Correct 21 ms 24852 KB n=500
53 Correct 21 ms 24852 KB n=500
54 Correct 25 ms 24852 KB n=500
55 Correct 21 ms 24852 KB n=278
56 Correct 20 ms 24860 KB n=500
57 Correct 20 ms 24872 KB n=500
58 Correct 21 ms 24884 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 21 ms 23928 KB n=5
2 Correct 21 ms 24032 KB n=100
3 Correct 21 ms 24068 KB n=100
4 Correct 22 ms 24172 KB n=100
5 Correct 23 ms 24172 KB n=100
6 Correct 23 ms 24172 KB n=100
7 Correct 27 ms 24212 KB n=100
8 Correct 23 ms 24212 KB n=100
9 Correct 20 ms 24212 KB n=100
10 Correct 20 ms 24212 KB n=100
11 Correct 26 ms 24212 KB n=100
12 Correct 21 ms 24212 KB n=100
13 Correct 20 ms 24212 KB n=100
14 Correct 22 ms 24212 KB n=100
15 Correct 21 ms 24212 KB n=100
16 Correct 21 ms 24212 KB n=100
17 Correct 21 ms 24212 KB n=100
18 Correct 20 ms 24212 KB n=100
19 Correct 20 ms 24212 KB n=100
20 Correct 20 ms 24212 KB n=100
21 Correct 21 ms 24212 KB n=100
22 Correct 20 ms 24212 KB n=100
23 Correct 22 ms 24212 KB n=100
24 Correct 22 ms 24212 KB n=100
25 Correct 20 ms 24236 KB n=100
26 Correct 20 ms 24240 KB n=12
27 Correct 22 ms 24372 KB n=100
28 Correct 21 ms 24408 KB n=500
29 Correct 21 ms 24420 KB n=500
30 Correct 21 ms 24452 KB n=500
31 Correct 22 ms 24484 KB n=500
32 Correct 24 ms 24500 KB n=500
33 Correct 23 ms 24512 KB n=500
34 Correct 21 ms 24528 KB n=500
35 Correct 21 ms 24640 KB n=500
36 Correct 21 ms 24640 KB n=500
37 Correct 21 ms 24640 KB n=500
38 Correct 20 ms 24640 KB n=500
39 Correct 23 ms 24640 KB n=500
40 Correct 21 ms 24640 KB n=500
41 Correct 21 ms 24656 KB n=500
42 Correct 22 ms 24800 KB n=500
43 Correct 20 ms 24800 KB n=500
44 Correct 22 ms 24800 KB n=500
45 Correct 22 ms 24800 KB n=500
46 Correct 22 ms 24800 KB n=500
47 Correct 21 ms 24800 KB n=500
48 Correct 20 ms 24800 KB n=500
49 Correct 21 ms 24800 KB n=500
50 Correct 28 ms 24800 KB n=500
51 Correct 21 ms 24800 KB n=500
52 Correct 21 ms 24852 KB n=500
53 Correct 21 ms 24852 KB n=500
54 Correct 25 ms 24852 KB n=500
55 Correct 21 ms 24852 KB n=278
56 Correct 20 ms 24860 KB n=500
57 Correct 20 ms 24872 KB n=500
58 Correct 21 ms 24884 KB n=500
59 Correct 25 ms 25284 KB n=2000
60 Correct 25 ms 25444 KB n=2000
61 Correct 24 ms 25520 KB n=2000
62 Correct 25 ms 25576 KB n=2000
63 Correct 25 ms 25576 KB n=2000
64 Correct 24 ms 25576 KB n=2000
65 Correct 25 ms 25612 KB n=2000
66 Correct 25 ms 25792 KB n=2000
67 Correct 26 ms 25792 KB n=2000
68 Correct 25 ms 25904 KB n=2000
69 Correct 23 ms 25904 KB n=2000
70 Correct 25 ms 25904 KB n=2000
71 Correct 26 ms 25936 KB n=2000
72 Correct 24 ms 26092 KB n=2000
73 Correct 23 ms 26092 KB n=2000
74 Correct 24 ms 26104 KB n=1844
75 Correct 24 ms 26148 KB n=2000
76 Correct 25 ms 26204 KB n=2000
77 Correct 26 ms 26256 KB n=2000
78 Correct 25 ms 26308 KB n=2000
79 Correct 25 ms 26488 KB n=2000
80 Correct 25 ms 26536 KB n=2000
81 Correct 24 ms 26536 KB n=2000
82 Correct 25 ms 26536 KB n=2000
83 Correct 27 ms 26696 KB n=2000
84 Correct 26 ms 26696 KB n=2000
85 Correct 25 ms 26696 KB n=2000
86 Correct 25 ms 26732 KB n=2000
87 Correct 26 ms 26788 KB n=2000
88 Correct 25 ms 26968 KB n=2000
89 Correct 24 ms 26992 KB n=2000
90 Correct 25 ms 27080 KB n=2000
91 Correct 23 ms 27080 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 21 ms 23928 KB n=5
2 Correct 21 ms 24032 KB n=100
3 Correct 21 ms 24068 KB n=100
4 Correct 22 ms 24172 KB n=100
5 Correct 23 ms 24172 KB n=100
6 Correct 23 ms 24172 KB n=100
7 Correct 27 ms 24212 KB n=100
8 Correct 23 ms 24212 KB n=100
9 Correct 20 ms 24212 KB n=100
10 Correct 20 ms 24212 KB n=100
11 Correct 26 ms 24212 KB n=100
12 Correct 21 ms 24212 KB n=100
13 Correct 20 ms 24212 KB n=100
14 Correct 22 ms 24212 KB n=100
15 Correct 21 ms 24212 KB n=100
16 Correct 21 ms 24212 KB n=100
17 Correct 21 ms 24212 KB n=100
18 Correct 20 ms 24212 KB n=100
19 Correct 20 ms 24212 KB n=100
20 Correct 20 ms 24212 KB n=100
21 Correct 21 ms 24212 KB n=100
22 Correct 20 ms 24212 KB n=100
23 Correct 22 ms 24212 KB n=100
24 Correct 22 ms 24212 KB n=100
25 Correct 20 ms 24236 KB n=100
26 Correct 20 ms 24240 KB n=12
27 Correct 22 ms 24372 KB n=100
28 Correct 21 ms 24408 KB n=500
29 Correct 21 ms 24420 KB n=500
30 Correct 21 ms 24452 KB n=500
31 Correct 22 ms 24484 KB n=500
32 Correct 24 ms 24500 KB n=500
33 Correct 23 ms 24512 KB n=500
34 Correct 21 ms 24528 KB n=500
35 Correct 21 ms 24640 KB n=500
36 Correct 21 ms 24640 KB n=500
37 Correct 21 ms 24640 KB n=500
38 Correct 20 ms 24640 KB n=500
39 Correct 23 ms 24640 KB n=500
40 Correct 21 ms 24640 KB n=500
41 Correct 21 ms 24656 KB n=500
42 Correct 22 ms 24800 KB n=500
43 Correct 20 ms 24800 KB n=500
44 Correct 22 ms 24800 KB n=500
45 Correct 22 ms 24800 KB n=500
46 Correct 22 ms 24800 KB n=500
47 Correct 21 ms 24800 KB n=500
48 Correct 20 ms 24800 KB n=500
49 Correct 21 ms 24800 KB n=500
50 Correct 28 ms 24800 KB n=500
51 Correct 21 ms 24800 KB n=500
52 Correct 21 ms 24852 KB n=500
53 Correct 21 ms 24852 KB n=500
54 Correct 25 ms 24852 KB n=500
55 Correct 21 ms 24852 KB n=278
56 Correct 20 ms 24860 KB n=500
57 Correct 20 ms 24872 KB n=500
58 Correct 21 ms 24884 KB n=500
59 Correct 25 ms 25284 KB n=2000
60 Correct 25 ms 25444 KB n=2000
61 Correct 24 ms 25520 KB n=2000
62 Correct 25 ms 25576 KB n=2000
63 Correct 25 ms 25576 KB n=2000
64 Correct 24 ms 25576 KB n=2000
65 Correct 25 ms 25612 KB n=2000
66 Correct 25 ms 25792 KB n=2000
67 Correct 26 ms 25792 KB n=2000
68 Correct 25 ms 25904 KB n=2000
69 Correct 23 ms 25904 KB n=2000
70 Correct 25 ms 25904 KB n=2000
71 Correct 26 ms 25936 KB n=2000
72 Correct 24 ms 26092 KB n=2000
73 Correct 23 ms 26092 KB n=2000
74 Correct 24 ms 26104 KB n=1844
75 Correct 24 ms 26148 KB n=2000
76 Correct 25 ms 26204 KB n=2000
77 Correct 26 ms 26256 KB n=2000
78 Correct 25 ms 26308 KB n=2000
79 Correct 25 ms 26488 KB n=2000
80 Correct 25 ms 26536 KB n=2000
81 Correct 24 ms 26536 KB n=2000
82 Correct 25 ms 26536 KB n=2000
83 Correct 27 ms 26696 KB n=2000
84 Correct 26 ms 26696 KB n=2000
85 Correct 25 ms 26696 KB n=2000
86 Correct 25 ms 26732 KB n=2000
87 Correct 26 ms 26788 KB n=2000
88 Correct 25 ms 26968 KB n=2000
89 Correct 24 ms 26992 KB n=2000
90 Correct 25 ms 27080 KB n=2000
91 Correct 23 ms 27080 KB n=2000
92 Correct 1365 ms 80496 KB n=200000
93 Correct 1111 ms 93260 KB n=200000
94 Correct 895 ms 105484 KB n=200000
95 Correct 1461 ms 105484 KB n=200000
96 Correct 1479 ms 108616 KB n=200000
97 Correct 1331 ms 120460 KB n=200000
98 Correct 1370 ms 122848 KB n=200000
99 Correct 1401 ms 129484 KB n=200000
100 Correct 1385 ms 136540 KB n=200000
101 Correct 706 ms 155524 KB n=200000
102 Correct 755 ms 155524 KB n=200000
103 Correct 758 ms 158324 KB n=200000
104 Correct 798 ms 165136 KB n=200000
105 Correct 776 ms 172284 KB n=200000
106 Correct 762 ms 179636 KB n=200000
107 Correct 806 ms 186920 KB n=200000
108 Correct 1353 ms 192804 KB n=200000
109 Correct 1292 ms 199712 KB n=200000
110 Correct 1332 ms 206456 KB n=200000
111 Correct 1180 ms 212872 KB n=200000
112 Correct 771 ms 230828 KB n=200000
113 Correct 1126 ms 232540 KB n=200000
114 Correct 1062 ms 234292 KB n=200000
115 Correct 1407 ms 243392 KB n=200000
116 Correct 1288 ms 248676 KB n=200000
117 Correct 691 ms 262144 KB n=200000
118 Correct 1293 ms 262144 KB n=200000
119 Correct 1390 ms 262144 KB n=200000
120 Correct 651 ms 262144 KB n=200000
121 Correct 641 ms 262144 KB n=200000
122 Correct 629 ms 262144 KB n=200000
123 Correct 754 ms 262144 KB n=200000
124 Correct 218 ms 262144 KB n=25264