#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
#define f first
#define s second
vector<int> parents(1000006, -1);
vector<int> sizes(1000006);
class DisjointSets {
public:
DisjointSets(int size) {
for (int i = 0; i < size; i++) { parents[i] = i; }
}
/** @return the "representative" node in x's component */
int find(int x) {
return parents[x] == x ? x : (parents[x] = find(parents[x]));
}
/** @return whether the merge changed connectivity */
int unite(int x, int y) {
int x_root = find(x);
int y_root = find(y);
if (x_root == y_root) { return 0; }
if (sizes[x_root] < sizes[y_root]) { swap(x_root, y_root); }
int A = sizes[x_root], B = sizes[y_root];
/*int ans = ((A + B) * (A + B - 1)) / 2;
ans -= (A * (A - 1)) / 2;
ans -= (B * (B - 1)) / 2;*/
sizes[x_root] += sizes[y_root];
parents[y_root] = x_root;
return A * B;
}
/** @return whether x and y are in the same connected component */
bool connected(int x, int y) { return find(x) == find(y); }
};
signed main() {
int N, Q; cin >> N >> Q;
vector<pii> H;
vector<int> M(N + 6);
for(int i = 1; i <= N; i++){
cin >> M[i];
H.push_back({M[i], -i});
}
M[N + 1] = 1000000006;
M[0] = 1000000006;
for(int i = 1; i <= Q; i++){
int x; cin >> x;
H.push_back({x, i});
}
sort(H.begin(), H.end());
DisjointSets DSU(N + 6);
vector<int> ans(Q + 1);
int count = 0;
for(int i = 0; i < N + Q; i++){
int Alt = H[i].f, ind = H[i].s;
if(ind > 0){
ans[ind] = count;
continue;
}
count++;
ind *= -1;
parents[ind] = ind;
sizes[ind] = 1;
if(Alt >= M[ind + 1]){
count += DSU.unite(ind, ind + 1);
}
if(Alt >= M[ind - 1]){
count += DSU.unite(ind, ind - 1);
}
}
for(int i = 1; i <= Q; i++){
cout << ans[i] << " ";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15964 KB |
Output is correct |
2 |
Correct |
6 ms |
15964 KB |
Output is correct |
3 |
Correct |
7 ms |
16100 KB |
Output is correct |
4 |
Correct |
7 ms |
15964 KB |
Output is correct |
5 |
Correct |
7 ms |
15964 KB |
Output is correct |
6 |
Correct |
7 ms |
15924 KB |
Output is correct |
7 |
Correct |
7 ms |
15960 KB |
Output is correct |
8 |
Correct |
7 ms |
15964 KB |
Output is correct |
9 |
Correct |
7 ms |
15960 KB |
Output is correct |
10 |
Correct |
7 ms |
15964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15964 KB |
Output is correct |
2 |
Correct |
6 ms |
15964 KB |
Output is correct |
3 |
Correct |
7 ms |
16100 KB |
Output is correct |
4 |
Correct |
7 ms |
15964 KB |
Output is correct |
5 |
Correct |
7 ms |
15964 KB |
Output is correct |
6 |
Correct |
7 ms |
15924 KB |
Output is correct |
7 |
Correct |
7 ms |
15960 KB |
Output is correct |
8 |
Correct |
7 ms |
15964 KB |
Output is correct |
9 |
Correct |
7 ms |
15960 KB |
Output is correct |
10 |
Correct |
7 ms |
15964 KB |
Output is correct |
11 |
Correct |
6 ms |
15960 KB |
Output is correct |
12 |
Correct |
7 ms |
15964 KB |
Output is correct |
13 |
Correct |
7 ms |
15964 KB |
Output is correct |
14 |
Correct |
7 ms |
15964 KB |
Output is correct |
15 |
Correct |
8 ms |
15960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15964 KB |
Output is correct |
2 |
Correct |
6 ms |
15964 KB |
Output is correct |
3 |
Correct |
7 ms |
16100 KB |
Output is correct |
4 |
Correct |
7 ms |
15964 KB |
Output is correct |
5 |
Correct |
7 ms |
15964 KB |
Output is correct |
6 |
Correct |
7 ms |
15924 KB |
Output is correct |
7 |
Correct |
7 ms |
15960 KB |
Output is correct |
8 |
Correct |
7 ms |
15964 KB |
Output is correct |
9 |
Correct |
7 ms |
15960 KB |
Output is correct |
10 |
Correct |
7 ms |
15964 KB |
Output is correct |
11 |
Correct |
6 ms |
15960 KB |
Output is correct |
12 |
Correct |
7 ms |
15964 KB |
Output is correct |
13 |
Correct |
7 ms |
15964 KB |
Output is correct |
14 |
Correct |
7 ms |
15964 KB |
Output is correct |
15 |
Correct |
8 ms |
15960 KB |
Output is correct |
16 |
Correct |
9 ms |
16084 KB |
Output is correct |
17 |
Correct |
6 ms |
16108 KB |
Output is correct |
18 |
Correct |
7 ms |
15964 KB |
Output is correct |
19 |
Correct |
6 ms |
15964 KB |
Output is correct |
20 |
Correct |
7 ms |
15964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15964 KB |
Output is correct |
2 |
Correct |
6 ms |
15964 KB |
Output is correct |
3 |
Correct |
7 ms |
16100 KB |
Output is correct |
4 |
Correct |
7 ms |
15964 KB |
Output is correct |
5 |
Correct |
7 ms |
15964 KB |
Output is correct |
6 |
Correct |
7 ms |
15924 KB |
Output is correct |
7 |
Correct |
7 ms |
15960 KB |
Output is correct |
8 |
Correct |
7 ms |
15964 KB |
Output is correct |
9 |
Correct |
7 ms |
15960 KB |
Output is correct |
10 |
Correct |
7 ms |
15964 KB |
Output is correct |
11 |
Correct |
6 ms |
15960 KB |
Output is correct |
12 |
Correct |
7 ms |
15964 KB |
Output is correct |
13 |
Correct |
7 ms |
15964 KB |
Output is correct |
14 |
Correct |
7 ms |
15964 KB |
Output is correct |
15 |
Correct |
8 ms |
15960 KB |
Output is correct |
16 |
Correct |
9 ms |
16084 KB |
Output is correct |
17 |
Correct |
6 ms |
16108 KB |
Output is correct |
18 |
Correct |
7 ms |
15964 KB |
Output is correct |
19 |
Correct |
6 ms |
15964 KB |
Output is correct |
20 |
Correct |
7 ms |
15964 KB |
Output is correct |
21 |
Correct |
7 ms |
15964 KB |
Output is correct |
22 |
Correct |
8 ms |
15992 KB |
Output is correct |
23 |
Correct |
7 ms |
15964 KB |
Output is correct |
24 |
Correct |
8 ms |
15944 KB |
Output is correct |
25 |
Correct |
7 ms |
16120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
19408 KB |
Output is correct |
2 |
Correct |
34 ms |
19664 KB |
Output is correct |
3 |
Correct |
33 ms |
19408 KB |
Output is correct |
4 |
Correct |
33 ms |
19360 KB |
Output is correct |
5 |
Correct |
31 ms |
19396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
22464 KB |
Output is correct |
2 |
Correct |
58 ms |
22524 KB |
Output is correct |
3 |
Correct |
59 ms |
22464 KB |
Output is correct |
4 |
Correct |
62 ms |
22720 KB |
Output is correct |
5 |
Correct |
60 ms |
22400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
22460 KB |
Output is correct |
2 |
Correct |
65 ms |
22724 KB |
Output is correct |
3 |
Correct |
65 ms |
22712 KB |
Output is correct |
4 |
Correct |
60 ms |
22616 KB |
Output is correct |
5 |
Correct |
65 ms |
22728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15964 KB |
Output is correct |
2 |
Correct |
6 ms |
15964 KB |
Output is correct |
3 |
Correct |
7 ms |
16100 KB |
Output is correct |
4 |
Correct |
7 ms |
15964 KB |
Output is correct |
5 |
Correct |
7 ms |
15964 KB |
Output is correct |
6 |
Correct |
7 ms |
15924 KB |
Output is correct |
7 |
Correct |
7 ms |
15960 KB |
Output is correct |
8 |
Correct |
7 ms |
15964 KB |
Output is correct |
9 |
Correct |
7 ms |
15960 KB |
Output is correct |
10 |
Correct |
7 ms |
15964 KB |
Output is correct |
11 |
Correct |
36 ms |
19408 KB |
Output is correct |
12 |
Correct |
34 ms |
19664 KB |
Output is correct |
13 |
Correct |
33 ms |
19408 KB |
Output is correct |
14 |
Correct |
33 ms |
19360 KB |
Output is correct |
15 |
Correct |
31 ms |
19396 KB |
Output is correct |
16 |
Correct |
35 ms |
19308 KB |
Output is correct |
17 |
Correct |
36 ms |
19664 KB |
Output is correct |
18 |
Correct |
34 ms |
19664 KB |
Output is correct |
19 |
Correct |
32 ms |
19436 KB |
Output is correct |
20 |
Correct |
32 ms |
19660 KB |
Output is correct |
21 |
Correct |
32 ms |
19408 KB |
Output is correct |
22 |
Correct |
33 ms |
19560 KB |
Output is correct |
23 |
Correct |
33 ms |
19408 KB |
Output is correct |
24 |
Correct |
33 ms |
19408 KB |
Output is correct |
25 |
Correct |
32 ms |
19396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15964 KB |
Output is correct |
2 |
Correct |
6 ms |
15964 KB |
Output is correct |
3 |
Correct |
7 ms |
16100 KB |
Output is correct |
4 |
Correct |
7 ms |
15964 KB |
Output is correct |
5 |
Correct |
7 ms |
15964 KB |
Output is correct |
6 |
Correct |
7 ms |
15924 KB |
Output is correct |
7 |
Correct |
7 ms |
15960 KB |
Output is correct |
8 |
Correct |
7 ms |
15964 KB |
Output is correct |
9 |
Correct |
7 ms |
15960 KB |
Output is correct |
10 |
Correct |
7 ms |
15964 KB |
Output is correct |
11 |
Correct |
6 ms |
15960 KB |
Output is correct |
12 |
Correct |
7 ms |
15964 KB |
Output is correct |
13 |
Correct |
7 ms |
15964 KB |
Output is correct |
14 |
Correct |
7 ms |
15964 KB |
Output is correct |
15 |
Correct |
8 ms |
15960 KB |
Output is correct |
16 |
Correct |
9 ms |
16084 KB |
Output is correct |
17 |
Correct |
6 ms |
16108 KB |
Output is correct |
18 |
Correct |
7 ms |
15964 KB |
Output is correct |
19 |
Correct |
6 ms |
15964 KB |
Output is correct |
20 |
Correct |
7 ms |
15964 KB |
Output is correct |
21 |
Correct |
7 ms |
15964 KB |
Output is correct |
22 |
Correct |
8 ms |
15992 KB |
Output is correct |
23 |
Correct |
7 ms |
15964 KB |
Output is correct |
24 |
Correct |
8 ms |
15944 KB |
Output is correct |
25 |
Correct |
7 ms |
16120 KB |
Output is correct |
26 |
Correct |
36 ms |
19408 KB |
Output is correct |
27 |
Correct |
34 ms |
19664 KB |
Output is correct |
28 |
Correct |
33 ms |
19408 KB |
Output is correct |
29 |
Correct |
33 ms |
19360 KB |
Output is correct |
30 |
Correct |
31 ms |
19396 KB |
Output is correct |
31 |
Correct |
58 ms |
22464 KB |
Output is correct |
32 |
Correct |
58 ms |
22524 KB |
Output is correct |
33 |
Correct |
59 ms |
22464 KB |
Output is correct |
34 |
Correct |
62 ms |
22720 KB |
Output is correct |
35 |
Correct |
60 ms |
22400 KB |
Output is correct |
36 |
Correct |
61 ms |
22460 KB |
Output is correct |
37 |
Correct |
65 ms |
22724 KB |
Output is correct |
38 |
Correct |
65 ms |
22712 KB |
Output is correct |
39 |
Correct |
60 ms |
22616 KB |
Output is correct |
40 |
Correct |
65 ms |
22728 KB |
Output is correct |
41 |
Correct |
35 ms |
19308 KB |
Output is correct |
42 |
Correct |
36 ms |
19664 KB |
Output is correct |
43 |
Correct |
34 ms |
19664 KB |
Output is correct |
44 |
Correct |
32 ms |
19436 KB |
Output is correct |
45 |
Correct |
32 ms |
19660 KB |
Output is correct |
46 |
Correct |
32 ms |
19408 KB |
Output is correct |
47 |
Correct |
33 ms |
19560 KB |
Output is correct |
48 |
Correct |
33 ms |
19408 KB |
Output is correct |
49 |
Correct |
33 ms |
19408 KB |
Output is correct |
50 |
Correct |
32 ms |
19396 KB |
Output is correct |
51 |
Correct |
68 ms |
22428 KB |
Output is correct |
52 |
Correct |
64 ms |
22276 KB |
Output is correct |
53 |
Correct |
65 ms |
22456 KB |
Output is correct |
54 |
Correct |
68 ms |
22424 KB |
Output is correct |
55 |
Correct |
71 ms |
22348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15964 KB |
Output is correct |
2 |
Correct |
6 ms |
15964 KB |
Output is correct |
3 |
Correct |
7 ms |
16100 KB |
Output is correct |
4 |
Correct |
7 ms |
15964 KB |
Output is correct |
5 |
Correct |
7 ms |
15964 KB |
Output is correct |
6 |
Correct |
7 ms |
15924 KB |
Output is correct |
7 |
Correct |
7 ms |
15960 KB |
Output is correct |
8 |
Correct |
7 ms |
15964 KB |
Output is correct |
9 |
Correct |
7 ms |
15960 KB |
Output is correct |
10 |
Correct |
7 ms |
15964 KB |
Output is correct |
11 |
Correct |
6 ms |
15960 KB |
Output is correct |
12 |
Correct |
7 ms |
15964 KB |
Output is correct |
13 |
Correct |
7 ms |
15964 KB |
Output is correct |
14 |
Correct |
7 ms |
15964 KB |
Output is correct |
15 |
Correct |
8 ms |
15960 KB |
Output is correct |
16 |
Correct |
9 ms |
16084 KB |
Output is correct |
17 |
Correct |
6 ms |
16108 KB |
Output is correct |
18 |
Correct |
7 ms |
15964 KB |
Output is correct |
19 |
Correct |
6 ms |
15964 KB |
Output is correct |
20 |
Correct |
7 ms |
15964 KB |
Output is correct |
21 |
Correct |
7 ms |
15964 KB |
Output is correct |
22 |
Correct |
8 ms |
15992 KB |
Output is correct |
23 |
Correct |
7 ms |
15964 KB |
Output is correct |
24 |
Correct |
8 ms |
15944 KB |
Output is correct |
25 |
Correct |
7 ms |
16120 KB |
Output is correct |
26 |
Correct |
36 ms |
19408 KB |
Output is correct |
27 |
Correct |
34 ms |
19664 KB |
Output is correct |
28 |
Correct |
33 ms |
19408 KB |
Output is correct |
29 |
Correct |
33 ms |
19360 KB |
Output is correct |
30 |
Correct |
31 ms |
19396 KB |
Output is correct |
31 |
Correct |
58 ms |
22464 KB |
Output is correct |
32 |
Correct |
58 ms |
22524 KB |
Output is correct |
33 |
Correct |
59 ms |
22464 KB |
Output is correct |
34 |
Correct |
62 ms |
22720 KB |
Output is correct |
35 |
Correct |
60 ms |
22400 KB |
Output is correct |
36 |
Correct |
61 ms |
22460 KB |
Output is correct |
37 |
Correct |
65 ms |
22724 KB |
Output is correct |
38 |
Correct |
65 ms |
22712 KB |
Output is correct |
39 |
Correct |
60 ms |
22616 KB |
Output is correct |
40 |
Correct |
65 ms |
22728 KB |
Output is correct |
41 |
Correct |
35 ms |
19308 KB |
Output is correct |
42 |
Correct |
36 ms |
19664 KB |
Output is correct |
43 |
Correct |
34 ms |
19664 KB |
Output is correct |
44 |
Correct |
32 ms |
19436 KB |
Output is correct |
45 |
Correct |
32 ms |
19660 KB |
Output is correct |
46 |
Correct |
32 ms |
19408 KB |
Output is correct |
47 |
Correct |
33 ms |
19560 KB |
Output is correct |
48 |
Correct |
33 ms |
19408 KB |
Output is correct |
49 |
Correct |
33 ms |
19408 KB |
Output is correct |
50 |
Correct |
32 ms |
19396 KB |
Output is correct |
51 |
Correct |
68 ms |
22428 KB |
Output is correct |
52 |
Correct |
64 ms |
22276 KB |
Output is correct |
53 |
Correct |
65 ms |
22456 KB |
Output is correct |
54 |
Correct |
68 ms |
22424 KB |
Output is correct |
55 |
Correct |
71 ms |
22348 KB |
Output is correct |
56 |
Correct |
677 ms |
81980 KB |
Output is correct |
57 |
Correct |
692 ms |
81164 KB |
Output is correct |
58 |
Correct |
669 ms |
77684 KB |
Output is correct |
59 |
Correct |
648 ms |
79220 KB |
Output is correct |
60 |
Correct |
705 ms |
83320 KB |
Output is correct |