답안 #857261

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
857261 2023-10-05T17:07:10 Z dknguyen176 Birthday gift (IZhO18_treearray) C++14
100 / 100
814 ms 84932 KB
/*
	Author: Nayeemul Islam Swad

	Idea:
		- For some query l r v, if we find an x such that a[x] = v, then
		  we can just set y = x and thus have found a solution (x, y).

		- If there is no such x, then a pair (x, y) would be a solution iff
		  l <= x <= y <= r and there exists two distinct subtrees of v such
		  that
		    * All a[i] lies in one of these subtrees
		    * Each subtree has at least one a[i] between [x, y]
		  Now, suppose we're iterating from x to y and keep track of which
		  subtree we're currently in of the two subtrees. Then, there must
		  be a point when we're change the current subtree to the other one.
		  This implies that if there exists a solution (x, y) with  x != y,
		  then there also is a solution (x0, y0) such that y0 = x0 + 1.

		- So, we just maintain all the lca's of consecutive elements of the
		  array and answer queries of second types using them.
*/

#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:99:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   99 |   scanf("%d %d", &u, &v);
      |   ~~~~~^~~~~~~~~~~~~~~~~
treearray.cpp:109:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  109 |   scanf("%d", &a[i]);
      |   ~~~~~^~~~~~~~~~~~~
treearray.cpp:117:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  117 |   scanf("%d", &type);
      |   ~~~~~^~~~~~~~~~~~~
treearray.cpp:121:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  121 |    scanf("%d %d", &pos, &v);
      |    ~~~~~^~~~~~~~~~~~~~~~~~~
treearray.cpp:141:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  141 |    scanf("%d %d %d", &l, &r, &v);
      |    ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 41816 KB n=5
