Submission #40438

# Submission time Handle Problem Language Result Execution time Memory
40438 2018-02-01T13:04:43 Z szawinis Birthday gift (IZhO18_treearray) C++14
56 / 100
4000 ms 262144 KB
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
using namespace std;
const int N = 1 << 18;

int n, m, q, a[N];
vector<int> g[N];
set<int> pos[N];

int d[N];
vector<int> anc[N];
void dfs(int u, int p) {
	for(int v: g[u]) if(v != p) {
		d[v] = d[u] + 1;
		anc[v].push_back(u);
		for(int j = 0; 1 << j+1 <= d[v]; j++)
			anc[v].push_back(anc[anc[v][j]][j]);
		dfs(v, u);
	}
}
int lca(int u, int v) {
	if(d[v] > d[u]) swap(u, v);
	if(d[u] > d[v]) {
		for(int j = 0; 1 << j <= d[u] - d[v]; j++)
			if(d[u] - d[v] >> j & 1) u = anc[u][j];
	}
	if(u == v) return u;
	for(int j = 18; j >= 0; j--)
		if(j < anc[u].size() && anc[u][j] != anc[v][j])
			u = anc[u][j], v = anc[v][j];
	return anc[u][0];
}

map<int, set<int> > t[2*N];
void update(int i, int nw, int rm, int idx) {
	i += N;
	t[i][rm].erase(idx);
	t[i][nw].insert(idx);
	i >>= 1;
	while(i >= 1) {
		t[i][rm].erase(idx);
		t[i][nw].insert(idx);
		i >>= 1;
	}
}
int query(int l, int r, int v) {
	int res = -1;
	for(l += N, r += N + 1; l < r; l >>= 1, r >>= 1) {
		if(l & 1) {
			if(!t[l][v].empty()) res = *t[l][v].begin();
			++l;
		}
		if(r & 1) {
			--r;
			if(!t[r][v].empty()) res = *t[r][v].begin();
		}
	}
	return res;
}
int main() {
	scanf("%d %d %d", &n, &m, &q);
	for(int i = 1, u, v; i < n; i++) {
		scanf("%d %d", &u, &v);
		g[u].push_back(v);
		g[v].push_back(u);
	}
	dfs(1, 0);
	for(int i = 1; i <= m; i++) {
		scanf("%d", a+i);
		pos[a[i]].insert(i);
		if(i > 1) t[N+i-1][lca(a[i-1], a[i])].insert(i-1);
	}
	for(int i = N-1; i >= 1; i--) {
		t[i].insert(t[i<<1].begin(), t[i<<1].end());
		for(auto elem: t[i<<1|1]) {
			for(int v: elem.second)
				t[i][elem.first].insert(v);
		}
	}
	for(int z = 0, tp, l, r, v, i; z < q; z++) {
		scanf("%d", &tp);
		if(tp == 1) {
			scanf("%d %d", &i, &v);
			if(i < m) update(i, lca(v, a[i+1]), lca(a[i], a[i+1]), i);
			if(i > 1) update(i-1, lca(a[i-1], v), lca(a[i-1], a[i]), i-1);
			pos[a[i]].erase(i);
			a[i] = v;
			pos[a[i]].insert(i);
		} else {
			scanf("%d %d %d", &l, &r, &v);
			int res = query(l, r-1, v);
			if(res == -1) {
				auto it = pos[v].lower_bound(l);
				if(it == pos[v].end() || *it > r) printf("-1 -1\n");
				else printf("%d %d\n", *it, *it);
			} else printf("%d %d\n", res, res+1);
		}
	}
}

Compilation message

treearray.cpp: In function 'void dfs(int, int)':
treearray.cpp:20:24: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   for(int j = 0; 1 << j+1 <= d[v]; j++)
                        ^
treearray.cpp: In function 'int lca(int, int)':
treearray.cpp:29:12: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
    if(d[u] - d[v] >> j & 1) u = anc[u][j];
            ^
treearray.cpp:33:8: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if(j < anc[u].size() && anc[u][j] != anc[v][j])
        ^
treearray.cpp: In function 'int main()':
treearray.cpp:65:31: 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:67:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &u, &v);
                         ^
treearray.cpp:73:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", a+i);
                   ^
treearray.cpp:85:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &tp);
                   ^
treearray.cpp:87:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d %d", &i, &v);
                          ^
