Submission #159270

# Submission time Handle Problem Language Result Execution time Memory
159270 2019-10-21T23:21:49 Z DrSwad Birthday gift (IZhO18_treearray) C++11
56 / 100
1427 ms 84856 KB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "debug.h"
#endif

typedef long long lint;
typedef unsigned int uint;

typedef pair<int, int> pii;
#define x first
#define y second

#define size(a) (int)a.size()

const int N = int(2e5) + 10, LOGN = 20;

int n, m, q;
vector<int> adj[N];
int a[N], lv[N], par[N], P[LOGN][N];
set<int> a_pos[N], lca_pos[N];

int lca(int u, int v) {
    if (lv[u] < lv[v]) {
        swap(u, v);
    }
    for (int i = LOGN - 1; i >= 0; i--) {
        if (lv[u] - (1 << i) >= lv[v]) {
            u = P[i][u];
        }
    }
    if (u == v) {
        return u;
    }
    for (int i = LOGN - 1; i >= 0; i--) {
        if (P[i][u] != P[i][v]) {
            u = P[i][u];
            v = P[i][v];
        }
    }
    return par[u];
}

void initSp() {
    memset(P, -1, sizeof P);
    for (int i = 0; i < n; i++) {
        P[0][i] = par[i];
    }
    for (int i = 1; i < LOGN; i++) {
        for (int j = 0; j < n; j++) {
            if (P[i - 1][j] != -1) {
                P[i][j] = P[i - 1][P[i - 1][j]];
            }
        }
    }
}

void dfs(int at, int p) {
	par[at] = p;
	lv[at] = (p >= 0 ? lv[p] : -1) + 1;
	for (int nxt : adj[at]) {
		if (nxt != p) dfs(nxt, at);
	}
}

int main() {
	#ifdef LOCAL
	freopen("in", "r", stdin);
	freopen("out", "w", stdout);
	#endif

	cin >> n >> m >> q;
	for (int i = 0; i < n - 1; i++) {
		int u, v;
		scanf("%d %d", &u, &v);
		u--, v--;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}

	dfs(0, -1);
	initSp();

	for(int i = 0; i < m; i++) {
		scanf("%d", &a[i]);
		a[i]--;
		a_pos[a[i]].insert(i);
		if (i > 0) lca_pos[lca(a[i - 1], a[i])].insert(i - 1);
	}

	for(int i = 0; i < q; i++) {
		int type;
		scanf("%d", &type);

		if (type == 1) {
			int pos, v;
			scanf("%d %d", &pos, &v);
			pos--, v--;

			a_pos[a[pos]].erase(pos);
			a_pos[v].insert(pos);

			if (pos > 0) {
				lca_pos[lca(a[pos - 1], a[pos])].erase(pos - 1);
				lca_pos[lca(a[pos - 1], v)].insert(pos - 1);
			}

			if (pos < n - 1) {
				lca_pos[lca(a[pos], a[pos + 1])].erase(pos);
				lca_pos[lca(v, a[pos + 1])].insert(pos);
			}

			a[pos] = v;
		}
		else {
			int l, r, v;
			scanf("%d %d %d", &l, &r, &v);
			l--, r--, v--;

			auto it = a_pos[v].lower_bound(l);
			if (it != a_pos[v].end() && *it <= r) {
				printf("%d %d\n", *it + 1, *it + 1);
				continue;
			}

			if (r > l) {
				it = lca_pos[v].lower_bound(l);
				if (it != lca_pos[v].end() && *it <= r - 1) {
					printf("%d %d\n", *it + 1, *it + 2);
					continue;
				}
			}

			printf("-1 -1\n");
		}
	}

	return 0;
}

Compilation message

treearray.cpp: In function 'int main()':
treearray.cpp:77: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:87:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &a[i]);
   ~~~~~^~~~~~~~~~~~~
treearray.cpp:95:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &type);
   ~~~~~^~~~~~~~~~~~~
treearray.cpp:99: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:119: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);
    ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 37 ms 39416 KB n=5