2 Correct 7 ms 41820 KB n=100
3 Correct 7 ms 41820 KB n=100
4 Correct 7 ms 41820 KB n=100
5 Correct 7 ms 41820 KB n=100
6 Correct 7 ms 41936 KB n=100
7 Correct 7 ms 41820 KB n=100
8 Correct 7 ms 42072 KB n=100
9 Correct 8 ms 41820 KB n=100
10 Correct 7 ms 41820 KB n=100
11 Correct 7 ms 41820 KB n=100
12 Correct 7 ms 41816 KB n=100
13 Correct 7 ms 41820 KB n=100
14 Correct 7 ms 41820 KB n=100
15 Correct 7 ms 41820 KB n=100
16 Correct 8 ms 41820 KB n=100
17 Correct 8 ms 41940 KB n=100
18 Correct 7 ms 41820 KB n=100
19 Correct 7 ms 41920 KB n=100
20 Correct 7 ms 41820 KB n=100
21 Correct 7 ms 41820 KB n=100
22 Correct 7 ms 41940 KB n=100
23 Correct 7 ms 41820 KB n=100
24 Correct 8 ms 41820 KB n=100
25 Correct 7 ms 41820 KB n=100
26 Correct 8 ms 41820 KB n=12
27 Correct 7 ms 41944 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 41816 KB n=5
2 Correct 7 ms 41820 KB n=100
3 Correct 7 ms 41820 KB n=100
4 Correct 7 ms 41820 KB n=100
5 Correct 7 ms 41820 KB n=100
6 Correct 7 ms 41936 KB n=100
7 Correct 7 ms 41820 KB n=100
8 Correct 7 ms 42072 KB n=100
9 Correct 8 ms 41820 KB n=100
10 Correct 7 ms 41820 KB n=100
11 Correct 7 ms 41820 KB n=100
12 Correct 7 ms 41816 KB n=100
13 Correct 7 ms 41820 KB n=100
14 Correct 7 ms 41820 KB n=100
15 Correct 7 ms 41820 KB n=100
16 Correct 8 ms 41820 KB n=100
17 Correct 8 ms 41940 KB n=100
18 Correct 7 ms 41820 KB n=100
19 Correct 7 ms 41920 KB n=100
20 Correct 7 ms 41820 KB n=100
21 Correct 7 ms 41820 KB n=100
22 Correct 7 ms 41940 KB n=100
23 Correct 7 ms 41820 KB n=100
24 Correct 8 ms 41820 KB n=100
25 Correct 7 ms 41820 KB n=100
26 Correct 8 ms 41820 KB n=12
27 Correct 7 ms 41944 KB n=100
28 Correct 8 ms 41820 KB n=500
29 Correct 8 ms 41820 KB n=500
30 Correct 8 ms 42072 KB n=500
31 Correct 8 ms 41820 KB n=500
32 Correct 8 ms 41816 KB n=500
33 Correct 8 ms 41820 KB n=500
34 Correct 8 ms 41908 KB n=500
35 Correct 8 ms 41820 KB n=500
36 Correct 8 ms 41820 KB n=500
37 Correct 8 ms 41820 KB n=500
38 Correct 8 ms 41820 KB n=500
39 Correct 8 ms 41820 KB n=500
40 Correct 7 ms 41820 KB n=500
41 Correct 8 ms 41816 KB n=500
42 Correct 9 ms 41816 KB n=500
43 Correct 8 ms 41820 KB n=500
44 Correct 8 ms 41996 KB n=500
45 Correct 8 ms 41820 KB n=500
46 Correct 8 ms 41820 KB n=500
47 Correct 8 ms 41820 KB n=500
48 Correct 8 ms 41820 KB n=500
49 Correct 8 ms 41868 KB n=500
50 Correct 8 ms 41816 KB n=500
51 Correct 8 ms 41820 KB n=500
52 Correct 8 ms 41820 KB n=500
53 Correct 8 ms 41820 KB n=500
54 Correct 8 ms 41820 KB n=500
55 Correct 7 ms 41820 KB n=278
56 Correct 8 ms 41820 KB n=500
57 Correct 8 ms 41816 KB n=500
58 Correct 8 ms 41992 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 41816 KB n=5
2 Correct 7 ms 41820 KB n=100
3 Correct 7 ms 41820 KB n=100
4 Correct 7 ms 41820 KB n=100
5 Correct 7 ms 41820 KB n=100
6 Correct 7 ms 41936 KB n=100
7 Correct 7 ms 41820 KB n=100
8 Correct 7 ms 42072 KB n=100
9 Correct 8 ms 41820 KB n=100
10 Correct 7 ms 41820 KB n=100
11 Correct 7 ms 41820 KB n=100
12 Correct 7 ms 41816 KB n=100
13 Correct 7 ms 41820 KB n=100
14 Correct 7 ms 41820 KB n=100
15 Correct 7 ms 41820 KB n=100
16 Correct 8 ms 41820 KB n=100
17 Correct 8 ms 41940 KB n=100
18 Correct 7 ms 41820 KB n=100
19 Correct 7 ms 41920 KB n=100
20 Correct 7 ms 41820 KB n=100
21 Correct 7 ms 41820 KB n=100
22 Correct 7 ms 41940 KB n=100
23 Correct 7 ms 41820 KB n=100
24 Correct 8 ms 41820 KB n=100
25 Correct 7 ms 41820 KB n=100
26 Correct 8 ms 41820 KB n=12
27 Correct 7 ms 41944 KB n=100
28 Correct 8 ms 41820 KB n=500
29 Correct 8 ms 41820 KB n=500
30 Correct 8 ms 42072 KB n=500
31 Correct 8 ms 41820 KB n=500
32 Correct 8 ms 41816 KB n=500
33 Correct 8 ms 41820 KB n=500
34 Correct 8 ms 41908 KB n=500
35 Correct 8 ms 41820 KB n=500
36 Correct 8 ms 41820 KB n=500
37 Correct 8 ms 41820 KB n=500
38 Correct 8 ms 41820 KB n=500
39 Correct 8 ms 41820 KB n=500
40 Correct 7 ms 41820 KB n=500
41 Correct 8 ms 41816 KB n=500
42 Correct 9 ms 41816 KB n=500
43 Correct 8 ms 41820 KB n=500
44 Correct 8 ms 41996 KB n=500
45 Correct 8 ms 41820 KB n=500
46 Correct 8 ms 41820 KB n=500
47 Correct 8 ms 41820 KB n=500
48 Correct 8 ms 41820 KB n=500
49 Correct 8 ms 41868 KB n=500
50 Correct 8 ms 41816 KB n=500
51 Correct 8 ms 41820 KB n=500
52 Correct 8 ms 41820 KB n=500
53 Correct 8 ms 41820 KB n=500
54 Correct 8 ms 41820 KB n=500
55 Correct 7 ms 41820 KB n=278
56 Correct 8 ms 41820 KB n=500
57 Correct 8 ms 41816 KB n=500
58 Correct 8 ms 41992 KB n=500
59 Correct 9 ms 42072 KB n=2000
60 Correct 10 ms 42076 KB n=2000
61 Correct 10 ms 42076 KB n=2000
62 Correct 10 ms 42076 KB n=2000
63 Correct 10 ms 42076 KB n=2000
64 Correct 10 ms 42076 KB n=2000
65 Correct 10 ms 42076 KB n=2000
66 Correct 10 ms 42076 KB n=2000
67 Correct 11 ms 42076 KB n=2000
68 Correct 10 ms 42076 KB n=2000
69 Correct 9 ms 42144 KB n=2000
70 Correct 10 ms 42072 KB n=2000
71 Correct 9 ms 42076 KB n=2000
72 Correct 9 ms 42072 KB n=2000
73 Correct 10 ms 42184 KB n=2000
74 Correct 9 ms 42072 KB n=1844
75 Correct 10 ms 42076 KB n=2000
76 Correct 11 ms 42072 KB n=2000
77 Correct 10 ms 42076 KB n=2000
78 Correct 10 ms 42076 KB n=2000
79 Correct 10 ms 42192 KB n=2000
80 Correct 10 ms 42136 KB n=2000
81 Correct 11 ms 42076 KB n=2000
82 Correct 11 ms 42188 KB n=2000
83 Correct 10 ms 42076 KB n=2000
84 Correct 10 ms 42072 KB n=2000
85 Correct 11 ms 42076 KB n=2000
86 Correct 10 ms 42076 KB n=2000
87 Correct 10 ms 42108 KB n=2000
88 Correct 10 ms 42072 KB n=2000
89 Correct 10 ms 42076 KB n=2000
90 Correct 10 ms 42076 KB n=2000
91 Correct 10 ms 42076 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 41816 KB n=5
2 Correct 7 ms 41820 KB n=100
3 Correct 7 ms 41820 KB n=100
4 Correct 7 ms 41820 KB n=100
5 Correct 7 ms 41820 KB n=100
6 Correct 7 ms 41936 KB n=100
7 Correct 7 ms 41820 KB n=100
8 Correct 7 ms 42072 KB n=100
9 Correct 8 ms 41820 KB n=100
10 Correct 7 ms 41820 KB n=100
11 Correct 7 ms 41820 KB n=100
12 Correct 7 ms 41816 KB n=100
13 Correct 7 ms 41820 KB n=100
14 Correct 7 ms 41820 KB n=100
15 Correct 7 ms 41820 KB n=100
16 Correct 8 ms 41820 KB n=100
17 Correct 8 ms 41940 KB n=100
18 Correct 7 ms 41820 KB n=100
19 Correct 7 ms 41920 KB n=100
20 Correct 7 ms 41820 KB n=100
21 Correct 7 ms 41820 KB n=100
22 Correct 7 ms 41940 KB n=100
23 Correct 7 ms 41820 KB n=100
24 Correct 8 ms 41820 KB n=100
25 Correct 7 ms 41820 KB n=100
26 Correct 8 ms 41820 KB n=12
27 Correct 7 ms 41944 KB n=100
28 Correct 8 ms 41820 KB n=500
29 Correct 8 ms 41820 KB n=500
30 Correct 8 ms 42072 KB n=500
31 Correct 8 ms 41820 KB n=500
32 Correct 8 ms 41816 KB n=500
33 Correct 8 ms 41820 KB n=500
34 Correct 8 ms 41908 KB n=500
35 Correct 8 ms 41820 KB n=500
36 Correct 8 ms 41820 KB n=500
37 Correct 8 ms 41820 KB n=500
38 Correct 8 ms 41820 KB n=500
39 Correct 8 ms 41820 KB n=500
40 Correct 7 ms 41820 KB n=500
41 Correct 8 ms 41816 KB n=500
42 Correct 9 ms 41816 KB n=500
43 Correct 8 ms 41820 KB n=500
44 Correct 8 ms 41996 KB n=500
45 Correct 8 ms 41820 KB n=500
46 Correct 8 ms 41820 KB n=500
47 Correct 8 ms 41820 KB n=500
48 Correct 8 ms 41820 KB n=500
49 Correct 8 ms 41868 KB n=500
50 Correct 8 ms 41816 KB n=500
51 Correct 8 ms 41820 KB n=500
52 Correct 8 ms 41820 KB n=500
53 Correct 8 ms 41820 KB n=500
54 Correct 8 ms 41820 KB n=500
55 Correct 7 ms 41820 KB n=278
56 Correct 8 ms 41820 KB n=500
57 Correct 8 ms 41816 KB n=500
58 Correct 8 ms 41992 KB n=500
59 Correct 9 ms 42072 KB n=2000
60 Correct 10 ms 42076 KB n=2000
61 Correct 10 ms 42076 KB n=2000
62 Correct 10 ms 42076 KB n=2000
63 Correct 10 ms 42076 KB n=2000
64 Correct 10 ms 42076 KB n=2000
65 Correct 10 ms 42076 KB n=2000
66 Correct 10 ms 42076 KB n=2000
67 Correct 11 ms 42076 KB n=2000
68 Correct 10 ms 42076 KB n=2000
69 Correct 9 ms 42144 KB n=2000
70 Correct 10 ms 42072 KB n=2000
71 Correct 9 ms 42076 KB n=2000
72 Correct 9 ms 42072 KB n=2000
73 Correct 10 ms 42184 KB n=2000
74 Correct 9 ms 42072 KB n=1844
75 Correct 10 ms 42076 KB n=2000
76 Correct 11 ms 42072 KB n=2000
77 Correct 10 ms 42076 KB n=2000
78 Correct 10 ms 42076 KB n=2000
79 Correct 10 ms 42192 KB n=2000
80 Correct 10 ms 42136 KB n=2000
81 Correct 11 ms 42076 KB n=2000
82 Correct 11 ms 42188 KB n=2000
83 Correct 10 ms 42076 KB n=2000
84 Correct 10 ms 42072 KB n=2000
85 Correct 11 ms 42076 KB n=2000
86 Correct 10 ms 42076 KB n=2000
87 Correct 10 ms 42108 KB n=2000
88 Correct 10 ms 42072 KB n=2000
89 Correct 10 ms 42076 KB n=2000
90 Correct 10 ms 42076 KB n=2000
91 Correct 10 ms 42076 KB n=2000
92 Correct 569 ms 68812 KB n=200000
93 Correct 723 ms 75968 KB n=200000
94 Correct 654 ms 83536 KB n=200000
95 Correct 560 ms 75136 KB n=200000
96 Correct 530 ms 75264 KB n=200000
97 Correct 749 ms 79564 KB n=200000
98 Correct 561 ms 75460 KB n=200000
99 Correct 699 ms 75496 KB n=200000
100 Correct 563 ms 75544 KB n=200000
101 Correct 632 ms 84932 KB n=200000
102 Correct 311 ms 76372 KB n=200000
103 Correct 314 ms 76448 KB n=200000
104 Correct 326 ms 76336 KB n=200000
105 Correct 314 ms 76988 KB n=200000
106 Correct 302 ms 76876 KB n=200000
107 Correct 305 ms 76772 KB n=200000
108 Correct 647 ms 75340 KB n=200000
109 Correct 591 ms 75340 KB n=200000
110 Correct 600 ms 75204 KB n=200000
111 Correct 548 ms 74696 KB n=200000
112 Correct 630 ms 83904 KB n=200000
113 Correct 710 ms 79344 KB n=200000
114 Correct 554 ms 74772 KB n=200000
115 Correct 814 ms 77408 KB n=200000
116 Correct 536 ms 75204 KB n=200000
117 Correct 635 ms 84188 KB n=200000
118 Correct 720 ms 78116 KB n=200000
119 Correct 562 ms 75276 KB n=200000
120 Correct 549 ms 83808 KB n=200000
121 Correct 553 ms 83856 KB n=200000
122 Correct 562 ms 84100 KB n=200000
123 Correct 322 ms 76624 KB n=200000
124 Correct 150 ms 54404 KB n=25264