Submission #65800

# Submission time Handle Problem Language Result Execution time Memory
65800 2018-08-08T21:49:23 Z kingpig9 Birthday gift (IZhO18_treearray) C++11
56 / 100
4000 ms 51888 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 ent[MAXN], exi[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 (x == 0 || y == 0) {
		return 0;
	}
	if (x == -1) {
		return y;
	}
	if (y == -1) {
		return x;
	}

	debug("LCA %d %d: ", x, y);
	if (depth[x] < depth[y]) {
		swap(x, y);
	}
	x = getpar(x, depth[x] - depth[y]);
	debug("so %d %d\n", x, y);
	if (x == y) {
		debug("DR %d\n", x);
		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];
		}
	}
	debug("PR %d\n", par[x][0]);
	return par[x][0];
}

bool isanc (int x, int y) {
	return ent[x] <= ent[y] && exi[y] <= exi[x];
}

void dfs (int x) {
	static int cur = 0;
	ent[x] = cur++;

	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;
		debug("depth[%d] = %d\n", y, depth[y]);

		dfs(y);
	}
	exi[x] = cur;
}

struct segtree {
	int tree[2 * MAXN];

	void merge (int x) {
		tree[x] = lca(tree[2 * x], tree[2 * x + 1]);
	}

	void update (int x, int v) {
		tree[x += MAXN] = v;
		while (x >>= 1) {
			merge(x);
		}
	}

	int query (int a, int b, int cur = 1, int lt = 0, int rt = MAXN) {
		if (rt <= a || b <= lt) {
			debug("ha -1\n");
			return -1;
		}

		if (a <= lt && rt <= b) {
			debug("tr %d\n", tree[cur]);
			return tree[cur];
		}

		int mid = (lt + rt) / 2;
		return lca(query(a, b, 2 * cur, lt, mid), query(a, b, 2 * cur + 1, mid, rt));
	}
} seg;

void init_segtree() {
	fillchar(seg.tree, -1);
	for (int i = 1; i <= M; i++) {
		scanf("%d", &A[i]);
		seg.tree[i + MAXN] = A[i];
	}
	for (int i = MAXN - 1; i; i--) {
		seg.merge(i);
	}
}

pii solve (int lt, int rt, int v) {
	for (int i = lt, j = lt - 1; i <= rt; i++) {
		debug("A[%d] = %d\n", i, A[i]);
		if (!isanc(v, A[i])) {
			debug("ibobbob\n");
			continue;
		}

		j = max(j, i);
		int c = seg.query(i, j + 1);
		debug("yeequery %d %d = %d\n", i, j, c);
		if (c == v) {
			return pii(i, j);
		}

		if (isanc(c, v)) {
			continue;
		}
		debug("i=%d,j=%d,c=%d.\n",i,j,c);

		while (j < rt) {
			j++;
			c = lca(c, A[j]);
			debug("i = %d, j = %d, c = %d\n", i, j, c);
			if (c == v) {
				return pii(i, j);
			}

			if (isanc(c, v)) {
				assert(c != v);
				break;
			}
		}
	}
	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);
	init_segtree();

	//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);
			A[pos] = v;
			seg.update(pos, v);
		} else {
			int lt, rt;
			scanf("%d %d %d", &lt, &rt, &v);
			pii ans = solve(lt, rt, v);
			if (ans != pii(-1, -1)) {
				debug("WHAT %d %d\n", lt, rt);
				debug("QU %d %d = %d\n", ans.fi, ans.se, seg.query(ans.fi, ans.se + 1));
				assert(seg.query(ans.fi, ans.se + 1) == v);
			}
			printf("%d %d\n", ans.fi, ans.se);
		}
	}
}

Compilation message

treearray.cpp: In function 'void init_segtree()':
treearray.cpp:118:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &A[i]);
   ~~~~~^~~~~~~~~~~~~
treearray.cpp: In function 'int main()':
treearray.cpp:164: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:167: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:178:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &qt);
   ~~~~~^~~~~~~~~~~