2 Correct 38 ms 39548 KB n=100
3 Correct 43 ms 39416 KB n=100
4 Correct 37 ms 39544 KB n=100
5 Correct 43 ms 39544 KB n=100
6 Correct 37 ms 39416 KB n=100
7 Correct 37 ms 39544 KB n=100
8 Correct 37 ms 39544 KB n=100
9 Correct 38 ms 39416 KB n=100
10 Correct 38 ms 39416 KB n=100
11 Correct 37 ms 39416 KB n=100
12 Correct 37 ms 39420 KB n=100
13 Correct 37 ms 39512 KB n=100
14 Correct 37 ms 39520 KB n=100
15 Correct 37 ms 39464 KB n=100
16 Correct 37 ms 39416 KB n=100
17 Correct 37 ms 39544 KB n=100
18 Correct 37 ms 39504 KB n=100
19 Correct 37 ms 39524 KB n=100
20 Correct 37 ms 39544 KB n=100
21 Correct 37 ms 39416 KB n=100
22 Correct 37 ms 39544 KB n=100
23 Correct 37 ms 39548 KB n=100
24 Correct 37 ms 39544 KB n=100
25 Correct 37 ms 39548 KB n=100
26 Correct 37 ms 39544 KB n=12
27 Correct 37 ms 39544 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 37 ms 39416 KB n=5
2 Correct 38 ms 39548 KB n=100
3 Correct 43 ms 39416 KB n=100
4 Correct 37 ms 39544 KB n=100
5 Correct 43 ms 39544 KB n=100
6 Correct 37 ms 39416 KB n=100
7 Correct 37 ms 39544 KB n=100
8 Correct 37 ms 39544 KB n=100
9 Correct 38 ms 39416 KB n=100
10 Correct 38 ms 39416 KB n=100
11 Correct 37 ms 39416 KB n=100
12 Correct 37 ms 39420 KB n=100
13 Correct 37 ms 39512 KB n=100
14 Correct 37 ms 39520 KB n=100
15 Correct 37 ms 39464 KB n=100
16 Correct 37 ms 39416 KB n=100
17 Correct 37 ms 39544 KB n=100
18 Correct 37 ms 39504 KB n=100
19 Correct 37 ms 39524 KB n=100
20 Correct 37 ms 39544 KB n=100
21 Correct 37 ms 39416 KB n=100
22 Correct 37 ms 39544 KB n=100
23 Correct 37 ms 39548 KB n=100
24 Correct 37 ms 39544 KB n=100
25 Correct 37 ms 39548 KB n=100
26 Correct 37 ms 39544 KB n=12
27 Correct 37 ms 39544 KB n=100
28 Correct 38 ms 39544 KB n=500
29 Correct 38 ms 39544 KB n=500
30 Correct 38 ms 39544 KB n=500
31 Correct 38 ms 39544 KB n=500
32 Correct 39 ms 39544 KB n=500
33 Correct 38 ms 39544 KB n=500
34 Correct 37 ms 39672 KB n=500
35 Correct 38 ms 39548 KB n=500
36 Correct 38 ms 39544 KB n=500
37 Correct 37 ms 39544 KB n=500
38 Correct 38 ms 39508 KB n=500
39 Correct 37 ms 39544 KB n=500
40 Correct 38 ms 39544 KB n=500
41 Correct 38 ms 39544 KB n=500
42 Correct 38 ms 39544 KB n=500
43 Correct 38 ms 39544 KB n=500
44 Correct 38 ms 39544 KB n=500
45 Correct 38 ms 39544 KB n=500
46 Correct 39 ms 39516 KB n=500
47 Correct 38 ms 39656 KB n=500
48 Correct 38 ms 39544 KB n=500
49 Correct 38 ms 39544 KB n=500
50 Correct 38 ms 39544 KB n=500
51 Correct 38 ms 39544 KB n=500
52 Correct 38 ms 39544 KB n=500
53 Correct 37 ms 39544 KB n=500
54 Correct 37 ms 39544 KB n=500
55 Correct 38 ms 39516 KB n=278
56 Correct 38 ms 39544 KB n=500
57 Correct 38 ms 39544 KB n=500
58 Correct 37 ms 39612 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 37 ms 39416 KB n=5
2 Correct 38 ms 39548 KB n=100
3 Correct 43 ms 39416 KB n=100
4 Correct 37 ms 39544 KB n=100
5 Correct 43 ms 39544 KB n=100
6 Correct 37 ms 39416 KB n=100
7 Correct 37 ms 39544 KB n=100
8 Correct 37 ms 39544 KB n=100
9 Correct 38 ms 39416 KB n=100
10 Correct 38 ms 39416 KB n=100
11 Correct 37 ms 39416 KB n=100
12 Correct 37 ms 39420 KB n=100
13 Correct 37 ms 39512 KB n=100
14 Correct 37 ms 39520 KB n=100
15 Correct 37 ms 39464 KB n=100
16 Correct 37 ms 39416 KB n=100
17 Correct 37 ms 39544 KB n=100
18 Correct 37 ms 39504 KB n=100
19 Correct 37 ms 39524 KB n=100
20 Correct 37 ms 39544 KB n=100
21 Correct 37 ms 39416 KB n=100
22 Correct 37 ms 39544 KB n=100
23 Correct 37 ms 39548 KB n=100
24 Correct 37 ms 39544 KB n=100
25 Correct 37 ms 39548 KB n=100
26 Correct 37 ms 39544 KB n=12
27 Correct 37 ms 39544 KB n=100
28 Correct 38 ms 39544 KB n=500
29 Correct 38 ms 39544 KB n=500
30 Correct 38 ms 39544 KB n=500
31 Correct 38 ms 39544 KB n=500
32 Correct 39 ms 39544 KB n=500
33 Correct 38 ms 39544 KB n=500
34 Correct 37 ms 39672 KB n=500
35 Correct 38 ms 39548 KB n=500
36 Correct 38 ms 39544 KB n=500
37 Correct 37 ms 39544 KB n=500
38 Correct 38 ms 39508 KB n=500
39 Correct 37 ms 39544 KB n=500
40 Correct 38 ms 39544 KB n=500
41 Correct 38 ms 39544 KB n=500
42 Correct 38 ms 39544 KB n=500
43 Correct 38 ms 39544 KB n=500
44 Correct 38 ms 39544 KB n=500
45 Correct 38 ms 39544 KB n=500
46 Correct 39 ms 39516 KB n=500
47 Correct 38 ms 39656 KB n=500
48 Correct 38 ms 39544 KB n=500
49 Correct 38 ms 39544 KB n=500
50 Correct 38 ms 39544 KB n=500
51 Correct 38 ms 39544 KB n=500
52 Correct 38 ms 39544 KB n=500
53 Correct 37 ms 39544 KB n=500
54 Correct 37 ms 39544 KB n=500
55 Correct 38 ms 39516 KB n=278
56 Correct 38 ms 39544 KB n=500
57 Correct 38 ms 39544 KB n=500
58 Correct 37 ms 39612 KB n=500
59 Correct 42 ms 39836 KB n=2000
60 Correct 41 ms 39928 KB n=2000
61 Correct 42 ms 39928 KB n=2000
62 Correct 42 ms 39808 KB n=2000
63 Correct 42 ms 39800 KB n=2000
64 Correct 42 ms 39800 KB n=2000
65 Correct 42 ms 39932 KB n=2000
66 Correct 42 ms 39800 KB n=2000
67 Correct 42 ms 39800 KB n=2000
68 Correct 42 ms 39900 KB n=2000
69 Correct 40 ms 39800 KB n=2000
70 Correct 40 ms 39804 KB n=2000
71 Correct 40 ms 39800 KB n=2000
72 Correct 39 ms 39800 KB n=2000
73 Correct 40 ms 39800 KB n=2000
74 Correct 41 ms 39848 KB n=1844
75 Correct 40 ms 39800 KB n=2000
76 Correct 42 ms 39800 KB n=2000
77 Correct 41 ms 39800 KB n=2000
78 Correct 41 ms 39800 KB n=2000
79 Correct 41 ms 39800 KB n=2000
80 Correct 41 ms 39928 KB n=2000
81 Correct 42 ms 39808 KB n=2000
82 Correct 41 ms 39892 KB n=2000
83 Correct 41 ms 39928 KB n=2000
84 Correct 40 ms 39800 KB n=2000
85 Correct 41 ms 39800 KB n=2000
86 Correct 42 ms 39800 KB n=2000
87 Correct 41 ms 39800 KB n=2000
88 Correct 41 ms 39912 KB n=2000
89 Correct 41 ms 39928 KB n=2000
90 Correct 41 ms 39932 KB n=2000
91 Correct 40 ms 39800 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 37 ms 39416 KB n=5
2 Correct 38 ms 39548 KB n=100
3 Correct 43 ms 39416 KB n=100
4 Correct 37 ms 39544 KB n=100
5 Correct 43 ms 39544 KB n=100
6 Correct 37 ms 39416 KB n=100
7 Correct 37 ms 39544 KB n=100
8 Correct 37 ms 39544 KB n=100
9 Correct 38 ms 39416 KB n=100
10 Correct 38 ms 39416 KB n=100
11 Correct 37 ms 39416 KB n=100
12 Correct 37 ms 39420 KB n=100
13 Correct 37 ms 39512 KB n=100
14 Correct 37 ms 39520 KB n=100
15 Correct 37 ms 39464 KB n=100
16 Correct 37 ms 39416 KB n=100
17 Correct 37 ms 39544 KB n=100
18 Correct 37 ms 39504 KB n=100
19 Correct 37 ms 39524 KB n=100
20 Correct 37 ms 39544 KB n=100
21 Correct 37 ms 39416 KB n=100
22 Correct 37 ms 39544 KB n=100
23 Correct 37 ms 39548 KB n=100
24 Correct 37 ms 39544 KB n=100
25 Correct 37 ms 39548 KB n=100
26 Correct 37 ms 39544 KB n=12
27 Correct 37 ms 39544 KB n=100
28 Correct 38 ms 39544 KB n=500
29 Correct 38 ms 39544 KB n=500
30 Correct 38 ms 39544 KB n=500
31 Correct 38 ms 39544 KB n=500
32 Correct 39 ms 39544 KB n=500
33 Correct 38 ms 39544 KB n=500
34 Correct 37 ms 39672 KB n=500
35 Correct 38 ms 39548 KB n=500
36 Correct 38 ms 39544 KB n=500
37 Correct 37 ms 39544 KB n=500
38 Correct 38 ms 39508 KB n=500
39 Correct 37 ms 39544 KB n=500
40 Correct 38 ms 39544 KB n=500
41 Correct 38 ms 39544 KB n=500
42 Correct 38 ms 39544 KB n=500
43 Correct 38 ms 39544 KB n=500
44 Correct 38 ms 39544 KB n=500
45 Correct 38 ms 39544 KB n=500
46 Correct 39 ms 39516 KB n=500
47 Correct 38 ms 39656 KB n=500
48 Correct 38 ms 39544 KB n=500
49 Correct 38 ms 39544 KB n=500
50 Correct 38 ms 39544 KB n=500
51 Correct 38 ms 39544 KB n=500
52 Correct 38 ms 39544 KB n=500
53 Correct 37 ms 39544 KB n=500
54 Correct 37 ms 39544 KB n=500
55 Correct 38 ms 39516 KB n=278
56 Correct 38 ms 39544 KB n=500
57 Correct 38 ms 39544 KB n=500
58 Correct 37 ms 39612 KB n=500
59 Correct 42 ms 39836 KB n=2000
60 Correct 41 ms 39928 KB n=2000
61 Correct 42 ms 39928 KB n=2000
62 Correct 42 ms 39808 KB n=2000
63 Correct 42 ms 39800 KB n=2000
64 Correct 42 ms 39800 KB n=2000
65 Correct 42 ms 39932 KB n=2000
66 Correct 42 ms 39800 KB n=2000
67 Correct 42 ms 39800 KB n=2000
68 Correct 42 ms 39900 KB n=2000
69 Correct 40 ms 39800 KB n=2000
70 Correct 40 ms 39804 KB n=2000
71 Correct 40 ms 39800 KB n=2000
72 Correct 39 ms 39800 KB n=2000
73 Correct 40 ms 39800 KB n=2000
74 Correct 41 ms 39848 KB n=1844
75 Correct 40 ms 39800 KB n=2000
76 Correct 42 ms 39800 KB n=2000
77 Correct 41 ms 39800 KB n=2000
78 Correct 41 ms 39800 KB n=2000
79 Correct 41 ms 39800 KB n=2000
80 Correct 41 ms 39928 KB n=2000
81 Correct 42 ms 39808 KB n=2000
82 Correct 41 ms 39892 KB n=2000
83 Correct 41 ms 39928 KB n=2000
84 Correct 40 ms 39800 KB n=2000
85 Correct 41 ms 39800 KB n=2000
86 Correct 42 ms 39800 KB n=2000
87 Correct 41 ms 39800 KB n=2000
88 Correct 41 ms 39912 KB n=2000
89 Correct 41 ms 39928 KB n=2000
90 Correct 41 ms 39932 KB n=2000
91 Correct 40 ms 39800 KB n=2000
92 Correct 953 ms 75456 KB n=200000
93 Correct 1201 ms 80448 KB n=200000
94 Correct 1098 ms 83700 KB n=200000
95 Correct 1028 ms 75148 KB n=200000
96 Correct 919 ms 75216 KB n=200000
97 Correct 1296 ms 79392 KB n=200000
98 Correct 959 ms 75372 KB n=200000
99 Correct 1214 ms 75432 KB n=200000
100 Correct 920 ms 75184 KB n=200000
101 Correct 1181 ms 84856 KB n=200000
102 Correct 609 ms 76356 KB n=200000
103 Correct 587 ms 76428 KB n=200000
104 Correct 555 ms 76576 KB n=200000
105 Correct 544 ms 76792 KB n=200000
106 Correct 566 ms 76688 KB n=200000
107 Correct 607 ms 76936 KB n=200000
108 Correct 1086 ms 75368 KB n=200000
109 Correct 1103 ms 75292 KB n=200000
110 Correct 1090 ms 75380 KB n=200000
111 Correct 950 ms 74828 KB n=200000
112 Correct 1166 ms 83888 KB n=200000
113 Correct 1241 ms 79480 KB n=200000
114 Correct 998 ms 74720 KB n=200000
115 Correct 1303 ms 77252 KB n=200000
116 Correct 1004 ms 75412 KB n=200000
117 Correct 1261 ms 84216 KB n=200000
118 Correct 1382 ms 78220 KB n=200000
119 Correct 985 ms 75344 KB n=200000
120 Correct 1092 ms 83908 KB n=200000
121 Correct 1427 ms 83984 KB n=200000
122 Correct 1240 ms 84160 KB n=200000
123 Correct 561 ms 76652 KB n=200000
124 Incorrect 276 ms 52724 KB Wrong range.