Submission #91893

# Submission time Handle Problem Language Result Execution time Memory
91893 2018-12-31T10:07:33 Z emil_physmath Birthday gift (IZhO18_treearray) C++17
56 / 100
85 ms 888 KB
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;
const int MAXN=2005;

int n, l, timer, a[MAXN], tin[MAXN], tout[MAXN];
vector<int> nei[MAXN], up[MAXN];

int lca(int u, int v);
void dfs (int u, int p=1);
bool upper (int u, int v);
int main()
{
	int m, q;
	cin>>n>>m>>q;
	for (int i=1; i<n; i++)
	{
		int u, v;
		scanf("%d%d", &u, &v);
		nei[u].push_back(v);
		nei[v].push_back(u);
	}
	l=1;
	while((1<<l)<=n)
		l++;
	for(int i=1; i<=n; i++)
		up[i].resize(l+1);
	dfs(1);
	for (int i=1; i<=m; i++)
		scanf("%d", a+i);
	int type, pos, L, R, v, ans;
	while (q--)
	{
		scanf("%d", &type);
		if (type==1)
		{
			scanf("%d%d", &pos, &v);
			a[pos]=v;
		}
		else
		{
			scanf("%d%d%d", &L, &R, &v);
			ans=-1;
			for (int i=L; i<R; i++)
				if (lca(a[i], a[i+1])==v)
				{
					ans=i;
					break;
				}
			if (ans==-1)
			{
				for (int i=L; i<=R; i++)
					if (a[i]==v)
					{
						ans=i;
						break;
					}
				if (ans==-1)
					printf("-1 -1\n");
				else
					printf("%d %d\n", ans, ans);
			}
			else
				printf("%d %d\n", ans, ans+1);
		}
	}

	char I;
	cin >> I;
	return 0;
}

void dfs(int u, int p) {
	tin[u]=++timer;
	up[u][0]=p;
	for (int i=1; i<=l; i++)
		up[u][i]=up[up[u][i-1]][i-1];
	for (int i=0; i<nei[u].size(); i++)
		if (nei[u][i]!=p)
			dfs(nei[u][i], u);
	tout[u]=++timer;
}

bool upper(int u, int v) {
	return tin[u]<=tin[v] && tout[u]>=tout[v];
}

int lca(int u, int v) {
	if (upper(u, v))  return u;
	if (upper(v, u))  return v;
	for (int i=l; i>=0; i--)
		if (!upper (up[u][i], v))
			u=up[u][i];
	return up[u][0];
}

Compilation message

treearray.cpp: In function 'void dfs(int, int)':
treearray.cpp:79:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i=0; i<nei[u].size(); i++)
                ~^~~~~~~~~~~~~~
treearray.cpp: In function 'int main()':
treearray.cpp:20:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &u, &v);
   ~~~~~^~~~~~~~~~~~~~~~
treearray.cpp:31:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", a+i);
   ~~~~~^~~~~~~~~~~
treearray.cpp:35:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &type);
   ~~~~~^~~~~~~~~~~~~
treearray.cpp:38:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d%d", &pos, &v);
    ~~~~~^~~~~~~~~~~~~~~~~~
