답안 #65819

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
65819 2018-08-09T00:44:06 Z kingpig9 Birthday gift (IZhO18_treearray) C++11
100 / 100
1879 ms 115428 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1 << 18, MAXLG = 18;

#define debug(...) fprintf(stderr, __VA_ARGS__)
//#define debug(...)
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define fillchar(a, s) memset((a), (s), sizeof(a))

int N, M, Q;
vector<int> adj[MAXN];
int par[MAXN][MAXLG], depth[MAXN];
int A[MAXN];

int getpar (int x, int d) {
	for (int i = 0; d; d >>= 1, i++) {
		if (d & 1) {
			x = par[x][i];
		}
	}
	return x;
}

int lca (int x, int y) {
	if (depth[x] < depth[y]) {
		swap(x, y);
	}
	x = getpar(x, depth[x] - depth[y]);
	if (x == y) {
		return x;
	}

	for (int i = MAXLG - 1; i >= 0; i--) {
		if (par[x][i] != par[y][i]) {
			x = par[x][i];
			y = par[y][i];
		}
	}
	return par[x][0];
}

void dfs (int x) {
	for (int y : adj[x]) {
		adj[y].erase(find(all(adj[y]), x));
		par[y][0] = x;
		for (int i = 1; i < MAXLG; i++) {
			par[y][i] = par[par[y][i - 1]][i - 1];
		}
		depth[y] = depth[x] + 1;

		dfs(y);
	}
}

set<int> st[MAXN][2];

pii solve (int lt, int rt, int v) {
	set<int> *s = st[v];
	auto it = s[0].lower_bound(lt);
	if (it != s[0].end() && *it <= rt) {
		return pii(*it, *it);
	}
	it = s[1].lower_bound(lt);
	if (it != s[1].end() && *it < rt) {
		return pii(*it, *it + 1);
	}
	return pii(-1, -1);
}

int main() {
	scanf("%d %d %d", &N, &M, &Q);
	for (int i = 1; i < N; i++) {
		int x, y;
		scanf("%d %d", &x, &y);
		adj[x].push_back(y);
		adj[y].push_back(x);
	}

	dfs(1);

	for (int i = 1; i <= M; i++) {
		scanf("%d", &A[i]);
	}
	for (int i = 1; i <= M; i++) {
		st[A[i]][0].insert(i);
		if (i + 1 <= M) {
			st[lca(A[i], A[i + 1])][1].insert(i);
		}
	}

	//and now...queries
	for (int qi = 1; qi <= Q; qi++) {
		int qt, v;
		scanf("%d", &qt);
		if (qt == 1) {
			int pos;
			scanf("%d %d", &pos, &v);

			st[A[pos]][0].erase(pos);
			if (pos + 1 <= M) {
				st[lca(A[pos], A[pos + 1])][1].erase(pos);
			}
			if (pos - 1 >= 1) {
				st[lca(A[pos - 1], A[pos])][1].erase(pos - 1);
			}

			A[pos] = v;

			st[A[pos]][0].insert(pos);
			if (pos + 1 <= M) {
				st[lca(A[pos], A[pos + 1])][1].insert(pos);
			}
			if (pos - 1 >= 1) {
				st[lca(A[pos - 1], A[pos])][1].insert(pos - 1);
			}
		} else {
			int lt, rt;
			scanf("%d %d %d", &lt, &rt, &v);
			pii ans = solve(lt, rt, v);
			printf("%d %d\n", ans.fi, ans.se);
		}
	}
}

Compilation message

treearray.cpp: In function 'int main()':
treearray.cpp:77:7: 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:80:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &x, &y);
   ~~~~~^~~~~~~~~~~~~~~~~
treearray.cpp:88:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &A[i]);
   ~~~~~^~~~~~~~~~~~~
treearray.cpp:100:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &qt);
   ~~~~~^~~~~~~~~~~
treearray.cpp:103: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:124:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d %d %d", &lt, &rt, &v);
    ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 31100 KB n=5
