답안 #92147

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
92147 2019-01-01T16:46:33 Z emil_physmath Birthday gift (IZhO18_treearray) C++17
100 / 100
1311 ms 92880 KB
#include <iostream>
#include <stdio.h>
#include <vector>
#include <set>
using namespace std;
const int MAXN=200005;

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

int LCA(int u, int v);
void dfs (int u, int p=1);
inline 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);
		atind[a[i]].insert(i);
	}
	for (int i=1; i<m; i++)
	{
		lca[i]=LCA(a[i], a[i+1]);
		lcas[lca[i]].insert(i);
	}
	int type, pos, L, R, v, ans;
	while (q--)
	{
		scanf("%d", &type);
		if (type==1)
		{
			scanf("%d%d", &pos, &v);
			atind[a[pos]].erase(pos);
			a[pos]=v;
			atind[v].insert(pos);
			if (pos-1>=1)
			{
				lcas[lca[pos-1]].erase(pos-1);
				lca[pos-1]=LCA(a[pos-1], a[pos]);
				lcas[lca[pos-1]].insert(pos-1);
			}
			if (pos+1<=m)
			{
				lcas[lca[pos]].erase(pos);
				lca[pos]=LCA(a[pos], a[pos+1]);
				lcas[lca[pos]].insert(pos);
			}
		}
		else
		{
			scanf("%d%d%d", &L, &R, &v);
			ans=-1;
			auto it=lcas[v].lower_bound(L);
			if (it!=lcas[v].end() && *it+1<=R)
				ans=*it;
			if (ans==-1)
			{
				auto it=atind[v].lower_bound(L);
				if (it!=atind[v].end() && *it<=R)
					ans=*it;
				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;
}

inline 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:97: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:22: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:34:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", a+i);
   ~~~~~^~~~~~~~~~~
treearray.cpp:45:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &type);
   ~~~~~^~~~~~~~~~~~~
treearray.cpp:48: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:67: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);
    ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 28536 KB n=5
