#pragma GCC optimize("O3")
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define fi first
#define se second
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N, Q;
cin >> N >> Q;
vector<pair<int, int>> H(N);
for (int i = 0; i < N; i++) {
cin >> H[i].fi;
H[i].se = i;
}
sort(H.begin(), H.end());
vector<bool> vis(N);
vector<int> par(N), left(N), right(N);
iota(par.begin(), par.end(), 0);
iota(left.begin(), left.end(), 0);
iota(right.begin(), right.end(), 1);
function<ll(int)> way = [&](int v) {
return 1ll * (right[v] - left[v]) * (right[v] - left[v] + 1) / 2;
};
function<int(int)> find = [&](int v) {
return v == par[v] ? v : (par[v] = find(par[v]));
};
function<ll(int, int)> merge = [&](int a, int b) {
a = find(a), b = find(b);
ll r = way(a) + way(b);
par[a] = b;
left[b] = left[a];
return way(b) - r;
};
ll sum = 0;
vector<pair<int, ll>> res;
for (auto [h, x] : H) {
vis[x] = true;
if (x > 0 && vis[x - 1]) {
sum += merge(x - 1, x);
}
if (x + 1 < N && vis[x + 1]) {
sum += merge(x, x + 1);
}
res.emplace_back(h, sum += 1);
}
for (int i = 0; i < Q; i++) {
int Y;
cin >> Y;
auto it = upper_bound(res.begin(), res.end(), make_pair(Y, (ll)1e18));
cout << (it == res.begin() ? 0ll : prev(it)->se) << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
4592 KB |
Output is correct |
2 |
Correct |
35 ms |
4592 KB |
Output is correct |
3 |
Correct |
32 ms |
4592 KB |
Output is correct |
4 |
Correct |
32 ms |
4464 KB |
Output is correct |
5 |
Correct |
32 ms |
4464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
4844 KB |
Output is correct |
2 |
Correct |
59 ms |
4972 KB |
Output is correct |
3 |
Correct |
53 ms |
4844 KB |
Output is correct |
4 |
Correct |
54 ms |
4972 KB |
Output is correct |
5 |
Correct |
52 ms |
4808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
4972 KB |
Output is correct |
2 |
Correct |
58 ms |
4844 KB |
Output is correct |
3 |
Correct |
60 ms |
4844 KB |
Output is correct |
4 |
Correct |
61 ms |
5096 KB |
Output is correct |
5 |
Correct |
61 ms |
4968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
36 ms |
4592 KB |
Output is correct |
12 |
Correct |
35 ms |
4592 KB |
Output is correct |
13 |
Correct |
32 ms |
4592 KB |
Output is correct |
14 |
Correct |
32 ms |
4464 KB |
Output is correct |
15 |
Correct |
32 ms |
4464 KB |
Output is correct |
16 |
Correct |
32 ms |
4464 KB |
Output is correct |
17 |
Correct |
33 ms |
4592 KB |
Output is correct |
18 |
Correct |
35 ms |
4592 KB |
Output is correct |
19 |
Correct |
31 ms |
4464 KB |
Output is correct |
20 |
Correct |
33 ms |
4592 KB |
Output is correct |
21 |
Correct |
31 ms |
4472 KB |
Output is correct |
22 |
Correct |
32 ms |
4464 KB |
Output is correct |
23 |
Correct |
34 ms |
4592 KB |
Output is correct |
24 |
Correct |
31 ms |
4472 KB |
Output is correct |
25 |
Correct |
33 ms |
4600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
36 ms |
4592 KB |
Output is correct |
27 |
Correct |
35 ms |
4592 KB |
Output is correct |
28 |
Correct |
32 ms |
4592 KB |
Output is correct |
29 |
Correct |
32 ms |
4464 KB |
Output is correct |
30 |
Correct |
32 ms |
4464 KB |
Output is correct |
31 |
Correct |
57 ms |
4844 KB |
Output is correct |
32 |
Correct |
59 ms |
4972 KB |
Output is correct |
33 |
Correct |
53 ms |
4844 KB |
Output is correct |
34 |
Correct |
54 ms |
4972 KB |
Output is correct |
35 |
Correct |
52 ms |
4808 KB |
Output is correct |
36 |
Correct |
60 ms |
4972 KB |
Output is correct |
37 |
Correct |
58 ms |
4844 KB |
Output is correct |
38 |
Correct |
60 ms |
4844 KB |
Output is correct |
39 |
Correct |
61 ms |
5096 KB |
Output is correct |
40 |
Correct |
61 ms |
4968 KB |
Output is correct |
41 |
Correct |
32 ms |
4464 KB |
Output is correct |
42 |
Correct |
33 ms |
4592 KB |
Output is correct |
43 |
Correct |
35 ms |
4592 KB |
Output is correct |
44 |
Correct |
31 ms |
4464 KB |
Output is correct |
45 |
Correct |
33 ms |
4592 KB |
Output is correct |
46 |
Correct |
31 ms |
4472 KB |
Output is correct |
47 |
Correct |
32 ms |
4464 KB |
Output is correct |
48 |
Correct |
34 ms |
4592 KB |
Output is correct |
49 |
Correct |
31 ms |
4472 KB |
Output is correct |
50 |
Correct |
33 ms |
4600 KB |
Output is correct |
51 |
Correct |
65 ms |
4876 KB |
Output is correct |
52 |
Correct |
70 ms |
4588 KB |
Output is correct |
53 |
Correct |
66 ms |
4972 KB |
Output is correct |
54 |
Correct |
69 ms |
4588 KB |
Output is correct |
55 |
Correct |
64 ms |
4848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
36 ms |
4592 KB |
Output is correct |
27 |
Correct |
35 ms |
4592 KB |
Output is correct |
28 |
Correct |
32 ms |
4592 KB |
Output is correct |
29 |
Correct |
32 ms |
4464 KB |
Output is correct |
30 |
Correct |
32 ms |
4464 KB |
Output is correct |
31 |
Correct |
57 ms |
4844 KB |
Output is correct |
32 |
Correct |
59 ms |
4972 KB |
Output is correct |
33 |
Correct |
53 ms |
4844 KB |
Output is correct |
34 |
Correct |
54 ms |
4972 KB |
Output is correct |
35 |
Correct |
52 ms |
4808 KB |
Output is correct |
36 |
Correct |
60 ms |
4972 KB |
Output is correct |
37 |
Correct |
58 ms |
4844 KB |
Output is correct |
38 |
Correct |
60 ms |
4844 KB |
Output is correct |
39 |
Correct |
61 ms |
5096 KB |
Output is correct |
40 |
Correct |
61 ms |
4968 KB |
Output is correct |
41 |
Correct |
32 ms |
4464 KB |
Output is correct |
42 |
Correct |
33 ms |
4592 KB |
Output is correct |
43 |
Correct |
35 ms |
4592 KB |
Output is correct |
44 |
Correct |
31 ms |
4464 KB |
Output is correct |
45 |
Correct |
33 ms |
4592 KB |
Output is correct |
46 |
Correct |
31 ms |
4472 KB |
Output is correct |
47 |
Correct |
32 ms |
4464 KB |
Output is correct |
48 |
Correct |
34 ms |
4592 KB |
Output is correct |
49 |
Correct |
31 ms |
4472 KB |
Output is correct |
50 |
Correct |
33 ms |
4600 KB |
Output is correct |
51 |
Correct |
65 ms |
4876 KB |
Output is correct |
52 |
Correct |
70 ms |
4588 KB |
Output is correct |
53 |
Correct |
66 ms |
4972 KB |
Output is correct |
54 |
Correct |
69 ms |
4588 KB |
Output is correct |
55 |
Correct |
64 ms |
4848 KB |
Output is correct |
56 |
Correct |
990 ms |
45548 KB |
Output is correct |
57 |
Execution timed out |
1008 ms |
55488 KB |
Time limit exceeded |
58 |
Halted |
0 ms |
0 KB |
- |