#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1000 * 1000 + 3;
int par[N], sz[N];
int ans = 0;
bool u[N];
int cnt(int v) { return (v * (v + 1)) / 2; }
void init(int n) {
for(int i = 0; i <= n + 1; i++) par[i] = i, sz[i] = 1;
}
int fin(int v) { return par[v] == v ? v : par[v] = fin(par[v]); }
void join(int a, int b) {
if(!u[a] || !u[b]) return;
a = fin(a); b = fin(b);
if(a == b) return;
ans -= cnt(sz[a]);
ans -= cnt(sz[b]);
if(sz[a] < sz[b]) swap(a, b);
sz[a] += sz[b];
par[b] = a;
ans += cnt(sz[a]);
}
void unite(int v) {
u[v] = 1;
ans += cnt(sz[v]);
join(v, v + 1);
join(v, v - 1);
}
signed main() {
std::ios::sync_with_stdio(0);
std::cout.tie(0);
std::cin.tie(0);
int n, q;
cin >> n >> q;
init(n);
vector<array<int, 2>> a(n), h(q);
for(int i = 0; i < n; i++) cin >> a[i][0], a[i][1] = i + 1;
for(int i = 0; i < q; i++) cin >> h[i][0], h[i][1] = i;
sort(h.begin(), h.end());
sort(a.begin(), a.end());
vector<int> id(q);
int j = 0;
for(int i = 0; i < q; i++) {
while(j < n && a[j][0] <= h[i][0]) {
unite(a[j][1]);
j++;
}
id[h[i][1]] = ans;
}
for(int i = 0; i < q; i++) cout << id[i] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
492 KB |
Output is correct |
22 |
Correct |
1 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
488 KB |
Output is correct |
24 |
Correct |
1 ms |
492 KB |
Output is correct |
25 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
4024 KB |
Output is correct |
2 |
Correct |
26 ms |
4204 KB |
Output is correct |
3 |
Correct |
25 ms |
3940 KB |
Output is correct |
4 |
Correct |
25 ms |
3948 KB |
Output is correct |
5 |
Correct |
25 ms |
3948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
7804 KB |
Output is correct |
2 |
Correct |
57 ms |
7780 KB |
Output is correct |
3 |
Correct |
46 ms |
7652 KB |
Output is correct |
4 |
Correct |
50 ms |
7908 KB |
Output is correct |
5 |
Correct |
47 ms |
7652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
7780 KB |
Output is correct |
2 |
Correct |
56 ms |
7912 KB |
Output is correct |
3 |
Correct |
48 ms |
7908 KB |
Output is correct |
4 |
Correct |
46 ms |
7908 KB |
Output is correct |
5 |
Correct |
49 ms |
8292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
31 ms |
4024 KB |
Output is correct |
12 |
Correct |
26 ms |
4204 KB |
Output is correct |
13 |
Correct |
25 ms |
3940 KB |
Output is correct |
14 |
Correct |
25 ms |
3948 KB |
Output is correct |
15 |
Correct |
25 ms |
3948 KB |
Output is correct |
16 |
Correct |
23 ms |
3940 KB |
Output is correct |
17 |
Correct |
24 ms |
4204 KB |
Output is correct |
18 |
Correct |
22 ms |
4204 KB |
Output is correct |
19 |
Correct |
21 ms |
3820 KB |
Output is correct |
20 |
Correct |
23 ms |
4076 KB |
Output is correct |
21 |
Correct |
25 ms |
3820 KB |
Output is correct |
22 |
Correct |
22 ms |
3948 KB |
Output is correct |
23 |
Correct |
25 ms |
4196 KB |
Output is correct |
24 |
Correct |
21 ms |
3948 KB |
Output is correct |
25 |
Correct |
23 ms |
4068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
492 KB |
Output is correct |
22 |
Correct |
1 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
488 KB |
Output is correct |
24 |
Correct |
1 ms |
492 KB |
Output is correct |
25 |
Correct |
1 ms |
492 KB |
Output is correct |
26 |
Correct |
31 ms |
4024 KB |
Output is correct |
27 |
Correct |
26 ms |
4204 KB |
Output is correct |
28 |
Correct |
25 ms |
3940 KB |
Output is correct |
29 |
Correct |
25 ms |
3948 KB |
Output is correct |
30 |
Correct |
25 ms |
3948 KB |
Output is correct |
31 |
Correct |
47 ms |
7804 KB |
Output is correct |
32 |
Correct |
57 ms |
7780 KB |
Output is correct |
33 |
Correct |
46 ms |
7652 KB |
Output is correct |
34 |
Correct |
50 ms |
7908 KB |
Output is correct |
35 |
Correct |
47 ms |
7652 KB |
Output is correct |
36 |
Correct |
50 ms |
7780 KB |
Output is correct |
37 |
Correct |
56 ms |
7912 KB |
Output is correct |
38 |
Correct |
48 ms |
7908 KB |
Output is correct |
39 |
Correct |
46 ms |
7908 KB |
Output is correct |
40 |
Correct |
49 ms |
8292 KB |
Output is correct |
41 |
Correct |
23 ms |
3940 KB |
Output is correct |
42 |
Correct |
24 ms |
4204 KB |
Output is correct |
43 |
Correct |
22 ms |
4204 KB |
Output is correct |
44 |
Correct |
21 ms |
3820 KB |
Output is correct |
45 |
Correct |
23 ms |
4076 KB |
Output is correct |
46 |
Correct |
25 ms |
3820 KB |
Output is correct |
47 |
Correct |
22 ms |
3948 KB |
Output is correct |
48 |
Correct |
25 ms |
4196 KB |
Output is correct |
49 |
Correct |
21 ms |
3948 KB |
Output is correct |
50 |
Correct |
23 ms |
4068 KB |
Output is correct |
51 |
Correct |
61 ms |
7776 KB |
Output is correct |
52 |
Correct |
57 ms |
7728 KB |
Output is correct |
53 |
Correct |
58 ms |
7780 KB |
Output is correct |
54 |
Correct |
56 ms |
7524 KB |
Output is correct |
55 |
Correct |
57 ms |
7780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
492 KB |
Output is correct |
22 |
Correct |
1 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
488 KB |
Output is correct |
24 |
Correct |
1 ms |
492 KB |
Output is correct |
25 |
Correct |
1 ms |
492 KB |
Output is correct |
26 |
Correct |
31 ms |
4024 KB |
Output is correct |
27 |
Correct |
26 ms |
4204 KB |
Output is correct |
28 |
Correct |
25 ms |
3940 KB |
Output is correct |
29 |
Correct |
25 ms |
3948 KB |
Output is correct |
30 |
Correct |
25 ms |
3948 KB |
Output is correct |
31 |
Correct |
47 ms |
7804 KB |
Output is correct |
32 |
Correct |
57 ms |
7780 KB |
Output is correct |
33 |
Correct |
46 ms |
7652 KB |
Output is correct |
34 |
Correct |
50 ms |
7908 KB |
Output is correct |
35 |
Correct |
47 ms |
7652 KB |
Output is correct |
36 |
Correct |
50 ms |
7780 KB |
Output is correct |
37 |
Correct |
56 ms |
7912 KB |
Output is correct |
38 |
Correct |
48 ms |
7908 KB |
Output is correct |
39 |
Correct |
46 ms |
7908 KB |
Output is correct |
40 |
Correct |
49 ms |
8292 KB |
Output is correct |
41 |
Correct |
23 ms |
3940 KB |
Output is correct |
42 |
Correct |
24 ms |
4204 KB |
Output is correct |
43 |
Correct |
22 ms |
4204 KB |
Output is correct |
44 |
Correct |
21 ms |
3820 KB |
Output is correct |
45 |
Correct |
23 ms |
4076 KB |
Output is correct |
46 |
Correct |
25 ms |
3820 KB |
Output is correct |
47 |
Correct |
22 ms |
3948 KB |
Output is correct |
48 |
Correct |
25 ms |
4196 KB |
Output is correct |
49 |
Correct |
21 ms |
3948 KB |
Output is correct |
50 |
Correct |
23 ms |
4068 KB |
Output is correct |
51 |
Correct |
61 ms |
7776 KB |
Output is correct |
52 |
Correct |
57 ms |
7728 KB |
Output is correct |
53 |
Correct |
58 ms |
7780 KB |
Output is correct |
54 |
Correct |
56 ms |
7524 KB |
Output is correct |
55 |
Correct |
57 ms |
7780 KB |
Output is correct |
56 |
Correct |
693 ms |
74536 KB |
Output is correct |
57 |
Correct |
699 ms |
74288 KB |
Output is correct |
58 |
Correct |
665 ms |
70244 KB |
Output is correct |
59 |
Correct |
675 ms |
71796 KB |
Output is correct |
60 |
Correct |
733 ms |
76516 KB |
Output is correct |