2 Correct 27 ms 28536 KB n=100
3 Correct 27 ms 28536 KB n=100
4 Correct 26 ms 28536 KB n=100
5 Correct 27 ms 28536 KB n=100
6 Correct 54 ms 28536 KB n=100
7 Correct 22 ms 28508 KB n=100
8 Correct 22 ms 28536 KB n=100
9 Correct 23 ms 28536 KB n=100
10 Correct 23 ms 28536 KB n=100
11 Correct 26 ms 28664 KB n=100
12 Correct 22 ms 28536 KB n=100
13 Correct 23 ms 28536 KB n=100
14 Correct 23 ms 28508 KB n=100
15 Correct 62 ms 28536 KB n=100
16 Correct 27 ms 28536 KB n=100
17 Correct 22 ms 28536 KB n=100
18 Correct 23 ms 28536 KB n=100
19 Correct 31 ms 28508 KB n=100
20 Correct 27 ms 28536 KB n=100
21 Correct 23 ms 28508 KB n=100
22 Correct 27 ms 28536 KB n=100
23 Correct 23 ms 28536 KB n=100
24 Correct 23 ms 28536 KB n=100
25 Correct 27 ms 28536 KB n=100
26 Correct 27 ms 28536 KB n=12
27 Correct 27 ms 28536 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 28536 KB n=5
2 Correct 27 ms 28536 KB n=100
3 Correct 27 ms 28536 KB n=100
4 Correct 26 ms 28536 KB n=100
5 Correct 27 ms 28536 KB n=100
6 Correct 54 ms 28536 KB n=100
7 Correct 22 ms 28508 KB n=100
8 Correct 22 ms 28536 KB n=100
9 Correct 23 ms 28536 KB n=100
10 Correct 23 ms 28536 KB n=100
11 Correct 26 ms 28664 KB n=100
12 Correct 22 ms 28536 KB n=100
13 Correct 23 ms 28536 KB n=100
14 Correct 23 ms 28508 KB n=100
15 Correct 62 ms 28536 KB n=100
16 Correct 27 ms 28536 KB n=100
17 Correct 22 ms 28536 KB n=100
18 Correct 23 ms 28536 KB n=100
19 Correct 31 ms 28508 KB n=100
20 Correct 27 ms 28536 KB n=100
21 Correct 23 ms 28508 KB n=100
22 Correct 27 ms 28536 KB n=100
23 Correct 23 ms 28536 KB n=100
24 Correct 23 ms 28536 KB n=100
25 Correct 27 ms 28536 KB n=100
26 Correct 27 ms 28536 KB n=12
27 Correct 27 ms 28536 KB n=100
28 Correct 23 ms 28536 KB n=500
29 Correct 24 ms 28792 KB n=500
30 Correct 27 ms 28664 KB n=500
31 Correct 24 ms 28664 KB n=500
32 Correct 27 ms 28668 KB n=500
33 Correct 23 ms 28664 KB n=500
34 Correct 27 ms 28536 KB n=500
35 Correct 27 ms 28552 KB n=500
36 Correct 28 ms 28664 KB n=500
37 Correct 28 ms 28536 KB n=500
38 Correct 27 ms 28664 KB n=500
39 Correct 27 ms 28636 KB n=500
40 Correct 28 ms 28536 KB n=500
41 Correct 28 ms 28628 KB n=500
42 Correct 28 ms 28664 KB n=500
43 Correct 27 ms 28536 KB n=500
44 Correct 28 ms 28536 KB n=500
45 Correct 27 ms 28536 KB n=500
46 Correct 27 ms 28664 KB n=500
47 Correct 27 ms 28664 KB n=500
48 Correct 28 ms 28664 KB n=500
49 Correct 27 ms 28664 KB n=500
50 Correct 24 ms 28664 KB n=500
51 Correct 23 ms 28664 KB n=500
52 Correct 23 ms 28664 KB n=500
53 Correct 27 ms 28664 KB n=500
54 Correct 23 ms 28664 KB n=500
55 Correct 23 ms 28664 KB n=278
56 Correct 27 ms 28676 KB n=500
57 Correct 27 ms 28660 KB n=500
58 Correct 27 ms 28612 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 28536 KB n=5
2 Correct 27 ms 28536 KB n=100
3 Correct 27 ms 28536 KB n=100
4 Correct 26 ms 28536 KB n=100
5 Correct 27 ms 28536 KB n=100
6 Correct 54 ms 28536 KB n=100
7 Correct 22 ms 28508 KB n=100
8 Correct 22 ms 28536 KB n=100
9 Correct 23 ms 28536 KB n=100
10 Correct 23 ms 28536 KB n=100
11 Correct 26 ms 28664 KB n=100
12 Correct 22 ms 28536 KB n=100
13 Correct 23 ms 28536 KB n=100
14 Correct 23 ms 28508 KB n=100
15 Correct 62 ms 28536 KB n=100
16 Correct 27 ms 28536 KB n=100
17 Correct 22 ms 28536 KB n=100
18 Correct 23 ms 28536 KB n=100
19 Correct 31 ms 28508 KB n=100
20 Correct 27 ms 28536 KB n=100
21 Correct 23 ms 28508 KB n=100
22 Correct 27 ms 28536 KB n=100
23 Correct 23 ms 28536 KB n=100
24 Correct 23 ms 28536 KB n=100
25 Correct 27 ms 28536 KB n=100
26 Correct 27 ms 28536 KB n=12
27 Correct 27 ms 28536 KB n=100
28 Correct 23 ms 28536 KB n=500
29 Correct 24 ms 28792 KB n=500
30 Correct 27 ms 28664 KB n=500
31 Correct 24 ms 28664 KB n=500
32 Correct 27 ms 28668 KB n=500
33 Correct 23 ms 28664 KB n=500
34 Correct 27 ms 28536 KB n=500
35 Correct 27 ms 28552 KB n=500
36 Correct 28 ms 28664 KB n=500
37 Correct 28 ms 28536 KB n=500
38 Correct 27 ms 28664 KB n=500
39 Correct 27 ms 28636 KB n=500
40 Correct 28 ms 28536 KB n=500
41 Correct 28 ms 28628 KB n=500
42 Correct 28 ms 28664 KB n=500
43 Correct 27 ms 28536 KB n=500
44 Correct 28 ms 28536 KB n=500
45 Correct 27 ms 28536 KB n=500
46 Correct 27 ms 28664 KB n=500
47 Correct 27 ms 28664 KB n=500
48 Correct 28 ms 28664 KB n=500
49 Correct 27 ms 28664 KB n=500
50 Correct 24 ms 28664 KB n=500
51 Correct 23 ms 28664 KB n=500
52 Correct 23 ms 28664 KB n=500
53 Correct 27 ms 28664 KB n=500
54 Correct 23 ms 28664 KB n=500
55 Correct 23 ms 28664 KB n=278
56 Correct 27 ms 28676 KB n=500
57 Correct 27 ms 28660 KB n=500
58 Correct 27 ms 28612 KB n=500
59 Correct 30 ms 28920 KB n=2000
60 Correct 29 ms 29048 KB n=2000
61 Correct 30 ms 29048 KB n=2000
62 Correct 31 ms 29048 KB n=2000
63 Correct 32 ms 29048 KB n=2000
64 Correct 31 ms 28924 KB n=2000
65 Correct 31 ms 28920 KB n=2000
66 Correct 31 ms 29048 KB n=2000
67 Correct 32 ms 28944 KB n=2000
68 Correct 31 ms 29048 KB n=2000
69 Correct 30 ms 28920 KB n=2000
70 Correct 30 ms 28920 KB n=2000
71 Correct 30 ms 28920 KB n=2000
72 Correct 31 ms 28892 KB n=2000
73 Correct 33 ms 28920 KB n=2000
74 Correct 31 ms 28920 KB n=1844
75 Correct 31 ms 29048 KB n=2000
76 Correct 31 ms 28920 KB n=2000
77 Correct 31 ms 29048 KB n=2000
78 Correct 30 ms 28920 KB n=2000
79 Correct 30 ms 28920 KB n=2000
80 Correct 31 ms 29048 KB n=2000
81 Correct 31 ms 29048 KB n=2000
82 Correct 32 ms 28920 KB n=2000
83 Correct 31 ms 29048 KB n=2000
84 Correct 31 ms 28920 KB n=2000
85 Correct 31 ms 28920 KB n=2000
86 Correct 31 ms 28920 KB n=2000
87 Correct 26 ms 28920 KB n=2000
88 Correct 29 ms 29048 KB n=2000
89 Correct 26 ms 28996 KB n=2000
90 Correct 31 ms 29080 KB n=2000
91 Correct 26 ms 28920 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 28536 KB n=5
2 Correct 27 ms 28536 KB n=100
3 Correct 27 ms 28536 KB n=100
4 Correct 26 ms 28536 KB n=100
5 Correct 27 ms 28536 KB n=100
6 Correct 54 ms 28536 KB n=100
7 Correct 22 ms 28508 KB n=100
8 Correct 22 ms 28536 KB n=100
9 Correct 23 ms 28536 KB n=100
10 Correct 23 ms 28536 KB n=100
11 Correct 26 ms 28664 KB n=100
12 Correct 22 ms 28536 KB n=100
13 Correct 23 ms 28536 KB n=100
14 Correct 23 ms 28508 KB n=100
15 Correct 62 ms 28536 KB n=100
16 Correct 27 ms 28536 KB n=100
17 Correct 22 ms 28536 KB n=100
18 Correct 23 ms 28536 KB n=100
19 Correct 31 ms 28508 KB n=100
20 Correct 27 ms 28536 KB n=100
21 Correct 23 ms 28508 KB n=100
22 Correct 27 ms 28536 KB n=100
23 Correct 23 ms 28536 KB n=100
24 Correct 23 ms 28536 KB n=100
25 Correct 27 ms 28536 KB n=100
26 Correct 27 ms 28536 KB n=12
27 Correct 27 ms 28536 KB n=100
28 Correct 23 ms 28536 KB n=500
29 Correct 24 ms 28792 KB n=500
30 Correct 27 ms 28664 KB n=500
31 Correct 24 ms 28664 KB n=500
32 Correct 27 ms 28668 KB n=500
33 Correct 23 ms 28664 KB n=500
34 Correct 27 ms 28536 KB n=500
35 Correct 27 ms 28552 KB n=500
36 Correct 28 ms 28664 KB n=500
37 Correct 28 ms 28536 KB n=500
38 Correct 27 ms 28664 KB n=500
39 Correct 27 ms 28636 KB n=500
40 Correct 28 ms 28536 KB n=500
41 Correct 28 ms 28628 KB n=500
42 Correct 28 ms 28664 KB n=500
43 Correct 27 ms 28536 KB n=500
44 Correct 28 ms 28536 KB n=500
45 Correct 27 ms 28536 KB n=500
46 Correct 27 ms 28664 KB n=500
47 Correct 27 ms 28664 KB n=500
48 Correct 28 ms 28664 KB n=500
49 Correct 27 ms 28664 KB n=500
50 Correct 24 ms 28664 KB n=500
51 Correct 23 ms 28664 KB n=500
52 Correct 23 ms 28664 KB n=500
53 Correct 27 ms 28664 KB n=500
54 Correct 23 ms 28664 KB n=500
55 Correct 23 ms 28664 KB n=278
56 Correct 27 ms 28676 KB n=500
57 Correct 27 ms 28660 KB n=500
58 Correct 27 ms 28612 KB n=500
59 Correct 30 ms 28920 KB n=2000
60 Correct 29 ms 29048 KB n=2000
61 Correct 30 ms 29048 KB n=2000
62 Correct 31 ms 29048 KB n=2000
63 Correct 32 ms 29048 KB n=2000
64 Correct 31 ms 28924 KB n=2000
65 Correct 31 ms 28920 KB n=2000
66 Correct 31 ms 29048 KB n=2000
67 Correct 32 ms 28944 KB n=2000
68 Correct 31 ms 29048 KB n=2000
69 Correct 30 ms 28920 KB n=2000
70 Correct 30 ms 28920 KB n=2000
71 Correct 30 ms 28920 KB n=2000
72 Correct 31 ms 28892 KB n=2000
73 Correct 33 ms 28920 KB n=2000
74 Correct 31 ms 28920 KB n=1844
75 Correct 31 ms 29048 KB n=2000
76 Correct 31 ms 28920 KB n=2000
77 Correct 31 ms 29048 KB n=2000
78 Correct 30 ms 28920 KB n=2000
79 Correct 30 ms 28920 KB n=2000
80 Correct 31 ms 29048 KB n=2000
81 Correct 31 ms 29048 KB n=2000
82 Correct 32 ms 28920 KB n=2000
83 Correct 31 ms 29048 KB n=2000
84 Correct 31 ms 28920 KB n=2000
85 Correct 31 ms 28920 KB n=2000
86 Correct 31 ms 28920 KB n=2000
87 Correct 26 ms 28920 KB n=2000
88 Correct 29 ms 29048 KB n=2000
89 Correct 26 ms 28996 KB n=2000
90 Correct 31 ms 29080 KB n=2000
91 Correct 26 ms 28920 KB n=2000
92 Correct 901 ms 79084 KB n=200000
93 Correct 1085 ms 82784 KB n=200000
94 Correct 902 ms 86040 KB n=200000
95 Correct 1083 ms 78952 KB n=200000
96 Correct 1038 ms 78964 KB n=200000
97 Correct 1071 ms 81852 KB n=200000
98 Correct 1102 ms 79124 KB n=200000
99 Correct 1232 ms 78112 KB n=200000
100 Correct 1037 ms 78832 KB n=200000
101 Correct 774 ms 86652 KB n=200000
102 Correct 593 ms 81404 KB n=200000
103 Correct 631 ms 84856 KB n=200000
104 Correct 600 ms 84944 KB n=200000
105 Correct 554 ms 85368 KB n=200000
106 Correct 554 ms 85496 KB n=200000
107 Correct 542 ms 85468 KB n=200000
108 Correct 938 ms 83960 KB n=200000
109 Correct 943 ms 83996 KB n=200000
110 Correct 1142 ms 83904 KB n=200000
111 Correct 1044 ms 83324 KB n=200000
112 Correct 870 ms 92444 KB n=200000
113 Correct 1220 ms 88084 KB n=200000
114 Correct 1004 ms 83444 KB n=200000
115 Correct 1250 ms 86008 KB n=200000
116 Correct 880 ms 84080 KB n=200000
117 Correct 687 ms 92828 KB n=200000
118 Correct 1311 ms 86820 KB n=200000
119 Correct 847 ms 84168 KB n=200000
120 Correct 765 ms 92404 KB n=200000
121 Correct 838 ms 92536 KB n=200000
122 Correct 808 ms 92880 KB n=200000
123 Correct 620 ms 85220 KB n=200000
124 Correct 268 ms 43936 KB n=25264