#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1000005;
int n, q;
int h[N], y[N];
struct event {
int ty, h, idx;
bool operator<(const event& ot) {
if(h == ot.h)
return ty < ot.ty;
return h < ot.h;
}
};
ll cur;
ll ans[N];
vector<event> events;
int par[N], sz[N];
int find(int x) {
if(par[x] == -1) return -1;
if(par[x] == x) return x;
else return par[x] = find(par[x]);
}
void join(int x, int y) {
int parX = find(x);
int parY = find(y);
par[parX] = parY;
sz[parY] = sz[parX] + sz[parY];
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> q;
for(int i = 0; i < n; i++) {
cin >> h[i];
events.push_back({0, h[i], i});
}
for(int i = 0; i < q; i++) {
cin >> y[i];
events.push_back({1, y[i], i});
}
for(int i = 0; i < n; i++) {
par[i] = -1;
sz[i] = 1;
}
sort(events.begin(), events.end());
cur = 0;
for(auto& e : events) {
if(e.ty == 0) {
par[e.idx] = e.idx;
ll l = 0, r = 0;
if(e.idx > 0 && par[e.idx-1] != -1) {
l = sz[find(e.idx-1)];
join(e.idx, e.idx-1);
}
if(e.idx < n-1 && par[e.idx+1] != -1) {
r = sz[find(e.idx+1)];
join(e.idx, e.idx+1);
}
cur += (l+1) * (r+1);
} else {
ans[e.idx] = cur;
}
}
for(int i = 0; i < q; i++)
cout << ans[i] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 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 |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
8 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 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 |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
8 ms |
384 KB |
Output is correct |
9 |
Correct |
5 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 |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 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 |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
8 ms |
384 KB |
Output is correct |
9 |
Correct |
5 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 |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 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 |
512 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 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 |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
8 ms |
384 KB |
Output is correct |
9 |
Correct |
5 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 |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 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 |
512 KB |
Output is correct |
22 |
Correct |
5 ms |
512 KB |
Output is correct |
23 |
Correct |
5 ms |
512 KB |
Output is correct |
24 |
Correct |
5 ms |
512 KB |
Output is correct |
25 |
Correct |
5 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
3444 KB |
Output is correct |
2 |
Correct |
31 ms |
3572 KB |
Output is correct |
3 |
Correct |
28 ms |
3316 KB |
Output is correct |
4 |
Correct |
27 ms |
3316 KB |
Output is correct |
5 |
Correct |
27 ms |
3316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
7012 KB |
Output is correct |
2 |
Correct |
59 ms |
7236 KB |
Output is correct |
3 |
Correct |
59 ms |
7204 KB |
Output is correct |
4 |
Correct |
60 ms |
7204 KB |
Output is correct |
5 |
Correct |
57 ms |
7008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
7144 KB |
Output is correct |
2 |
Correct |
60 ms |
7140 KB |
Output is correct |
3 |
Correct |
57 ms |
7268 KB |
Output is correct |
4 |
Correct |
61 ms |
7248 KB |
Output is correct |
5 |
Correct |
61 ms |
7400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 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 |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
8 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
28 ms |
3444 KB |
Output is correct |
12 |
Correct |
31 ms |
3572 KB |
Output is correct |
13 |
Correct |
28 ms |
3316 KB |
Output is correct |
14 |
Correct |
27 ms |
3316 KB |
Output is correct |
15 |
Correct |
27 ms |
3316 KB |
Output is correct |
16 |
Correct |
28 ms |
3188 KB |
Output is correct |
17 |
Correct |
32 ms |
3444 KB |
Output is correct |
18 |
Correct |
30 ms |
3572 KB |
Output is correct |
19 |
Correct |
28 ms |
3188 KB |
Output is correct |
20 |
Correct |
30 ms |
3444 KB |
Output is correct |
21 |
Correct |
28 ms |
3188 KB |
Output is correct |
22 |
Correct |
28 ms |
3316 KB |
Output is correct |
23 |
Correct |
30 ms |
3444 KB |
Output is correct |
24 |
Correct |
29 ms |
3316 KB |
Output is correct |
25 |
Correct |
29 ms |
3316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 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 |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
8 ms |
384 KB |
Output is correct |
9 |
Correct |
5 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 |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 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 |
512 KB |
Output is correct |
22 |
Correct |
5 ms |
512 KB |
Output is correct |
23 |
Correct |
5 ms |
512 KB |
Output is correct |
24 |
Correct |
5 ms |
512 KB |
Output is correct |
25 |
Correct |
5 ms |
512 KB |
Output is correct |
26 |
Correct |
28 ms |
3444 KB |
Output is correct |
27 |
Correct |
31 ms |
3572 KB |
Output is correct |
28 |
Correct |
28 ms |
3316 KB |
Output is correct |
29 |
Correct |
27 ms |
3316 KB |
Output is correct |
30 |
Correct |
27 ms |
3316 KB |
Output is correct |
31 |
Correct |
57 ms |
7012 KB |
Output is correct |
32 |
Correct |
59 ms |
7236 KB |
Output is correct |
33 |
Correct |
59 ms |
7204 KB |
Output is correct |
34 |
Correct |
60 ms |
7204 KB |
Output is correct |
35 |
Correct |
57 ms |
7008 KB |
Output is correct |
36 |
Correct |
59 ms |
7144 KB |
Output is correct |
37 |
Correct |
60 ms |
7140 KB |
Output is correct |
38 |
Correct |
57 ms |
7268 KB |
Output is correct |
39 |
Correct |
61 ms |
7248 KB |
Output is correct |
40 |
Correct |
61 ms |
7400 KB |
Output is correct |
41 |
Correct |
28 ms |
3188 KB |
Output is correct |
42 |
Correct |
32 ms |
3444 KB |
Output is correct |
43 |
Correct |
30 ms |
3572 KB |
Output is correct |
44 |
Correct |
28 ms |
3188 KB |
Output is correct |
45 |
Correct |
30 ms |
3444 KB |
Output is correct |
46 |
Correct |
28 ms |
3188 KB |
Output is correct |
47 |
Correct |
28 ms |
3316 KB |
Output is correct |
48 |
Correct |
30 ms |
3444 KB |
Output is correct |
49 |
Correct |
29 ms |
3316 KB |
Output is correct |
50 |
Correct |
29 ms |
3316 KB |
Output is correct |
51 |
Correct |
62 ms |
7012 KB |
Output is correct |
52 |
Correct |
61 ms |
7016 KB |
Output is correct |
53 |
Correct |
61 ms |
6988 KB |
Output is correct |
54 |
Correct |
64 ms |
6860 KB |
Output is correct |
55 |
Correct |
62 ms |
7012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 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 |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
8 ms |
384 KB |
Output is correct |
9 |
Correct |
5 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 |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 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 |
512 KB |
Output is correct |
22 |
Correct |
5 ms |
512 KB |
Output is correct |
23 |
Correct |
5 ms |
512 KB |
Output is correct |
24 |
Correct |
5 ms |
512 KB |
Output is correct |
25 |
Correct |
5 ms |
512 KB |
Output is correct |
26 |
Correct |
28 ms |
3444 KB |
Output is correct |
27 |
Correct |
31 ms |
3572 KB |
Output is correct |
28 |
Correct |
28 ms |
3316 KB |
Output is correct |
29 |
Correct |
27 ms |
3316 KB |
Output is correct |
30 |
Correct |
27 ms |
3316 KB |
Output is correct |
31 |
Correct |
57 ms |
7012 KB |
Output is correct |
32 |
Correct |
59 ms |
7236 KB |
Output is correct |
33 |
Correct |
59 ms |
7204 KB |
Output is correct |
34 |
Correct |
60 ms |
7204 KB |
Output is correct |
35 |
Correct |
57 ms |
7008 KB |
Output is correct |
36 |
Correct |
59 ms |
7144 KB |
Output is correct |
37 |
Correct |
60 ms |
7140 KB |
Output is correct |
38 |
Correct |
57 ms |
7268 KB |
Output is correct |
39 |
Correct |
61 ms |
7248 KB |
Output is correct |
40 |
Correct |
61 ms |
7400 KB |
Output is correct |
41 |
Correct |
28 ms |
3188 KB |
Output is correct |
42 |
Correct |
32 ms |
3444 KB |
Output is correct |
43 |
Correct |
30 ms |
3572 KB |
Output is correct |
44 |
Correct |
28 ms |
3188 KB |
Output is correct |
45 |
Correct |
30 ms |
3444 KB |
Output is correct |
46 |
Correct |
28 ms |
3188 KB |
Output is correct |
47 |
Correct |
28 ms |
3316 KB |
Output is correct |
48 |
Correct |
30 ms |
3444 KB |
Output is correct |
49 |
Correct |
29 ms |
3316 KB |
Output is correct |
50 |
Correct |
29 ms |
3316 KB |
Output is correct |
51 |
Correct |
62 ms |
7012 KB |
Output is correct |
52 |
Correct |
61 ms |
7016 KB |
Output is correct |
53 |
Correct |
61 ms |
6988 KB |
Output is correct |
54 |
Correct |
64 ms |
6860 KB |
Output is correct |
55 |
Correct |
62 ms |
7012 KB |
Output is correct |
56 |
Correct |
692 ms |
66348 KB |
Output is correct |
57 |
Correct |
670 ms |
65792 KB |
Output is correct |
58 |
Correct |
640 ms |
62384 KB |
Output is correct |
59 |
Correct |
632 ms |
63788 KB |
Output is correct |
60 |
Correct |
665 ms |
67872 KB |
Output is correct |