Submission #164845

# Submission time Handle Problem Language Result Execution time Memory
164845 2019-11-23T18:21:01 Z DrSwad Birthday gift (IZhO18_treearray) C++11
100 / 100
1652 ms 84884 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 < m - 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 38 ms 39800 KB n=5
2 Correct 41 ms 39544 KB n=100
3 Correct 42 ms 39544 KB n=100
4 Correct 42 ms 39548 KB n=100
5 Correct 37 ms 39544 KB n=100
6 Correct 42 ms 39468 KB n=100
7 Correct 38 ms 39448 KB n=100
8 Correct 38 ms 39464 KB n=100
9 Correct 38 ms 39544 KB n=100
10 Correct 39 ms 39516 KB n=100
11 Correct 38 ms 39524 KB n=100
12 Correct 42 ms 39544 KB n=100
13 Correct 38 ms 39544 KB n=100
14 Correct 39 ms 39544 KB n=100
15 Correct 38 ms 39516 KB n=100
16 Correct 38 ms 39516 KB n=100
17 Correct 42 ms 39416 KB n=100
18 Correct 38 ms 39544 KB n=100
19 Correct 39 ms 39544 KB n=100
20 Correct 38 ms 39544 KB n=100
21 Correct 38 ms 39548 KB n=100
22 Correct 39 ms 39572 KB n=100
23 Correct 39 ms 39544 KB n=100
24 Correct 38 ms 39416 KB n=100
25 Correct 39 ms 39568 KB n=100
26 Correct 37 ms 39416 KB n=12
27 Correct 37 ms 39544 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 38 ms 39800 KB n=5
2 Correct 41 ms 39544 KB n=100
3 Correct 42 ms 39544 KB n=100
4 Correct 42 ms 39548 KB n=100
5 Correct 37 ms 39544 KB n=100
6 Correct 42 ms 39468 KB n=100
7 Correct 38 ms 39448 KB n=100
8 Correct 38 ms 39464 KB n=100
9 Correct 38 ms 39544 KB n=100
10 Correct 39 ms 39516 KB n=100
11 Correct 38 ms 39524 KB n=100
12 Correct 42 ms 39544 KB n=100
13 Correct 38 ms 39544 KB n=100
14 Correct 39 ms 39544 KB n=100
15 Correct 38 ms 39516 KB n=100
16 Correct 38 ms 39516 KB n=100
17 Correct 42 ms 39416 KB n=100
18 Correct 38 ms 39544 KB n=100
19 Correct 39 ms 39544 KB n=100
20 Correct 38 ms 39544 KB n=100
21 Correct 38 ms 39548 KB n=100
22 Correct 39 ms 39572 KB n=100
23 Correct 39 ms 39544 KB n=100
24 Correct 38 ms 39416 KB n=100
25 Correct 39 ms 39568 KB n=100
26 Correct 37 ms 39416 KB n=12
27 Correct 37 ms 39544 KB n=100
28 Correct 80 ms 39544 KB n=500
29 Correct 39 ms 39544 KB n=500
30 Correct 39 ms 39548 KB n=500
31 Correct 41 ms 39544 KB n=500
32 Correct 39 ms 39544 KB n=500
33 Correct 39 ms 39544 KB n=500
34 Correct 39 ms 39544 KB n=500
35 Correct 65 ms 39608 KB n=500
36 Correct 39 ms 39672 KB n=500
37 Correct 39 ms 39548 KB n=500
38 Correct 39 ms 39544 KB n=500
39 Correct 39 ms 39548 KB n=500
40 Correct 42 ms 39544 KB n=500
41 Correct 39 ms 39544 KB n=500
42 Correct 40 ms 39544 KB n=500
43 Correct 39 ms 39544 KB n=500
44 Correct 39 ms 39544 KB n=500
45 Correct 39 ms 39520 KB n=500
46 Correct 39 ms 39544 KB n=500
47 Correct 68 ms 39544 KB n=500
48 Correct 39 ms 39544 KB n=500
49 Correct 39 ms 39544 KB n=500
50 Correct 40 ms 39548 KB n=500
51 Correct 39 ms 39544 KB n=500
52 Correct 39 ms 39544 KB n=500
53 Correct 40 ms 39672 KB n=500
54 Correct 48 ms 39672 KB n=500
55 Correct 39 ms 39544 KB n=278
56 Correct 38 ms 39544 KB n=500
57 Correct 39 ms 39544 KB n=500
58 Correct 38 ms 39536 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 38 ms 39800 KB n=5
2 Correct 41 ms 39544 KB n=100
3 Correct 42 ms 39544 KB n=100
4 Correct 42 ms 39548 KB n=100
5 Correct 37 ms 39544 KB n=100
6 Correct 42 ms 39468 KB n=100
7 Correct 38 ms 39448 KB n=100
8 Correct 38 ms 39464 KB n=100
9 Correct 38 ms 39544 KB n=100
10 Correct 39 ms 39516 KB n=100
11 Correct 38 ms 39524 KB n=100
12 Correct 42 ms 39544 KB n=100
13 Correct 38 ms 39544 KB n=100
14 Correct 39 ms 39544 KB n=100
15 Correct 38 ms 39516 KB n=100
16 Correct 38 ms 39516 KB n=100
17 Correct 42 ms 39416 KB n=100
18 Correct 38 ms 39544 KB n=100
19 Correct 39 ms 39544 KB n=100
20 Correct 38 ms 39544 KB n=100
21 Correct 38 ms 39548 KB n=100
22 Correct 39 ms 39572 KB n=100
23 Correct 39 ms 39544 KB n=100
24 Correct 38 ms 39416 KB n=100
25 Correct 39 ms 39568 KB n=100
26 Correct 37 ms 39416 KB n=12
27 Correct 37 ms 39544 KB n=100
28 Correct 80 ms 39544 KB n=500
29 Correct 39 ms 39544 KB n=500
30 Correct 39 ms 39548 KB n=500
31 Correct 41 ms 39544 KB n=500
32 Correct 39 ms 39544 KB n=500
33 Correct 39 ms 39544 KB n=500
34 Correct 39 ms 39544 KB n=500
35 Correct 65 ms 39608 KB n=500
36 Correct 39 ms 39672 KB n=500
37 Correct 39 ms 39548 KB n=500
38 Correct 39 ms 39544 KB n=500
39 Correct 39 ms 39548 KB n=500
40 Correct 42 ms 39544 KB n=500
41 Correct 39 ms 39544 KB n=500
42 Correct 40 ms 39544 KB n=500
43 Correct 39 ms 39544 KB n=500
44 Correct 39 ms 39544 KB n=500
45 Correct 39 ms 39520 KB n=500
46 Correct 39 ms 39544 KB n=500
47 Correct 68 ms 39544 KB n=500
48 Correct 39 ms 39544 KB n=500
49 Correct 39 ms 39544 KB n=500
50 Correct 40 ms 39548 KB n=500
51 Correct 39 ms 39544 KB n=500
52 Correct 39 ms 39544 KB n=500
53 Correct 40 ms 39672 KB n=500
54 Correct 48 ms 39672 KB n=500
55 Correct 39 ms 39544 KB n=278
56 Correct 38 ms 39544 KB n=500
57 Correct 39 ms 39544 KB n=500
58 Correct 38 ms 39536 KB n=500
59 Correct 43 ms 39892 KB n=2000
60 Correct 43 ms 39928 KB n=2000
61 Correct 43 ms 39928 KB n=2000
62 Correct 44 ms 39800 KB n=2000
63 Correct 43 ms 39800 KB n=2000
64 Correct 43 ms 39800 KB n=2000
65 Correct 45 ms 39800 KB n=2000
66 Correct 43 ms 39928 KB n=2000
67 Correct 43 ms 39800 KB n=2000
68 Correct 42 ms 39800 KB n=2000
69 Correct 42 ms 39828 KB n=2000
70 Correct 42 ms 39800 KB n=2000
71 Correct 41 ms 39800 KB n=2000
72 Correct 41 ms 39928 KB n=2000
73 Correct 41 ms 39800 KB n=2000
74 Correct 53 ms 39800 KB n=1844
75 Correct 43 ms 39800 KB n=2000
76 Correct 43 ms 39800 KB n=2000
77 Correct 44 ms 39800 KB n=2000
78 Correct 47 ms 39800 KB n=2000
79 Correct 45 ms 39800 KB n=2000
80 Correct 43 ms 39932 KB n=2000
81 Correct 44 ms 39884 KB n=2000
82 Correct 42 ms 39804 KB n=2000
83 Correct 44 ms 39928 KB n=2000
84 Correct 42 ms 39804 KB n=2000
85 Correct 44 ms 39804 KB n=2000
86 Correct 47 ms 39928 KB n=2000
87 Correct 44 ms 39928 KB n=2000
88 Correct 43 ms 39928 KB n=2000
89 Correct 45 ms 39928 KB n=2000
90 Correct 43 ms 39900 KB n=2000
91 Correct 42 ms 39800 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 38 ms 39800 KB n=5
2 Correct 41 ms 39544 KB n=100
3 Correct 42 ms 39544 KB n=100
4 Correct 42 ms 39548 KB n=100
5 Correct 37 ms 39544 KB n=100
6 Correct 42 ms 39468 KB n=100
7 Correct 38 ms 39448 KB n=100
8 Correct 38 ms 39464 KB n=100
9 Correct 38 ms 39544 KB n=100
10 Correct 39 ms 39516 KB n=100
11 Correct 38 ms 39524 KB n=100
12 Correct 42 ms 39544 KB n=100
13 Correct 38 ms 39544 KB n=100
14 Correct 39 ms 39544 KB n=100
15 Correct 38 ms 39516 KB n=100
16 Correct 38 ms 39516 KB n=100
17 Correct 42 ms 39416 KB n=100
18 Correct 38 ms 39544 KB n=100
19 Correct 39 ms 39544 KB n=100
20 Correct 38 ms 39544 KB n=100
21 Correct 38 ms 39548 KB n=100
22 Correct 39 ms 39572 KB n=100
23 Correct 39 ms 39544 KB n=100
24 Correct 38 ms 39416 KB n=100
25 Correct 39 ms 39568 KB n=100
26 Correct 37 ms 39416 KB n=12
27 Correct 37 ms 39544 KB n=100
28 Correct 80 ms 39544 KB n=500
29 Correct 39 ms 39544 KB n=500
30 Correct 39 ms 39548 KB n=500
31 Correct 41 ms 39544 KB n=500
32 Correct 39 ms 39544 KB n=500
33 Correct 39 ms 39544 KB n=500
34 Correct 39 ms 39544 KB n=500
35 Correct 65 ms 39608 KB n=500
36 Correct 39 ms 39672 KB n=500
37 Correct 39 ms 39548 KB n=500
38 Correct 39 ms 39544 KB n=500
39 Correct 39 ms 39548 KB n=500
40 Correct 42 ms 39544 KB n=500
41 Correct 39 ms 39544 KB n=500
42 Correct 40 ms 39544 KB n=500
43 Correct 39 ms 39544 KB n=500
44 Correct 39 ms 39544 KB n=500
45 Correct 39 ms 39520 KB n=500
46 Correct 39 ms 39544 KB n=500
47 Correct 68 ms 39544 KB n=500
48 Correct 39 ms 39544 KB n=500
49 Correct 39 ms 39544 KB n=500
50 Correct 40 ms 39548 KB n=500
51 Correct 39 ms 39544 KB n=500
52 Correct 39 ms 39544 KB n=500
53 Correct 40 ms 39672 KB n=500
54 Correct 48 ms 39672 KB n=500
55 Correct 39 ms 39544 KB n=278
56 Correct 38 ms 39544 KB n=500
57 Correct 39 ms 39544 KB n=500
58 Correct 38 ms 39536 KB n=500
59 Correct 43 ms 39892 KB n=2000
60 Correct 43 ms 39928 KB n=2000
61 Correct 43 ms 39928 KB n=2000
62 Correct 44 ms 39800 KB n=2000
63 Correct 43 ms 39800 KB n=2000
64 Correct 43 ms 39800 KB n=2000
65 Correct 45 ms 39800 KB n=2000
66 Correct 43 ms 39928 KB n=2000
67 Correct 43 ms 39800 KB n=2000
68 Correct 42 ms 39800 KB n=2000
69 Correct 42 ms 39828 KB n=2000
70 Correct 42 ms 39800 KB n=2000
71 Correct 41 ms 39800 KB n=2000
72 Correct 41 ms 39928 KB n=2000
73 Correct 41 ms 39800 KB n=2000
74 Correct 53 ms 39800 KB n=1844
75 Correct 43 ms 39800 KB n=2000
76 Correct 43 ms 39800 KB n=2000
77 Correct 44 ms 39800 KB n=2000
78 Correct 47 ms 39800 KB n=2000
79 Correct 45 ms 39800 KB n=2000
80 Correct 43 ms 39932 KB n=2000
81 Correct 44 ms 39884 KB n=2000
82 Correct 42 ms 39804 KB n=2000
83 Correct 44 ms 39928 KB n=2000
84 Correct 42 ms 39804 KB n=2000
85 Correct 44 ms 39804 KB n=2000
86 Correct 47 ms 39928 KB n=2000
87 Correct 44 ms 39928 KB n=2000
88 Correct 43 ms 39928 KB n=2000
89 Correct 45 ms 39928 KB n=2000
90 Correct 43 ms 39900 KB n=2000
91 Correct 42 ms 39800 KB n=2000
92 Correct 1110 ms 75436 KB n=200000
93 Correct 1505 ms 80536 KB n=200000
94 Correct 1393 ms 83628 KB n=200000
95 Correct 1133 ms 75216 KB n=200000
96 Correct 1058 ms 75304 KB n=200000
97 Correct 1573 ms 79452 KB n=200000
98 Correct 1095 ms 75292 KB n=200000
99 Correct 1337 ms 75472 KB n=200000
100 Correct 1095 ms 75340 KB n=200000
101 Correct 1400 ms 84884 KB n=200000
102 Correct 695 ms 76400 KB n=200000
103 Correct 637 ms 76496 KB n=200000
104 Correct 638 ms 76536 KB n=200000
105 Correct 650 ms 76792 KB n=200000
106 Correct 654 ms 76872 KB n=200000
107 Correct 677 ms 76920 KB n=200000
108 Correct 1229 ms 75464 KB n=200000
109 Correct 1243 ms 75452 KB n=200000
110 Correct 1272 ms 75512 KB n=200000
111 Correct 1082 ms 74916 KB n=200000
112 Correct 1367 ms 84040 KB n=200000
113 Correct 1502 ms 79576 KB n=200000
114 Correct 1047 ms 74864 KB n=200000
115 Correct 1628 ms 77432 KB n=200000
116 Correct 1089 ms 75548 KB n=200000
117 Correct 1468 ms 84392 KB n=200000
118 Correct 1652 ms 78328 KB n=200000
119 Correct 1140 ms 75416 KB n=200000
120 Correct 1409 ms 83960 KB n=200000
121 Correct 1415 ms 83932 KB n=200000
122 Correct 1482 ms 84356 KB n=200000
123 Correct 681 ms 76744 KB n=200000
124 Correct 349 ms 52444 KB n=25264