2 Correct 33 ms 31208 KB n=100
3 Correct 29 ms 31280 KB n=100
4 Correct 33 ms 31572 KB n=100
5 Correct 34 ms 31572 KB n=100
6 Correct 33 ms 31572 KB n=100
7 Correct 29 ms 31572 KB n=100
8 Correct 35 ms 31572 KB n=100
9 Correct 31 ms 31572 KB n=100
10 Correct 31 ms 31572 KB n=100
11 Correct 31 ms 31572 KB n=100
12 Correct 33 ms 31572 KB n=100
13 Correct 35 ms 31572 KB n=100
14 Correct 32 ms 31572 KB n=100
15 Correct 31 ms 31572 KB n=100
16 Correct 30 ms 31572 KB n=100
17 Correct 31 ms 31572 KB n=100
18 Correct 32 ms 31572 KB n=100
19 Correct 31 ms 31572 KB n=100
20 Correct 33 ms 31572 KB n=100
21 Correct 31 ms 31572 KB n=100
22 Correct 29 ms 31572 KB n=100
23 Correct 31 ms 31572 KB n=100
24 Correct 29 ms 31572 KB n=100
25 Correct 33 ms 31572 KB n=100
26 Correct 41 ms 31572 KB n=12
27 Correct 32 ms 31572 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 31100 KB n=5
2 Correct 33 ms 31208 KB n=100
3 Correct 29 ms 31280 KB n=100
4 Correct 33 ms 31572 KB n=100
5 Correct 34 ms 31572 KB n=100
6 Correct 33 ms 31572 KB n=100
7 Correct 29 ms 31572 KB n=100
8 Correct 35 ms 31572 KB n=100
9 Correct 31 ms 31572 KB n=100
10 Correct 31 ms 31572 KB n=100
11 Correct 31 ms 31572 KB n=100
12 Correct 33 ms 31572 KB n=100
13 Correct 35 ms 31572 KB n=100
14 Correct 32 ms 31572 KB n=100
15 Correct 31 ms 31572 KB n=100
16 Correct 30 ms 31572 KB n=100
17 Correct 31 ms 31572 KB n=100
18 Correct 32 ms 31572 KB n=100
19 Correct 31 ms 31572 KB n=100
20 Correct 33 ms 31572 KB n=100
21 Correct 31 ms 31572 KB n=100
22 Correct 29 ms 31572 KB n=100
23 Correct 31 ms 31572 KB n=100
24 Correct 29 ms 31572 KB n=100
25 Correct 33 ms 31572 KB n=100
26 Correct 41 ms 31572 KB n=12
27 Correct 32 ms 31572 KB n=100
28 Correct 30 ms 31572 KB n=500
29 Correct 31 ms 31612 KB n=500
30 Correct 31 ms 31612 KB n=500
31 Correct 32 ms 31612 KB n=500
32 Correct 31 ms 31612 KB n=500
33 Correct 29 ms 31612 KB n=500
34 Correct 32 ms 31612 KB n=500
35 Correct 31 ms 31612 KB n=500
36 Correct 33 ms 31612 KB n=500
37 Correct 32 ms 31612 KB n=500
38 Correct 29 ms 31612 KB n=500
39 Correct 35 ms 31612 KB n=500
40 Correct 30 ms 31612 KB n=500
41 Correct 32 ms 31612 KB n=500
42 Correct 30 ms 31612 KB n=500
43 Correct 30 ms 31612 KB n=500
44 Correct 33 ms 31612 KB n=500
45 Correct 30 ms 31612 KB n=500
46 Correct 28 ms 31612 KB n=500
47 Correct 32 ms 31612 KB n=500
48 Correct 32 ms 31612 KB n=500
49 Correct 29 ms 31612 KB n=500
50 Correct 29 ms 31612 KB n=500
51 Correct 29 ms 31612 KB n=500
52 Correct 30 ms 31612 KB n=500
53 Correct 30 ms 31612 KB n=500
54 Correct 31 ms 31612 KB n=500
55 Correct 31 ms 31612 KB n=278
56 Correct 29 ms 31612 KB n=500
57 Correct 37 ms 31612 KB n=500
58 Correct 30 ms 31612 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 31100 KB n=5
2 Correct 33 ms 31208 KB n=100
3 Correct 29 ms 31280 KB n=100
4 Correct 33 ms 31572 KB n=100
5 Correct 34 ms 31572 KB n=100
6 Correct 33 ms 31572 KB n=100
7 Correct 29 ms 31572 KB n=100
8 Correct 35 ms 31572 KB n=100
9 Correct 31 ms 31572 KB n=100
10 Correct 31 ms 31572 KB n=100
11 Correct 31 ms 31572 KB n=100
12 Correct 33 ms 31572 KB n=100
13 Correct 35 ms 31572 KB n=100
14 Correct 32 ms 31572 KB n=100
15 Correct 31 ms 31572 KB n=100
16 Correct 30 ms 31572 KB n=100
17 Correct 31 ms 31572 KB n=100
18 Correct 32 ms 31572 KB n=100
19 Correct 31 ms 31572 KB n=100
20 Correct 33 ms 31572 KB n=100
21 Correct 31 ms 31572 KB n=100
22 Correct 29 ms 31572 KB n=100
23 Correct 31 ms 31572 KB n=100
24 Correct 29 ms 31572 KB n=100
25 Correct 33 ms 31572 KB n=100
26 Correct 41 ms 31572 KB n=12
27 Correct 32 ms 31572 KB n=100
28 Correct 30 ms 31572 KB n=500
29 Correct 31 ms 31612 KB n=500
30 Correct 31 ms 31612 KB n=500
31 Correct 32 ms 31612 KB n=500
32 Correct 31 ms 31612 KB n=500
33 Correct 29 ms 31612 KB n=500
34 Correct 32 ms 31612 KB n=500
35 Correct 31 ms 31612 KB n=500
36 Correct 33 ms 31612 KB n=500
37 Correct 32 ms 31612 KB n=500
38 Correct 29 ms 31612 KB n=500
39 Correct 35 ms 31612 KB n=500
40 Correct 30 ms 31612 KB n=500
41 Correct 32 ms 31612 KB n=500
42 Correct 30 ms 31612 KB n=500
43 Correct 30 ms 31612 KB n=500
44 Correct 33 ms 31612 KB n=500
45 Correct 30 ms 31612 KB n=500
46 Correct 28 ms 31612 KB n=500
47 Correct 32 ms 31612 KB n=500
48 Correct 32 ms 31612 KB n=500
49 Correct 29 ms 31612 KB n=500
50 Correct 29 ms 31612 KB n=500
51 Correct 29 ms 31612 KB n=500
52 Correct 30 ms 31612 KB n=500
53 Correct 30 ms 31612 KB n=500
54 Correct 31 ms 31612 KB n=500
55 Correct 31 ms 31612 KB n=278
56 Correct 29 ms 31612 KB n=500
57 Correct 37 ms 31612 KB n=500
58 Correct 30 ms 31612 KB n=500
59 Correct 34 ms 31744 KB n=2000
60 Correct 37 ms 31868 KB n=2000
61 Correct 42 ms 31884 KB n=2000
62 Correct 33 ms 31884 KB n=2000
63 Correct 42 ms 31884 KB n=2000
64 Correct 41 ms 31916 KB n=2000
65 Correct 36 ms 31916 KB n=2000
66 Correct 40 ms 31916 KB n=2000
67 Correct 35 ms 31916 KB n=2000
68 Correct 42 ms 31916 KB n=2000
69 Correct 34 ms 31916 KB n=2000
70 Correct 33 ms 31916 KB n=2000
71 Correct 33 ms 31916 KB n=2000
72 Correct 32 ms 31916 KB n=2000
73 Correct 33 ms 31916 KB n=2000
74 Correct 34 ms 31916 KB n=1844
75 Correct 32 ms 31916 KB n=2000
76 Correct 38 ms 31916 KB n=2000
77 Correct 34 ms 31916 KB n=2000
78 Correct 35 ms 31916 KB n=2000
79 Correct 34 ms 31916 KB n=2000
80 Correct 32 ms 31932 KB n=2000
81 Correct 35 ms 31932 KB n=2000
82 Correct 32 ms 31932 KB n=2000
83 Correct 34 ms 31932 KB n=2000
84 Correct 35 ms 31932 KB n=2000
85 Correct 32 ms 31932 KB n=2000
86 Correct 35 ms 31932 KB n=2000
87 Correct 33 ms 31932 KB n=2000
88 Correct 32 ms 31932 KB n=2000
89 Correct 32 ms 31968 KB n=2000
90 Correct 40 ms 31968 KB n=2000
91 Correct 35 ms 31968 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 31100 KB n=5
2 Correct 33 ms 31208 KB n=100
3 Correct 29 ms 31280 KB n=100
4 Correct 33 ms 31572 KB n=100
5 Correct 34 ms 31572 KB n=100
6 Correct 33 ms 31572 KB n=100
7 Correct 29 ms 31572 KB n=100
8 Correct 35 ms 31572 KB n=100
9 Correct 31 ms 31572 KB n=100
10 Correct 31 ms 31572 KB n=100
11 Correct 31 ms 31572 KB n=100
12 Correct 33 ms 31572 KB n=100
13 Correct 35 ms 31572 KB n=100
14 Correct 32 ms 31572 KB n=100
15 Correct 31 ms 31572 KB n=100
16 Correct 30 ms 31572 KB n=100
17 Correct 31 ms 31572 KB n=100
18 Correct 32 ms 31572 KB n=100
19 Correct 31 ms 31572 KB n=100
20 Correct 33 ms 31572 KB n=100
21 Correct 31 ms 31572 KB n=100
22 Correct 29 ms 31572 KB n=100
23 Correct 31 ms 31572 KB n=100
24 Correct 29 ms 31572 KB n=100
25 Correct 33 ms 31572 KB n=100
26 Correct 41 ms 31572 KB n=12
27 Correct 32 ms 31572 KB n=100
28 Correct 30 ms 31572 KB n=500
29 Correct 31 ms 31612 KB n=500
30 Correct 31 ms 31612 KB n=500
31 Correct 32 ms 31612 KB n=500
32 Correct 31 ms 31612 KB n=500
33 Correct 29 ms 31612 KB n=500
34 Correct 32 ms 31612 KB n=500
35 Correct 31 ms 31612 KB n=500
36 Correct 33 ms 31612 KB n=500
37 Correct 32 ms 31612 KB n=500
38 Correct 29 ms 31612 KB n=500
39 Correct 35 ms 31612 KB n=500
40 Correct 30 ms 31612 KB n=500
41 Correct 32 ms 31612 KB n=500
42 Correct 30 ms 31612 KB n=500
43 Correct 30 ms 31612 KB n=500
44 Correct 33 ms 31612 KB n=500
45 Correct 30 ms 31612 KB n=500
46 Correct 28 ms 31612 KB n=500
47 Correct 32 ms 31612 KB n=500
48 Correct 32 ms 31612 KB n=500
49 Correct 29 ms 31612 KB n=500
50 Correct 29 ms 31612 KB n=500
51 Correct 29 ms 31612 KB n=500
52 Correct 30 ms 31612 KB n=500
53 Correct 30 ms 31612 KB n=500
54 Correct 31 ms 31612 KB n=500
55 Correct 31 ms 31612 KB n=278
56 Correct 29 ms 31612 KB n=500
57 Correct 37 ms 31612 KB n=500
58 Correct 30 ms 31612 KB n=500
59 Correct 34 ms 31744 KB n=2000
60 Correct 37 ms 31868 KB n=2000
61 Correct 42 ms 31884 KB n=2000
62 Correct 33 ms 31884 KB n=2000
63 Correct 42 ms 31884 KB n=2000
64 Correct 41 ms 31916 KB n=2000
65 Correct 36 ms 31916 KB n=2000
66 Correct 40 ms 31916 KB n=2000
67 Correct 35 ms 31916 KB n=2000
68 Correct 42 ms 31916 KB n=2000
69 Correct 34 ms 31916 KB n=2000
70 Correct 33 ms 31916 KB n=2000
71 Correct 33 ms 31916 KB n=2000
72 Correct 32 ms 31916 KB n=2000
73 Correct 33 ms 31916 KB n=2000
74 Correct 34 ms 31916 KB n=1844
75 Correct 32 ms 31916 KB n=2000
76 Correct 38 ms 31916 KB n=2000
77 Correct 34 ms 31916 KB n=2000
78 Correct 35 ms 31916 KB n=2000
79 Correct 34 ms 31916 KB n=2000
80 Correct 32 ms 31932 KB n=2000
81 Correct 35 ms 31932 KB n=2000
82 Correct 32 ms 31932 KB n=2000
83 Correct 34 ms 31932 KB n=2000
84 Correct 35 ms 31932 KB n=2000
85 Correct 32 ms 31932 KB n=2000
86 Correct 35 ms 31932 KB n=2000
87 Correct 33 ms 31932 KB n=2000
88 Correct 32 ms 31932 KB n=2000
89 Correct 32 ms 31968 KB n=2000
90 Correct 40 ms 31968 KB n=2000
91 Correct 35 ms 31968 KB n=2000
92 Correct 1142 ms 74112 KB n=200000
93 Correct 1497 ms 83104 KB n=200000
94 Correct 1499 ms 95112 KB n=200000
95 Correct 1201 ms 95112 KB n=200000
96 Correct 1024 ms 95112 KB n=200000
97 Correct 1640 ms 95112 KB n=200000
98 Correct 1230 ms 95112 KB n=200000
99 Correct 1490 ms 95112 KB n=200000
100 Correct 1099 ms 95112 KB n=200000
101 Correct 1536 ms 97372 KB n=200000
102 Correct 767 ms 97372 KB n=200000
103 Correct 696 ms 97372 KB n=200000
104 Correct 699 ms 97372 KB n=200000
105 Correct 699 ms 97372 KB n=200000
106 Correct 682 ms 97372 KB n=200000
107 Correct 772 ms 97372 KB n=200000
108 Correct 1411 ms 97372 KB n=200000
109 Correct 1354 ms 97372 KB n=200000
110 Correct 1245 ms 97372 KB n=200000
111 Correct 1382 ms 97372 KB n=200000
112 Correct 1690 ms 97372 KB n=200000
113 Correct 1708 ms 97372 KB n=200000
114 Correct 1196 ms 97372 KB n=200000
115 Correct 1879 ms 97372 KB n=200000
116 Correct 1004 ms 97372 KB n=200000
117 Correct 1416 ms 97372 KB n=200000
118 Correct 1581 ms 97372 KB n=200000
119 Correct 1008 ms 97372 KB n=200000
120 Correct 1398 ms 101236 KB n=200000
121 Correct 1254 ms 108472 KB n=200000
122 Correct 1306 ms 115428 KB n=200000
123 Correct 705 ms 115428 KB n=200000
124 Correct 324 ms 115428 KB n=25264