Submission #65818

# Submission time Handle Problem Language Result Execution time Memory
65818 2018-08-09T00:42:38 Z kingpig9 Birthday gift (IZhO18_treearray) C++11
56 / 100
1993 ms 263168 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);
    ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 30 ms 31096 KB n=5
2 Correct 34 ms 31372 KB n=100
3 Correct 31 ms 31404 KB n=100
4 Correct 33 ms 31404 KB n=100
5 Correct 31 ms 31404 KB n=100
6 Correct 29 ms 31404 KB n=100
7 Correct 29 ms 31404 KB n=100
8 Correct 35 ms 31404 KB n=100
9 Correct 36 ms 31404 KB n=100
10 Correct 30 ms 31404 KB n=100
11 Correct 35 ms 31404 KB n=100
12 Correct 30 ms 31404 KB n=100
13 Correct 33 ms 31404 KB n=100
14 Correct 30 ms 31404 KB n=100
15 Correct 30 ms 31404 KB n=100
16 Correct 30 ms 31404 KB n=100
17 Correct 31 ms 31404 KB n=100
18 Correct 33 ms 31420 KB n=100
19 Correct 31 ms 31420 KB n=100
20 Correct 36 ms 31468 KB n=100
21 Correct 36 ms 31468 KB n=100
22 Correct 32 ms 31468 KB n=100
23 Correct 31 ms 31468 KB n=100
24 Correct 34 ms 31468 KB n=100
25 Correct 33 ms 31468 KB n=100
26 Correct 31 ms 31468 KB n=12
27 Correct 37 ms 31468 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 30 ms 31096 KB n=5
2 Correct 34 ms 31372 KB n=100
3 Correct 31 ms 31404 KB n=100
4 Correct 33 ms 31404 KB n=100
5 Correct 31 ms 31404 KB n=100
6 Correct 29 ms 31404 KB n=100
7 Correct 29 ms 31404 KB n=100
8 Correct 35 ms 31404 KB n=100
9 Correct 36 ms 31404 KB n=100
10 Correct 30 ms 31404 KB n=100
11 Correct 35 ms 31404 KB n=100
12 Correct 30 ms 31404 KB n=100
13 Correct 33 ms 31404 KB n=100
14 Correct 30 ms 31404 KB n=100
15 Correct 30 ms 31404 KB n=100
16 Correct 30 ms 31404 KB n=100
17 Correct 31 ms 31404 KB n=100
18 Correct 33 ms 31420 KB n=100
19 Correct 31 ms 31420 KB n=100
20 Correct 36 ms 31468 KB n=100
21 Correct 36 ms 31468 KB n=100
22 Correct 32 ms 31468 KB n=100
23 Correct 31 ms 31468 KB n=100
24 Correct 34 ms 31468 KB n=100
25 Correct 33 ms 31468 KB n=100
26 Correct 31 ms 31468 KB n=12
27 Correct 37 ms 31468 KB n=100
28 Correct 37 ms 31468 KB n=500
29 Correct 31 ms 31468 KB n=500
30 Correct 31 ms 31468 KB n=500
31 Correct 33 ms 31468 KB n=500
32 Correct 32 ms 31468 KB n=500
33 Correct 38 ms 31552 KB n=500
34 Correct 33 ms 31552 KB n=500
35 Correct 32 ms 31552 KB n=500
36 Correct 32 ms 31552 KB n=500
37 Correct 36 ms 31552 KB n=500
38 Correct 35 ms 31552 KB n=500
39 Correct 31 ms 31552 KB n=500
40 Correct 30 ms 31552 KB n=500
41 Correct 31 ms 31552 KB n=500
42 Correct 32 ms 31552 KB n=500
43 Correct 34 ms 31552 KB n=500
44 Correct 36 ms 31552 KB n=500
45 Correct 33 ms 31596 KB n=500
46 Correct 32 ms 31596 KB n=500
47 Correct 33 ms 31596 KB n=500
48 Correct 32 ms 31596 KB n=500
49 Correct 31 ms 31596 KB n=500
50 Correct 31 ms 31596 KB n=500
51 Correct 33 ms 31596 KB n=500
52 Correct 32 ms 31596 KB n=500
53 Correct 30 ms 31596 KB n=500
54 Correct 31 ms 31596 KB n=500
55 Correct 35 ms 31596 KB n=278
56 Correct 32 ms 31596 KB n=500
57 Correct 32 ms 31596 KB n=500
58 Correct 29 ms 31596 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 30 ms 31096 KB n=5
2 Correct 34 ms 31372 KB n=100
3 Correct 31 ms 31404 KB n=100
4 Correct 33 ms 31404 KB n=100
5 Correct 31 ms 31404 KB n=100
6 Correct 29 ms 31404 KB n=100
7 Correct 29 ms 31404 KB n=100
8 Correct 35 ms 31404 KB n=100
9 Correct 36 ms 31404 KB n=100
10 Correct 30 ms 31404 KB n=100
11 Correct 35 ms 31404 KB n=100
12 Correct 30 ms 31404 KB n=100
13 Correct 33 ms 31404 KB n=100
14 Correct 30 ms 31404 KB n=100
15 Correct 30 ms 31404 KB n=100
16 Correct 30 ms 31404 KB n=100
17 Correct 31 ms 31404 KB n=100
18 Correct 33 ms 31420 KB n=100
19 Correct 31 ms 31420 KB n=100
20 Correct 36 ms 31468 KB n=100
21 Correct 36 ms 31468 KB n=100
22 Correct 32 ms 31468 KB n=100
23 Correct 31 ms 31468 KB n=100
24 Correct 34 ms 31468 KB n=100
25 Correct 33 ms 31468 KB n=100
26 Correct 31 ms 31468 KB n=12
27 Correct 37 ms 31468 KB n=100
28 Correct 37 ms 31468 KB n=500
29 Correct 31 ms 31468 KB n=500
30 Correct 31 ms 31468 KB n=500
31 Correct 33 ms 31468 KB n=500
32 Correct 32 ms 31468 KB n=500
33 Correct 38 ms 31552 KB n=500
34 Correct 33 ms 31552 KB n=500
35 Correct 32 ms 31552 KB n=500
36 Correct 32 ms 31552 KB n=500
37 Correct 36 ms 31552 KB n=500
38 Correct 35 ms 31552 KB n=500
39 Correct 31 ms 31552 KB n=500
40 Correct 30 ms 31552 KB n=500
41 Correct 31 ms 31552 KB n=500
42 Correct 32 ms 31552 KB n=500
43 Correct 34 ms 31552 KB n=500
44 Correct 36 ms 31552 KB n=500
45 Correct 33 ms 31596 KB n=500
46 Correct 32 ms 31596 KB n=500
47 Correct 33 ms 31596 KB n=500
48 Correct 32 ms 31596 KB n=500
49 Correct 31 ms 31596 KB n=500
50 Correct 31 ms 31596 KB n=500
51 Correct 33 ms 31596 KB n=500
52 Correct 32 ms 31596 KB n=500
53 Correct 30 ms 31596 KB n=500
54 Correct 31 ms 31596 KB n=500
55 Correct 35 ms 31596 KB n=278
56 Correct 32 ms 31596 KB n=500
57 Correct 32 ms 31596 KB n=500
58 Correct 29 ms 31596 KB n=500
59 Correct 34 ms 31796 KB n=2000
60 Correct 35 ms 31852 KB n=2000
61 Correct 40 ms 31868 KB n=2000
62 Correct 42 ms 31884 KB n=2000
63 Correct 33 ms 31884 KB n=2000
64 Correct 34 ms 31900 KB n=2000
65 Correct 34 ms 31900 KB n=2000
66 Correct 32 ms 31900 KB n=2000
67 Correct 38 ms 31900 KB n=2000
68 Correct 36 ms 31900 KB n=2000
69 Correct 36 ms 31900 KB n=2000
70 Correct 37 ms 31900 KB n=2000
71 Correct 35 ms 31900 KB n=2000
72 Correct 36 ms 31900 KB n=2000
73 Correct 33 ms 31900 KB n=2000
74 Correct 33 ms 31900 KB n=1844
75 Correct 33 ms 31900 KB n=2000
76 Correct 39 ms 31900 KB n=2000
77 Correct 35 ms 31900 KB n=2000
78 Correct 35 ms 31916 KB n=2000
79 Correct 37 ms 31916 KB n=2000
80 Correct 36 ms 31940 KB n=2000
81 Correct 36 ms 31940 KB n=2000
82 Correct 37 ms 31940 KB n=2000
83 Correct 34 ms 31964 KB n=2000
84 Correct 36 ms 31964 KB n=2000
85 Correct 38 ms 32044 KB n=2000
86 Correct 36 ms 32044 KB n=2000
87 Correct 36 ms 32044 KB n=2000
88 Correct 33 ms 32044 KB n=2000
89 Correct 35 ms 32044 KB n=2000
90 Correct 38 ms 32044 KB n=2000
91 Correct 38 ms 32044 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 30 ms 31096 KB n=5
2 Correct 34 ms 31372 KB n=100
3 Correct 31 ms 31404 KB n=100
4 Correct 33 ms 31404 KB n=100
5 Correct 31 ms 31404 KB n=100
6 Correct 29 ms 31404 KB n=100
7 Correct 29 ms 31404 KB n=100
8 Correct 35 ms 31404 KB n=100
9 Correct 36 ms 31404 KB n=100
10 Correct 30 ms 31404 KB n=100
11 Correct 35 ms 31404 KB n=100
12 Correct 30 ms 31404 KB n=100
13 Correct 33 ms 31404 KB n=100
14 Correct 30 ms 31404 KB n=100
15 Correct 30 ms 31404 KB n=100
16 Correct 30 ms 31404 KB n=100
17 Correct 31 ms 31404 KB n=100
18 Correct 33 ms 31420 KB n=100
19 Correct 31 ms 31420 KB n=100
20 Correct 36 ms 31468 KB n=100
21 Correct 36 ms 31468 KB n=100
22 Correct 32 ms 31468 KB n=100
23 Correct 31 ms 31468 KB n=100
24 Correct 34 ms 31468 KB n=100
25 Correct 33 ms 31468 KB n=100
26 Correct 31 ms 31468 KB n=12
27 Correct 37 ms 31468 KB n=100
28 Correct 37 ms 31468 KB n=500
29 Correct 31 ms 31468 KB n=500
30 Correct 31 ms 31468 KB n=500
31 Correct 33 ms 31468 KB n=500
32 Correct 32 ms 31468 KB n=500
33 Correct 38 ms 31552 KB n=500
34 Correct 33 ms 31552 KB n=500
35 Correct 32 ms 31552 KB n=500
36 Correct 32 ms 31552 KB n=500
37 Correct 36 ms 31552 KB n=500
38 Correct 35 ms 31552 KB n=500
39 Correct 31 ms 31552 KB n=500
40 Correct 30 ms 31552 KB n=500
41 Correct 31 ms 31552 KB n=500
42 Correct 32 ms 31552 KB n=500
43 Correct 34 ms 31552 KB n=500
44 Correct 36 ms 31552 KB n=500
45 Correct 33 ms 31596 KB n=500
46 Correct 32 ms 31596 KB n=500
47 Correct 33 ms 31596 KB n=500
48 Correct 32 ms 31596 KB n=500
49 Correct 31 ms 31596 KB n=500
50 Correct 31 ms 31596 KB n=500
51 Correct 33 ms 31596 KB n=500
52 Correct 32 ms 31596 KB n=500
53 Correct 30 ms 31596 KB n=500
54 Correct 31 ms 31596 KB n=500
55 Correct 35 ms 31596 KB n=278
56 Correct 32 ms 31596 KB n=500
57 Correct 32 ms 31596 KB n=500
58 Correct 29 ms 31596 KB n=500
59 Correct 34 ms 31796 KB n=2000
60 Correct 35 ms 31852 KB n=2000
61 Correct 40 ms 31868 KB n=2000
62 Correct 42 ms 31884 KB n=2000
63 Correct 33 ms 31884 KB n=2000
64 Correct 34 ms 31900 KB n=2000
65 Correct 34 ms 31900 KB n=2000
66 Correct 32 ms 31900 KB n=2000
67 Correct 38 ms 31900 KB n=2000
68 Correct 36 ms 31900 KB n=2000
69 Correct 36 ms 31900 KB n=2000
70 Correct 37 ms 31900 KB n=2000
71 Correct 35 ms 31900 KB n=2000
72 Correct 36 ms 31900 KB n=2000
73 Correct 33 ms 31900 KB n=2000
74 Correct 33 ms 31900 KB n=1844
75 Correct 33 ms 31900 KB n=2000
76 Correct 39 ms 31900 KB n=2000
77 Correct 35 ms 31900 KB n=2000
78 Correct 35 ms 31916 KB n=2000
79 Correct 37 ms 31916 KB n=2000
80 Correct 36 ms 31940 KB n=2000
81 Correct 36 ms 31940 KB n=2000
82 Correct 37 ms 31940 KB n=2000
83 Correct 34 ms 31964 KB n=2000
84 Correct 36 ms 31964 KB n=2000
85 Correct 38 ms 32044 KB n=2000
86 Correct 36 ms 32044 KB n=2000
87 Correct 36 ms 32044 KB n=2000
88 Correct 33 ms 32044 KB n=2000
89 Correct 35 ms 32044 KB n=2000
90 Correct 38 ms 32044 KB n=2000
91 Correct 38 ms 32044 KB n=2000
92 Correct 1150 ms 74192 KB n=200000
93 Correct 1614 ms 82940 KB n=200000
94 Correct 1393 ms 95108 KB n=200000
95 Correct 1257 ms 95108 KB n=200000
96 Correct 1146 ms 98228 KB n=200000
97 Correct 1596 ms 109996 KB n=200000
98 Correct 1254 ms 112408 KB n=200000
99 Correct 1328 ms 119104 KB n=200000
100 Correct 1120 ms 125972 KB n=200000
101 Correct 1450 ms 145072 KB n=200000
102 Correct 671 ms 145072 KB n=200000
103 Correct 677 ms 147848 KB n=200000
104 Correct 770 ms 154552 KB n=200000
105 Correct 750 ms 161708 KB n=200000
106 Correct 727 ms 168980 KB n=200000
107 Correct 747 ms 176480 KB n=200000
108 Correct 1372 ms 182440 KB n=200000
109 Correct 1349 ms 189284 KB n=200000
110 Correct 1287 ms 195960 KB n=200000
111 Correct 1174 ms 202384 KB n=200000
112 Correct 1668 ms 220456 KB n=200000
113 Correct 1608 ms 222104 KB n=200000
114 Correct 1366 ms 223676 KB n=200000
115 Correct 1993 ms 232928 KB n=200000
116 Correct 1274 ms 238208 KB n=200000
117 Correct 1559 ms 256744 KB n=200000
118 Correct 1668 ms 256744 KB n=200000
119 Correct 1200 ms 260764 KB n=200000
120 Runtime error 1440 ms 263168 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
121 Halted 0 ms 0 KB -