treearray.cpp:43:9: 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 2 ms 376 KB n=5
2 Correct 2 ms 552 KB n=100
3 Correct 2 ms 376 KB n=100
4 Correct 2 ms 376 KB n=100
5 Correct 2 ms 504 KB n=100
6 Correct 2 ms 508 KB n=100
7 Correct 2 ms 504 KB n=100
8 Correct 2 ms 376 KB n=100
9 Correct 2 ms 508 KB n=100
10 Correct 2 ms 504 KB n=100
11 Correct 2 ms 376 KB n=100
12 Correct 2 ms 504 KB n=100
13 Correct 2 ms 504 KB n=100
14 Correct 2 ms 504 KB n=100
15 Correct 2 ms 504 KB n=100
16 Correct 2 ms 376 KB n=100
17 Correct 2 ms 508 KB n=100
18 Correct 2 ms 504 KB n=100
19 Correct 2 ms 376 KB n=100
20 Correct 2 ms 508 KB n=100
21 Correct 2 ms 504 KB n=100
22 Correct 2 ms 376 KB n=100
23 Correct 2 ms 504 KB n=100
24 Correct 2 ms 376 KB n=100
25 Correct 2 ms 376 KB n=100
26 Correct 2 ms 376 KB n=12
27 Correct 2 ms 504 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB n=5
2 Correct 2 ms 552 KB n=100
3 Correct 2 ms 376 KB n=100
4 Correct 2 ms 376 KB n=100
5 Correct 2 ms 504 KB n=100
6 Correct 2 ms 508 KB n=100
7 Correct 2 ms 504 KB n=100
8 Correct 2 ms 376 KB n=100
9 Correct 2 ms 508 KB n=100
10 Correct 2 ms 504 KB n=100
11 Correct 2 ms 376 KB n=100
12 Correct 2 ms 504 KB n=100
13 Correct 2 ms 504 KB n=100
14 Correct 2 ms 504 KB n=100
15 Correct 2 ms 504 KB n=100
16 Correct 2 ms 376 KB n=100
17 Correct 2 ms 508 KB n=100
18 Correct 2 ms 504 KB n=100
19 Correct 2 ms 376 KB n=100
20 Correct 2 ms 508 KB n=100
21 Correct 2 ms 504 KB n=100
22 Correct 2 ms 376 KB n=100
23 Correct 2 ms 504 KB n=100
24 Correct 2 ms 376 KB n=100
25 Correct 2 ms 376 KB n=100
26 Correct 2 ms 376 KB n=12
27 Correct 2 ms 504 KB n=100
28 Correct 2 ms 504 KB n=500
29 Correct 2 ms 504 KB n=500
30 Correct 3 ms 504 KB n=500
31 Correct 2 ms 504 KB n=500
32 Correct 2 ms 504 KB n=500
33 Correct 3 ms 504 KB n=500
34 Correct 2 ms 504 KB n=500
35 Correct 2 ms 504 KB n=500
36 Correct 5 ms 504 KB n=500
37 Correct 5 ms 504 KB n=500
38 Correct 5 ms 504 KB n=500
39 Correct 4 ms 504 KB n=500
40 Correct 4 ms 504 KB n=500
41 Correct 4 ms 504 KB n=500
42 Correct 4 ms 504 KB n=500
43 Correct 4 ms 504 KB n=500
44 Correct 4 ms 504 KB n=500
45 Correct 2 ms 504 KB n=500
46 Correct 2 ms 508 KB n=500
47 Correct 2 ms 504 KB n=500
48 Correct 2 ms 504 KB n=500
49 Correct 3 ms 504 KB n=500
50 Correct 3 ms 504 KB n=500
51 Correct 3 ms 508 KB n=500
52 Correct 2 ms 504 KB n=500
53 Correct 3 ms 504 KB n=500
54 Correct 2 ms 508 KB n=500
55 Correct 2 ms 376 KB n=278
56 Correct 3 ms 504 KB n=500
57 Correct 3 ms 504 KB n=500
58 Correct 5 ms 504 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB n=5
2 Correct 2 ms 552 KB n=100
3 Correct 2 ms 376 KB n=100
4 Correct 2 ms 376 KB n=100
5 Correct 2 ms 504 KB n=100
6 Correct 2 ms 508 KB n=100
7 Correct 2 ms 504 KB n=100
8 Correct 2 ms 376 KB n=100
9 Correct 2 ms 508 KB n=100
10 Correct 2 ms 504 KB n=100
11 Correct 2 ms 376 KB n=100
12 Correct 2 ms 504 KB n=100
13 Correct 2 ms 504 KB n=100
14 Correct 2 ms 504 KB n=100
15 Correct 2 ms 504 KB n=100
16 Correct 2 ms 376 KB n=100
17 Correct 2 ms 508 KB n=100
18 Correct 2 ms 504 KB n=100
19 Correct 2 ms 376 KB n=100
20 Correct 2 ms 508 KB n=100
21 Correct 2 ms 504 KB n=100
22 Correct 2 ms 376 KB n=100
23 Correct 2 ms 504 KB n=100
24 Correct 2 ms 376 KB n=100
25 Correct 2 ms 376 KB n=100
26 Correct 2 ms 376 KB n=12
27 Correct 2 ms 504 KB n=100
28 Correct 2 ms 504 KB n=500
29 Correct 2 ms 504 KB n=500
30 Correct 3 ms 504 KB n=500
31 Correct 2 ms 504 KB n=500
32 Correct 2 ms 504 KB n=500
33 Correct 3 ms 504 KB n=500
34 Correct 2 ms 504 KB n=500
35 Correct 2 ms 504 KB n=500
36 Correct 5 ms 504 KB n=500
37 Correct 5 ms 504 KB n=500
38 Correct 5 ms 504 KB n=500
39 Correct 4 ms 504 KB n=500
40 Correct 4 ms 504 KB n=500
41 Correct 4 ms 504 KB n=500
42 Correct 4 ms 504 KB n=500
43 Correct 4 ms 504 KB n=500
44 Correct 4 ms 504 KB n=500
45 Correct 2 ms 504 KB n=500
46 Correct 2 ms 508 KB n=500
47 Correct 2 ms 504 KB n=500
48 Correct 2 ms 504 KB n=500
49 Correct 3 ms 504 KB n=500
50 Correct 3 ms 504 KB n=500
51 Correct 3 ms 508 KB n=500
52 Correct 2 ms 504 KB n=500
53 Correct 3 ms 504 KB n=500
54 Correct 2 ms 508 KB n=500
55 Correct 2 ms 376 KB n=278
56 Correct 3 ms 504 KB n=500
57 Correct 3 ms 504 KB n=500
58 Correct 5 ms 504 KB n=500
59 Correct 4 ms 632 KB n=2000
60 Correct 6 ms 760 KB n=2000
61 Correct 11 ms 760 KB n=2000
62 Correct 13 ms 760 KB n=2000
63 Correct 5 ms 632 KB n=2000
64 Correct 14 ms 888 KB n=2000
65 Correct 4 ms 632 KB n=2000
66 Correct 9 ms 760 KB n=2000
67 Correct 5 ms 632 KB n=2000
68 Correct 12 ms 760 KB n=2000
69 Correct 82 ms 720 KB n=2000
70 Correct 85 ms 752 KB n=2000
71 Correct 83 ms 632 KB n=2000
72 Correct 46 ms 632 KB n=2000
73 Correct 45 ms 632 KB n=2000
74 Correct 3 ms 760 KB n=1844
75 Correct 42 ms 636 KB n=2000
76 Correct 43 ms 632 KB n=2000
77 Correct 47 ms 760 KB n=2000
78 Correct 45 ms 632 KB n=2000
79 Correct 4 ms 760 KB n=2000
80 Correct 7 ms 760 KB n=2000
81 Correct 12 ms 760 KB n=2000
82 Correct 4 ms 760 KB n=2000
83 Correct 9 ms 760 KB n=2000
84 Correct 26 ms 760 KB n=2000
85 Correct 28 ms 760 KB n=2000
86 Correct 34 ms 760 KB n=2000
87 Correct 30 ms 728 KB n=2000
88 Correct 15 ms 760 KB n=2000
89 Correct 15 ms 760 KB n=2000
90 Correct 8 ms 760 KB n=2000
91 Correct 63 ms 732 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB n=5
2 Correct 2 ms 552 KB n=100
3 Correct 2 ms 376 KB n=100
4 Correct 2 ms 376 KB n=100
5 Correct 2 ms 504 KB n=100
6 Correct 2 ms 508 KB n=100
7 Correct 2 ms 504 KB n=100
8 Correct 2 ms 376 KB n=100
9 Correct 2 ms 508 KB n=100
10 Correct 2 ms 504 KB n=100
11 Correct 2 ms 376 KB n=100
12 Correct 2 ms 504 KB n=100
13 Correct 2 ms 504 KB n=100
14 Correct 2 ms 504 KB n=100
15 Correct 2 ms 504 KB n=100
16 Correct 2 ms 376 KB n=100
17 Correct 2 ms 508 KB n=100
18 Correct 2 ms 504 KB n=100
19 Correct 2 ms 376 KB n=100
20 Correct 2 ms 508 KB n=100
21 Correct 2 ms 504 KB n=100
22 Correct 2 ms 376 KB n=100
23 Correct 2 ms 504 KB n=100
24 Correct 2 ms 376 KB n=100
25 Correct 2 ms 376 KB n=100
26 Correct 2 ms 376 KB n=12
27 Correct 2 ms 504 KB n=100
28 Correct 2 ms 504 KB n=500
29 Correct 2 ms 504 KB n=500
30 Correct 3 ms 504 KB n=500
31 Correct 2 ms 504 KB n=500
32 Correct 2 ms 504 KB n=500
33 Correct 3 ms 504 KB n=500
34 Correct 2 ms 504 KB n=500
35 Correct 2 ms 504 KB n=500
36 Correct 5 ms 504 KB n=500
37 Correct 5 ms 504 KB n=500
38 Correct 5 ms 504 KB n=500
39 Correct 4 ms 504 KB n=500
40 Correct 4 ms 504 KB n=500
41 Correct 4 ms 504 KB n=500
42 Correct 4 ms 504 KB n=500
43 Correct 4 ms 504 KB n=500
44 Correct 4 ms 504 KB n=500
45 Correct 2 ms 504 KB n=500
46 Correct 2 ms 508 KB n=500
47 Correct 2 ms 504 KB n=500
48 Correct 2 ms 504 KB n=500
49 Correct 3 ms 504 KB n=500
50 Correct 3 ms 504 KB n=500
51 Correct 3 ms 508 KB n=500
52 Correct 2 ms 504 KB n=500
53 Correct 3 ms 504 KB n=500
54 Correct 2 ms 508 KB n=500
55 Correct 2 ms 376 KB n=278
56 Correct 3 ms 504 KB n=500
57 Correct 3 ms 504 KB n=500
58 Correct 5 ms 504 KB n=500
59 Correct 4 ms 632 KB n=2000
60 Correct 6 ms 760 KB n=2000
61 Correct 11 ms 760 KB n=2000
62 Correct 13 ms 760 KB n=2000
63 Correct 5 ms 632 KB n=2000
64 Correct 14 ms 888 KB n=2000
65 Correct 4 ms 632 KB n=2000
66 Correct 9 ms 760 KB n=2000
67 Correct 5 ms 632 KB n=2000
68 Correct 12 ms 760 KB n=2000
69 Correct 82 ms 720 KB n=2000
70 Correct 85 ms 752 KB n=2000
71 Correct 83 ms 632 KB n=2000
72 Correct 46 ms 632 KB n=2000
73 Correct 45 ms 632 KB n=2000
74 Correct 3 ms 760 KB n=1844
75 Correct 42 ms 636 KB n=2000
76 Correct 43 ms 632 KB n=2000
77 Correct 47 ms 760 KB n=2000
78 Correct 45 ms 632 KB n=2000
79 Correct 4 ms 760 KB n=2000
80 Correct 7 ms 760 KB n=2000
81 Correct 12 ms 760 KB n=2000
82 Correct 4 ms 760 KB n=2000
83 Correct 9 ms 760 KB n=2000
84 Correct 26 ms 760 KB n=2000
85 Correct 28 ms 760 KB n=2000
86 Correct 34 ms 760 KB n=2000
87 Correct 30 ms 728 KB n=2000
88 Correct 15 ms 760 KB n=2000
89 Correct 15 ms 760 KB n=2000
90 Correct 8 ms 760 KB n=2000
91 Correct 63 ms 732 KB n=2000
92 Runtime error 2 ms 632 KB Execution killed with signal 11 (could be triggered by violating memory limits)
93 Halted 0 ms 0 KB -