treearray.cpp:181: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:186: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 11 ms 8696 KB n=5
2 Correct 11 ms 8696 KB n=100
3 Correct 12 ms 8888 KB n=100
4 Correct 11 ms 8888 KB n=100
5 Correct 11 ms 8928 KB n=100
6 Correct 15 ms 8928 KB n=100
7 Correct 12 ms 8928 KB n=100
8 Correct 11 ms 8928 KB n=100
9 Correct 12 ms 8928 KB n=100
10 Correct 15 ms 8972 KB n=100
11 Correct 12 ms 9020 KB n=100
12 Correct 12 ms 9028 KB n=100
13 Correct 12 ms 9032 KB n=100
14 Correct 12 ms 9092 KB n=100
15 Correct 11 ms 9096 KB n=100
16 Correct 13 ms 9100 KB n=100
17 Correct 11 ms 9104 KB n=100
18 Correct 13 ms 9108 KB n=100
19 Correct 11 ms 9112 KB n=100
20 Correct 13 ms 9116 KB n=100
21 Correct 11 ms 9152 KB n=100
22 Correct 11 ms 9152 KB n=100
23 Correct 13 ms 9152 KB n=100
24 Correct 13 ms 9152 KB n=100
25 Correct 11 ms 9152 KB n=100
26 Correct 10 ms 9152 KB n=12
27 Correct 11 ms 9152 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 11 ms 8696 KB n=5
2 Correct 11 ms 8696 KB n=100
3 Correct 12 ms 8888 KB n=100
4 Correct 11 ms 8888 KB n=100
5 Correct 11 ms 8928 KB n=100
6 Correct 15 ms 8928 KB n=100
7 Correct 12 ms 8928 KB n=100
8 Correct 11 ms 8928 KB n=100
9 Correct 12 ms 8928 KB n=100
10 Correct 15 ms 8972 KB n=100
11 Correct 12 ms 9020 KB n=100
12 Correct 12 ms 9028 KB n=100
13 Correct 12 ms 9032 KB n=100
14 Correct 12 ms 9092 KB n=100
15 Correct 11 ms 9096 KB n=100
16 Correct 13 ms 9100 KB n=100
17 Correct 11 ms 9104 KB n=100
18 Correct 13 ms 9108 KB n=100
19 Correct 11 ms 9112 KB n=100
20 Correct 13 ms 9116 KB n=100
21 Correct 11 ms 9152 KB n=100
22 Correct 11 ms 9152 KB n=100
23 Correct 13 ms 9152 KB n=100
24 Correct 13 ms 9152 KB n=100
25 Correct 11 ms 9152 KB n=100
26 Correct 10 ms 9152 KB n=12
27 Correct 11 ms 9152 KB n=100
28 Correct 12 ms 9152 KB n=500
29 Correct 16 ms 9288 KB n=500
30 Correct 16 ms 9320 KB n=500
31 Correct 15 ms 9336 KB n=500
32 Correct 11 ms 9368 KB n=500
33 Correct 14 ms 9368 KB n=500
34 Correct 12 ms 9368 KB n=500
35 Correct 15 ms 9428 KB n=500
36 Correct 27 ms 9428 KB n=500
37 Correct 27 ms 9452 KB n=500
38 Correct 27 ms 9452 KB n=500
39 Correct 19 ms 9452 KB n=500
40 Correct 24 ms 9492 KB n=500
41 Correct 19 ms 9492 KB n=500
42 Correct 20 ms 9492 KB n=500
43 Correct 64 ms 9536 KB n=500
44 Correct 18 ms 9564 KB n=500
45 Correct 13 ms 9576 KB n=500
46 Correct 17 ms 9588 KB n=500
47 Correct 15 ms 9604 KB n=500
48 Correct 13 ms 9620 KB n=500
49 Correct 15 ms 9640 KB n=500
50 Correct 14 ms 9640 KB n=500
51 Correct 15 ms 9664 KB n=500
52 Correct 18 ms 9712 KB n=500
53 Correct 14 ms 9712 KB n=500
54 Correct 27 ms 9744 KB n=500
55 Correct 12 ms 9868 KB n=278
56 Correct 76 ms 9892 KB n=500
57 Correct 54 ms 9892 KB n=500
58 Correct 13 ms 9892 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 11 ms 8696 KB n=5
2 Correct 11 ms 8696 KB n=100
3 Correct 12 ms 8888 KB n=100
4 Correct 11 ms 8888 KB n=100
5 Correct 11 ms 8928 KB n=100
6 Correct 15 ms 8928 KB n=100
7 Correct 12 ms 8928 KB n=100
8 Correct 11 ms 8928 KB n=100
9 Correct 12 ms 8928 KB n=100
10 Correct 15 ms 8972 KB n=100
11 Correct 12 ms 9020 KB n=100
12 Correct 12 ms 9028 KB n=100
13 Correct 12 ms 9032 KB n=100
14 Correct 12 ms 9092 KB n=100
15 Correct 11 ms 9096 KB n=100
16 Correct 13 ms 9100 KB n=100
17 Correct 11 ms 9104 KB n=100
18 Correct 13 ms 9108 KB n=100
19 Correct 11 ms 9112 KB n=100
20 Correct 13 ms 9116 KB n=100
21 Correct 11 ms 9152 KB n=100
22 Correct 11 ms 9152 KB n=100
23 Correct 13 ms 9152 KB n=100
24 Correct 13 ms 9152 KB n=100
25 Correct 11 ms 9152 KB n=100
26 Correct 10 ms 9152 KB n=12
27 Correct 11 ms 9152 KB n=100
28 Correct 12 ms 9152 KB n=500
29 Correct 16 ms 9288 KB n=500
30 Correct 16 ms 9320 KB n=500
31 Correct 15 ms 9336 KB n=500
32 Correct 11 ms 9368 KB n=500
33 Correct 14 ms 9368 KB n=500
34 Correct 12 ms 9368 KB n=500
35 Correct 15 ms 9428 KB n=500
36 Correct 27 ms 9428 KB n=500
37 Correct 27 ms 9452 KB n=500
38 Correct 27 ms 9452 KB n=500
39 Correct 19 ms 9452 KB n=500
40 Correct 24 ms 9492 KB n=500
41 Correct 19 ms 9492 KB n=500
42 Correct 20 ms 9492 KB n=500
43 Correct 64 ms 9536 KB n=500
44 Correct 18 ms 9564 KB n=500
45 Correct 13 ms 9576 KB n=500
46 Correct 17 ms 9588 KB n=500
47 Correct 15 ms 9604 KB n=500
48 Correct 13 ms 9620 KB n=500
49 Correct 15 ms 9640 KB n=500
50 Correct 14 ms 9640 KB n=500
51 Correct 15 ms 9664 KB n=500
52 Correct 18 ms 9712 KB n=500
53 Correct 14 ms 9712 KB n=500
54 Correct 27 ms 9744 KB n=500
55 Correct 12 ms 9868 KB n=278
56 Correct 76 ms 9892 KB n=500
57 Correct 54 ms 9892 KB n=500
58 Correct 13 ms 9892 KB n=500
59 Correct 14 ms 9932 KB n=2000
60 Correct 100 ms 10180 KB n=2000
61 Correct 89 ms 10296 KB n=2000
62 Correct 77 ms 10296 KB n=2000
63 Correct 15 ms 10296 KB n=2000
64 Correct 95 ms 10376 KB n=2000
65 Correct 16 ms 10376 KB n=2000
66 Correct 124 ms 10568 KB n=2000
67 Correct 16 ms 10568 KB n=2000
68 Correct 85 ms 10568 KB n=2000
69 Correct 270 ms 10568 KB n=2000
70 Correct 331 ms 10616 KB n=2000
71 Correct 294 ms 10716 KB n=2000
72 Correct 209 ms 10716 KB n=2000
73 Correct 185 ms 10824 KB n=2000
74 Correct 19 ms 10824 KB n=1844
75 Correct 156 ms 10824 KB n=2000
76 Correct 174 ms 10848 KB n=2000
77 Correct 180 ms 10900 KB n=2000
78 Correct 183 ms 11024 KB n=2000
79 Correct 18 ms 11072 KB n=2000
80 Correct 81 ms 11180 KB n=2000
81 Correct 81 ms 11252 KB n=2000
82 Correct 17 ms 11252 KB n=2000
83 Correct 80 ms 11340 KB n=2000
84 Correct 17 ms 11340 KB n=2000
85 Correct 63 ms 11456 KB n=2000
86 Correct 47 ms 11456 KB n=2000
87 Correct 19 ms 11456 KB n=2000
88 Correct 912 ms 11808 KB n=2000
89 Correct 918 ms 11924 KB n=2000
90 Correct 211 ms 11924 KB n=2000
91 Correct 23 ms 11924 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 11 ms 8696 KB n=5
2 Correct 11 ms 8696 KB n=100
3 Correct 12 ms 8888 KB n=100
4 Correct 11 ms 8888 KB n=100
5 Correct 11 ms 8928 KB n=100
6 Correct 15 ms 8928 KB n=100
7 Correct 12 ms 8928 KB n=100
8 Correct 11 ms 8928 KB n=100
9 Correct 12 ms 8928 KB n=100
10 Correct 15 ms 8972 KB n=100
11 Correct 12 ms 9020 KB n=100
12 Correct 12 ms 9028 KB n=100
13 Correct 12 ms 9032 KB n=100
14 Correct 12 ms 9092 KB n=100
15 Correct 11 ms 9096 KB n=100
16 Correct 13 ms 9100 KB n=100
17 Correct 11 ms 9104 KB n=100
18 Correct 13 ms 9108 KB n=100
19 Correct 11 ms 9112 KB n=100
20 Correct 13 ms 9116 KB n=100
21 Correct 11 ms 9152 KB n=100
22 Correct 11 ms 9152 KB n=100
23 Correct 13 ms 9152 KB n=100
24 Correct 13 ms 9152 KB n=100
25 Correct 11 ms 9152 KB n=100
26 Correct 10 ms 9152 KB n=12
27 Correct 11 ms 9152 KB n=100
28 Correct 12 ms 9152 KB n=500
29 Correct 16 ms 9288 KB n=500
30 Correct 16 ms 9320 KB n=500
31 Correct 15 ms 9336 KB n=500
32 Correct 11 ms 9368 KB n=500
33 Correct 14 ms 9368 KB n=500
34 Correct 12 ms 9368 KB n=500
35 Correct 15 ms 9428 KB n=500
36 Correct 27 ms 9428 KB n=500
37 Correct 27 ms 9452 KB n=500
38 Correct 27 ms 9452 KB n=500
39 Correct 19 ms 9452 KB n=500
40 Correct 24 ms 9492 KB n=500
41 Correct 19 ms 9492 KB n=500
42 Correct 20 ms 9492 KB n=500
43 Correct 64 ms 9536 KB n=500
44 Correct 18 ms 9564 KB n=500
45 Correct 13 ms 9576 KB n=500
46 Correct 17 ms 9588 KB n=500
47 Correct 15 ms 9604 KB n=500
48 Correct 13 ms 9620 KB n=500
49 Correct 15 ms 9640 KB n=500
50 Correct 14 ms 9640 KB n=500
51 Correct 15 ms 9664 KB n=500
52 Correct 18 ms 9712 KB n=500
53 Correct 14 ms 9712 KB n=500
54 Correct 27 ms 9744 KB n=500
55 Correct 12 ms 9868 KB n=278
56 Correct 76 ms 9892 KB n=500
57 Correct 54 ms 9892 KB n=500
58 Correct 13 ms 9892 KB n=500
59 Correct 14 ms 9932 KB n=2000
60 Correct 100 ms 10180 KB n=2000
61 Correct 89 ms 10296 KB n=2000
62 Correct 77 ms 10296 KB n=2000
63 Correct 15 ms 10296 KB n=2000
64 Correct 95 ms 10376 KB n=2000
65 Correct 16 ms 10376 KB n=2000
66 Correct 124 ms 10568 KB n=2000
67 Correct 16 ms 10568 KB n=2000
68 Correct 85 ms 10568 KB n=2000
69 Correct 270 ms 10568 KB n=2000
70 Correct 331 ms 10616 KB n=2000
71 Correct 294 ms 10716 KB n=2000
72 Correct 209 ms 10716 KB n=2000
73 Correct 185 ms 10824 KB n=2000
74 Correct 19 ms 10824 KB n=1844
75 Correct 156 ms 10824 KB n=2000
76 Correct 174 ms 10848 KB n=2000
77 Correct 180 ms 10900 KB n=2000
78 Correct 183 ms 11024 KB n=2000
79 Correct 18 ms 11072 KB n=2000
80 Correct 81 ms 11180 KB n=2000
81 Correct 81 ms 11252 KB n=2000
82 Correct 17 ms 11252 KB n=2000
83 Correct 80 ms 11340 KB n=2000
84 Correct 17 ms 11340 KB n=2000
85 Correct 63 ms 11456 KB n=2000
86 Correct 47 ms 11456 KB n=2000
87 Correct 19 ms 11456 KB n=2000
88 Correct 912 ms 11808 KB n=2000
89 Correct 918 ms 11924 KB n=2000
90 Correct 211 ms 11924 KB n=2000
91 Correct 23 ms 11924 KB n=2000
92 Correct 632 ms 43576 KB n=200000
93 Execution timed out 4021 ms 51888 KB Time limit exceeded
94 Halted 0 ms 0 KB -