#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll ans = 0;
ll f(int x) {
return (1LL * x * (x + 1)) / 2;
}
struct DSU {
vector<int> pt, rk, val;
DSU(int n) : pt(n), rk(n, 1), val(n) {
for (int i = 0; i < n; ++i)
pt[i] = i;
}
int find_set(int i) {
return i == pt[i] ? i : pt[i] = find_set(pt[i]);
}
void unite(int i, int j) {
i = find_set(i);
j = find_set(j);
if (i != j) {
ans -= f(val[i]) + f(val[j]);
if (rk[i] < rk[j])
swap(i, j);
pt[j] = i;
rk[i] += rk[j];
val[i] += val[j];
ans += f(val[i]);
}
}
};
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n, q;
cin >> n >> q;
int h[n];
ll query_ans[q];
vector<array<int, 2>> v(n), x(q);
vector<bool> dn(n);
DSU dsu(n);
for (int i = 0; i < n; ++i) {
cin >> h[i];
v[i] = {h[i], i};
}
for (int i = 0; i < q; ++i) {
cin >> x[i][0];
x[i][1] = i;
}
sort(v.rbegin(), v.rend());
sort(x.begin(), x.end());
for (auto i : x) {
while (!v.empty() && v.back()[0] <= i[0]) {
++ans;
dsu.val[v.back()[1]] = 1;
dn[v.back()[1]] = true;
if (v.back()[1] - 1 >= 0 && dn[v.back()[1] - 1] && h[v.back()[1] - 1] <= i[0])
dsu.unite(v.back()[1], v.back()[1] - 1);
if (v.back()[1] + 1 < n && dn[v.back()[1] + 1] && h[v.back()[1] + 1] <= i[0])
dsu.unite(v.back()[1], v.back()[1] + 1);
v.pop_back();
}
query_ans[i[1]] = ans;
}
for (int i = 0; i < q; ++i)
cout << query_ans[i] << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
2616 KB |
Output is correct |
2 |
Correct |
23 ms |
3304 KB |
Output is correct |
3 |
Correct |
21 ms |
3028 KB |
Output is correct |
4 |
Correct |
20 ms |
3028 KB |
Output is correct |
5 |
Correct |
21 ms |
3020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
4860 KB |
Output is correct |
2 |
Correct |
37 ms |
6084 KB |
Output is correct |
3 |
Correct |
38 ms |
5976 KB |
Output is correct |
4 |
Correct |
38 ms |
6144 KB |
Output is correct |
5 |
Correct |
36 ms |
5952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
4904 KB |
Output is correct |
2 |
Correct |
39 ms |
6092 KB |
Output is correct |
3 |
Correct |
38 ms |
6156 KB |
Output is correct |
4 |
Correct |
37 ms |
6280 KB |
Output is correct |
5 |
Correct |
39 ms |
6424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
25 ms |
2616 KB |
Output is correct |
12 |
Correct |
23 ms |
3304 KB |
Output is correct |
13 |
Correct |
21 ms |
3028 KB |
Output is correct |
14 |
Correct |
20 ms |
3028 KB |
Output is correct |
15 |
Correct |
21 ms |
3020 KB |
Output is correct |
16 |
Correct |
19 ms |
3028 KB |
Output is correct |
17 |
Correct |
19 ms |
3284 KB |
Output is correct |
18 |
Correct |
18 ms |
3296 KB |
Output is correct |
19 |
Correct |
16 ms |
2900 KB |
Output is correct |
20 |
Correct |
19 ms |
3144 KB |
Output is correct |
21 |
Correct |
20 ms |
2992 KB |
Output is correct |
22 |
Correct |
18 ms |
3148 KB |
Output is correct |
23 |
Correct |
19 ms |
3156 KB |
Output is correct |
24 |
Correct |
16 ms |
2992 KB |
Output is correct |
25 |
Correct |
19 ms |
3144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
25 ms |
2616 KB |
Output is correct |
27 |
Correct |
23 ms |
3304 KB |
Output is correct |
28 |
Correct |
21 ms |
3028 KB |
Output is correct |
29 |
Correct |
20 ms |
3028 KB |
Output is correct |
30 |
Correct |
21 ms |
3020 KB |
Output is correct |
31 |
Correct |
35 ms |
4860 KB |
Output is correct |
32 |
Correct |
37 ms |
6084 KB |
Output is correct |
33 |
Correct |
38 ms |
5976 KB |
Output is correct |
34 |
Correct |
38 ms |
6144 KB |
Output is correct |
35 |
Correct |
36 ms |
5952 KB |
Output is correct |
36 |
Correct |
40 ms |
4904 KB |
Output is correct |
37 |
Correct |
39 ms |
6092 KB |
Output is correct |
38 |
Correct |
38 ms |
6156 KB |
Output is correct |
39 |
Correct |
37 ms |
6280 KB |
Output is correct |
40 |
Correct |
39 ms |
6424 KB |
Output is correct |
41 |
Correct |
19 ms |
3028 KB |
Output is correct |
42 |
Correct |
19 ms |
3284 KB |
Output is correct |
43 |
Correct |
18 ms |
3296 KB |
Output is correct |
44 |
Correct |
16 ms |
2900 KB |
Output is correct |
45 |
Correct |
19 ms |
3144 KB |
Output is correct |
46 |
Correct |
20 ms |
2992 KB |
Output is correct |
47 |
Correct |
18 ms |
3148 KB |
Output is correct |
48 |
Correct |
19 ms |
3156 KB |
Output is correct |
49 |
Correct |
16 ms |
2992 KB |
Output is correct |
50 |
Correct |
19 ms |
3144 KB |
Output is correct |
51 |
Correct |
48 ms |
6028 KB |
Output is correct |
52 |
Correct |
49 ms |
5976 KB |
Output is correct |
53 |
Correct |
46 ms |
5964 KB |
Output is correct |
54 |
Correct |
45 ms |
5844 KB |
Output is correct |
55 |
Correct |
50 ms |
6064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
25 ms |
2616 KB |
Output is correct |
27 |
Correct |
23 ms |
3304 KB |
Output is correct |
28 |
Correct |
21 ms |
3028 KB |
Output is correct |
29 |
Correct |
20 ms |
3028 KB |
Output is correct |
30 |
Correct |
21 ms |
3020 KB |
Output is correct |
31 |
Correct |
35 ms |
4860 KB |
Output is correct |
32 |
Correct |
37 ms |
6084 KB |
Output is correct |
33 |
Correct |
38 ms |
5976 KB |
Output is correct |
34 |
Correct |
38 ms |
6144 KB |
Output is correct |
35 |
Correct |
36 ms |
5952 KB |
Output is correct |
36 |
Correct |
40 ms |
4904 KB |
Output is correct |
37 |
Correct |
39 ms |
6092 KB |
Output is correct |
38 |
Correct |
38 ms |
6156 KB |
Output is correct |
39 |
Correct |
37 ms |
6280 KB |
Output is correct |
40 |
Correct |
39 ms |
6424 KB |
Output is correct |
41 |
Correct |
19 ms |
3028 KB |
Output is correct |
42 |
Correct |
19 ms |
3284 KB |
Output is correct |
43 |
Correct |
18 ms |
3296 KB |
Output is correct |
44 |
Correct |
16 ms |
2900 KB |
Output is correct |
45 |
Correct |
19 ms |
3144 KB |
Output is correct |
46 |
Correct |
20 ms |
2992 KB |
Output is correct |
47 |
Correct |
18 ms |
3148 KB |
Output is correct |
48 |
Correct |
19 ms |
3156 KB |
Output is correct |
49 |
Correct |
16 ms |
2992 KB |
Output is correct |
50 |
Correct |
19 ms |
3144 KB |
Output is correct |
51 |
Correct |
48 ms |
6028 KB |
Output is correct |
52 |
Correct |
49 ms |
5976 KB |
Output is correct |
53 |
Correct |
46 ms |
5964 KB |
Output is correct |
54 |
Correct |
45 ms |
5844 KB |
Output is correct |
55 |
Correct |
50 ms |
6064 KB |
Output is correct |
56 |
Correct |
588 ms |
58548 KB |
Output is correct |
57 |
Correct |
569 ms |
58332 KB |
Output is correct |
58 |
Correct |
540 ms |
55064 KB |
Output is correct |
59 |
Correct |
559 ms |
56352 KB |
Output is correct |
60 |
Correct |
597 ms |
59988 KB |
Output is correct |