treearray.cpp:94:33: 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 43 ms 49528 KB n=5
2 Correct 43 ms 49760 KB n=100
3 Correct 43 ms 49796 KB n=100
4 Correct 46 ms 49868 KB n=100
5 Correct 47 ms 49940 KB n=100
6 Correct 44 ms 49940 KB n=100
7 Correct 43 ms 49940 KB n=100
8 Correct 47 ms 49940 KB n=100
9 Correct 43 ms 49940 KB n=100
10 Correct 47 ms 49940 KB n=100
11 Correct 46 ms 49996 KB n=100
12 Correct 44 ms 50012 KB n=100
13 Correct 48 ms 50012 KB n=100
14 Correct 47 ms 50012 KB n=100
15 Correct 44 ms 50012 KB n=100
16 Correct 44 ms 50012 KB n=100
17 Correct 49 ms 50012 KB n=100
18 Correct 45 ms 50012 KB n=100
19 Correct 44 ms 50012 KB n=100
20 Correct 45 ms 50012 KB n=100
21 Correct 45 ms 50072 KB n=100
22 Correct 50 ms 50072 KB n=100
23 Correct 49 ms 50072 KB n=100
24 Correct 52 ms 50072 KB n=100
25 Correct 50 ms 50072 KB n=100
26 Correct 47 ms 50072 KB n=12
27 Correct 48 ms 50072 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 43 ms 49528 KB n=5
2 Correct 43 ms 49760 KB n=100
3 Correct 43 ms 49796 KB n=100
4 Correct 46 ms 49868 KB n=100
5 Correct 47 ms 49940 KB n=100
6 Correct 44 ms 49940 KB n=100
7 Correct 43 ms 49940 KB n=100
8 Correct 47 ms 49940 KB n=100
9 Correct 43 ms 49940 KB n=100
10 Correct 47 ms 49940 KB n=100
11 Correct 46 ms 49996 KB n=100
12 Correct 44 ms 50012 KB n=100
13 Correct 48 ms 50012 KB n=100
14 Correct 47 ms 50012 KB n=100
15 Correct 44 ms 50012 KB n=100
16 Correct 44 ms 50012 KB n=100
17 Correct 49 ms 50012 KB n=100
18 Correct 45 ms 50012 KB n=100
19 Correct 44 ms 50012 KB n=100
20 Correct 45 ms 50012 KB n=100
21 Correct 45 ms 50072 KB n=100
22 Correct 50 ms 50072 KB n=100
23 Correct 49 ms 50072 KB n=100
24 Correct 52 ms 50072 KB n=100
25 Correct 50 ms 50072 KB n=100
26 Correct 47 ms 50072 KB n=12
27 Correct 48 ms 50072 KB n=100
28 Correct 48 ms 50396 KB n=500
29 Correct 50 ms 51292 KB n=500
30 Correct 56 ms 51292 KB n=500
31 Correct 52 ms 51292 KB n=500
32 Correct 56 ms 51292 KB n=500
33 Correct 50 ms 51292 KB n=500
34 Correct 46 ms 51292 KB n=500
35 Correct 53 ms 51292 KB n=500
36 Correct 44 ms 51292 KB n=500
37 Correct 46 ms 51292 KB n=500
38 Correct 50 ms 51292 KB n=500
39 Correct 47 ms 51292 KB n=500
40 Correct 47 ms 51292 KB n=500
41 Correct 45 ms 51292 KB n=500
42 Correct 49 ms 51292 KB n=500
43 Correct 50 ms 51292 KB n=500
44 Correct 49 ms 51292 KB n=500
45 Correct 49 ms 51292 KB n=500
46 Correct 51 ms 51292 KB n=500
47 Correct 58 ms 51292 KB n=500
48 Correct 46 ms 51292 KB n=500
49 Correct 50 ms 51292 KB n=500
50 Correct 49 ms 51292 KB n=500
51 Correct 52 ms 51292 KB n=500
52 Correct 51 ms 51292 KB n=500
53 Correct 51 ms 51292 KB n=500
54 Correct 53 ms 51292 KB n=500
55 Correct 47 ms 51292 KB n=278
56 Correct 54 ms 51292 KB n=500
57 Correct 61 ms 51292 KB n=500
58 Correct 55 ms 51292 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 43 ms 49528 KB n=5
2 Correct 43 ms 49760 KB n=100
3 Correct 43 ms 49796 KB n=100
4 Correct 46 ms 49868 KB n=100
5 Correct 47 ms 49940 KB n=100
6 Correct 44 ms 49940 KB n=100
7 Correct 43 ms 49940 KB n=100
8 Correct 47 ms 49940 KB n=100
9 Correct 43 ms 49940 KB n=100
10 Correct 47 ms 49940 KB n=100
11 Correct 46 ms 49996 KB n=100
12 Correct 44 ms 50012 KB n=100
13 Correct 48 ms 50012 KB n=100
14 Correct 47 ms 50012 KB n=100
15 Correct 44 ms 50012 KB n=100
16 Correct 44 ms 50012 KB n=100
17 Correct 49 ms 50012 KB n=100
18 Correct 45 ms 50012 KB n=100
19 Correct 44 ms 50012 KB n=100
20 Correct 45 ms 50012 KB n=100
21 Correct 45 ms 50072 KB n=100
22 Correct 50 ms 50072 KB n=100
23 Correct 49 ms 50072 KB n=100
24 Correct 52 ms 50072 KB n=100
25 Correct 50 ms 50072 KB n=100
26 Correct 47 ms 50072 KB n=12
27 Correct 48 ms 50072 KB n=100
28 Correct 48 ms 50396 KB n=500
29 Correct 50 ms 51292 KB n=500
30 Correct 56 ms 51292 KB n=500
31 Correct 52 ms 51292 KB n=500
32 Correct 56 ms 51292 KB n=500
33 Correct 50 ms 51292 KB n=500
34 Correct 46 ms 51292 KB n=500
35 Correct 53 ms 51292 KB n=500
36 Correct 44 ms 51292 KB n=500
37 Correct 46 ms 51292 KB n=500
38 Correct 50 ms 51292 KB n=500
39 Correct 47 ms 51292 KB n=500
40 Correct 47 ms 51292 KB n=500
41 Correct 45 ms 51292 KB n=500
42 Correct 49 ms 51292 KB n=500
43 Correct 50 ms 51292 KB n=500
44 Correct 49 ms 51292 KB n=500
45 Correct 49 ms 51292 KB n=500
46 Correct 51 ms 51292 KB n=500
47 Correct 58 ms 51292 KB n=500
48 Correct 46 ms 51292 KB n=500
49 Correct 50 ms 51292 KB n=500
50 Correct 49 ms 51292 KB n=500
51 Correct 52 ms 51292 KB n=500
52 Correct 51 ms 51292 KB n=500
53 Correct 51 ms 51292 KB n=500
54 Correct 53 ms 51292 KB n=500
55 Correct 47 ms 51292 KB n=278
56 Correct 54 ms 51292 KB n=500
57 Correct 61 ms 51292 KB n=500
58 Correct 55 ms 51292 KB n=500
59 Correct 63 ms 52444 KB n=2000
60 Correct 85 ms 55992 KB n=2000
61 Correct 86 ms 55992 KB n=2000
62 Correct 80 ms 55992 KB n=2000
63 Correct 66 ms 55992 KB n=2000
64 Correct 79 ms 55992 KB n=2000
65 Correct 76 ms 55992 KB n=2000
66 Correct 89 ms 55992 KB n=2000
67 Correct 71 ms 55992 KB n=2000
68 Correct 85 ms 55992 KB n=2000
69 Correct 53 ms 55992 KB n=2000
70 Correct 57 ms 55992 KB n=2000
71 Correct 51 ms 55992 KB n=2000
72 Correct 79 ms 55992 KB n=2000
73 Correct 54 ms 55992 KB n=2000
74 Correct 64 ms 55992 KB n=1844
75 Correct 54 ms 55992 KB n=2000
76 Correct 74 ms 55992 KB n=2000
77 Correct 69 ms 55992 KB n=2000
78 Correct 70 ms 55992 KB n=2000
79 Correct 70 ms 55992 KB n=2000
80 Correct 85 ms 55992 KB n=2000
81 Correct 84 ms 55992 KB n=2000
82 Correct 68 ms 55992 KB n=2000
83 Correct 86 ms 55992 KB n=2000
84 Correct 69 ms 55992 KB n=2000
85 Correct 81 ms 55992 KB n=2000
86 Correct 84 ms 55992 KB n=2000
87 Correct 67 ms 55992 KB n=2000
88 Correct 82 ms 55992 KB n=2000
89 Correct 80 ms 55992 KB n=2000
90 Correct 86 ms 55992 KB n=2000
91 Correct 57 ms 55992 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 43 ms 49528 KB n=5
2 Correct 43 ms 49760 KB n=100
3 Correct 43 ms 49796 KB n=100
4 Correct 46 ms 49868 KB n=100
5 Correct 47 ms 49940 KB n=100
6 Correct 44 ms 49940 KB n=100
7 Correct 43 ms 49940 KB n=100
8 Correct 47 ms 49940 KB n=100
9 Correct 43 ms 49940 KB n=100
10 Correct 47 ms 49940 KB n=100
11 Correct 46 ms 49996 KB n=100
12 Correct 44 ms 50012 KB n=100
13 Correct 48 ms 50012 KB n=100
14 Correct 47 ms 50012 KB n=100
15 Correct 44 ms 50012 KB n=100
16 Correct 44 ms 50012 KB n=100
17 Correct 49 ms 50012 KB n=100
18 Correct 45 ms 50012 KB n=100
19 Correct 44 ms 50012 KB n=100
20 Correct 45 ms 50012 KB n=100
21 Correct 45 ms 50072 KB n=100
22 Correct 50 ms 50072 KB n=100
23 Correct 49 ms 50072 KB n=100
24 Correct 52 ms 50072 KB n=100
25 Correct 50 ms 50072 KB n=100
26 Correct 47 ms 50072 KB n=12
27 Correct 48 ms 50072 KB n=100
28 Correct 48 ms 50396 KB n=500
29 Correct 50 ms 51292 KB n=500
30 Correct 56 ms 51292 KB n=500
31 Correct 52 ms 51292 KB n=500
32 Correct 56 ms 51292 KB n=500
33 Correct 50 ms 51292 KB n=500
34 Correct 46 ms 51292 KB n=500
35 Correct 53 ms 51292 KB n=500
36 Correct 44 ms 51292 KB n=500
37 Correct 46 ms 51292 KB n=500
38 Correct 50 ms 51292 KB n=500
39 Correct 47 ms 51292 KB n=500
40 Correct 47 ms 51292 KB n=500
41 Correct 45 ms 51292 KB n=500
42 Correct 49 ms 51292 KB n=500
43 Correct 50 ms 51292 KB n=500
44 Correct 49 ms 51292 KB n=500
45 Correct 49 ms 51292 KB n=500
46 Correct 51 ms 51292 KB n=500
47 Correct 58 ms 51292 KB n=500
48 Correct 46 ms 51292 KB n=500
49 Correct 50 ms 51292 KB n=500
50 Correct 49 ms 51292 KB n=500
51 Correct 52 ms 51292 KB n=500
52 Correct 51 ms 51292 KB n=500
53 Correct 51 ms 51292 KB n=500
54 Correct 53 ms 51292 KB n=500
55 Correct 47 ms 51292 KB n=278
56 Correct 54 ms 51292 KB n=500
57 Correct 61 ms 51292 KB n=500
58 Correct 55 ms 51292 KB n=500
59 Correct 63 ms 52444 KB n=2000
60 Correct 85 ms 55992 KB n=2000
61 Correct 86 ms 55992 KB n=2000
62 Correct 80 ms 55992 KB n=2000
63 Correct 66 ms 55992 KB n=2000
64 Correct 79 ms 55992 KB n=2000
65 Correct 76 ms 55992 KB n=2000
66 Correct 89 ms 55992 KB n=2000
67 Correct 71 ms 55992 KB n=2000
68 Correct 85 ms 55992 KB n=2000
69 Correct 53 ms 55992 KB n=2000
70 Correct 57 ms 55992 KB n=2000
71 Correct 51 ms 55992 KB n=2000
72 Correct 79 ms 55992 KB n=2000
73 Correct 54 ms 55992 KB n=2000
74 Correct 64 ms 55992 KB n=1844
75 Correct 54 ms 55992 KB n=2000
76 Correct 74 ms 55992 KB n=2000
77 Correct 69 ms 55992 KB n=2000
78 Correct 70 ms 55992 KB n=2000
79 Correct 70 ms 55992 KB n=2000
80 Correct 85 ms 55992 KB n=2000
81 Correct 84 ms 55992 KB n=2000
82 Correct 68 ms 55992 KB n=2000
83 Correct 86 ms 55992 KB n=2000
84 Correct 69 ms 55992 KB n=2000
85 Correct 81 ms 55992 KB n=2000
86 Correct 84 ms 55992 KB n=2000
87 Correct 67 ms 55992 KB n=2000
88 Correct 82 ms 55992 KB n=2000
89 Correct 80 ms 55992 KB n=2000
90 Correct 86 ms 55992 KB n=2000
91 Correct 57 ms 55992 KB n=2000
92 Execution timed out 4046 ms 262144 KB Time limit exceeded
93 Halted 0 ms 0 KB -