#include "bits/stdc++.h"
using namespace std;
const int maxn = 1e6 + 5;
int n, q;
pair<int, int> y[maxn];
pair<int, int> h[maxn];
int par[maxn], sz[maxn];
long long total[maxn];
bool check[maxn];
long long ans[maxn], res[maxn];
struct dsu {
int n;
dsu() {}
dsu(int n) : n(n) {
for(int i = 1; i <= n; ++i) {
sz[i] = 1;
par[i] = i;
total[i] = 1;
}
}
int find(int u) {
return u == par[u] ? u : par[u] = find(par[u]);
}
void join(int u, int v) {
u = find(u), v = find(v);
if(u == v) return;
par[v] = u;
total[u] += total[v];
total[u] += 1ll * sz[u] * sz[v];
sz[u] += sz[v];
}
};
void solve() {
cin >> n >> q;
for(int i = 1; i <= n; ++i) {
cin >> h[i].first;
h[i].second = i;
}
for(int i = 1; i <= q; ++i) {
cin >> y[i].first;
y[i].second = i;
}
sort(h + 1, h + n + 1);
sort(y + 1, y + q + 1);
dsu d(n);
int j = 0;
long long sum = 0;
for(int i = 1; i <= n; ++i) {
int cur = 0;
while(i + cur <= n and h[i].first == h[i + cur].first) {
if(check[h[i + cur].second - 1]) {
sum -= total[d.find(h[i + cur].second - 1)];
d.join(h[i + cur].second, h[i + cur].second - 1);
}
if(check[h[i + cur].second + 1]) {
sum -= total[d.find(h[i + cur].second + 1)];
d.join(h[i + cur].second, h[i + cur].second + 1);
}
sum += total[d.find(h[i + cur].second)];
check[h[i + cur].second] = 1;
++cur;
}
while(j <= q and y[j].first < h[i].first) ++j;
ans[j] = sum;
i = i + cur - 1;
}
for(int i = 1; i <= q; ++i) {
ans[i] = max(ans[i], ans[i - 1]);
res[y[i].second] = ans[i];
}
for(int i = 1; i <= q; ++i) {
cout << res[i] << '\n';
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
1 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
1 ms |
12632 KB |
Output is correct |
5 |
Correct |
1 ms |
12636 KB |
Output is correct |
6 |
Correct |
1 ms |
12636 KB |
Output is correct |
7 |
Correct |
1 ms |
12636 KB |
Output is correct |
8 |
Correct |
1 ms |
12636 KB |
Output is correct |
9 |
Correct |
1 ms |
12636 KB |
Output is correct |
10 |
Correct |
1 ms |
12636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
1 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
1 ms |
12632 KB |
Output is correct |
5 |
Correct |
1 ms |
12636 KB |
Output is correct |
6 |
Correct |
1 ms |
12636 KB |
Output is correct |
7 |
Correct |
1 ms |
12636 KB |
Output is correct |
8 |
Correct |
1 ms |
12636 KB |
Output is correct |
9 |
Correct |
1 ms |
12636 KB |
Output is correct |
10 |
Correct |
1 ms |
12636 KB |
Output is correct |
11 |
Correct |
1 ms |
12636 KB |
Output is correct |
12 |
Correct |
1 ms |
12636 KB |
Output is correct |
13 |
Correct |
1 ms |
12632 KB |
Output is correct |
14 |
Correct |
1 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
12636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
1 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
1 ms |
12632 KB |
Output is correct |
5 |
Correct |
1 ms |
12636 KB |
Output is correct |
6 |
Correct |
1 ms |
12636 KB |
Output is correct |
7 |
Correct |
1 ms |
12636 KB |
Output is correct |
8 |
Correct |
1 ms |
12636 KB |
Output is correct |
9 |
Correct |
1 ms |
12636 KB |
Output is correct |
10 |
Correct |
1 ms |
12636 KB |
Output is correct |
11 |
Correct |
1 ms |
12636 KB |
Output is correct |
12 |
Correct |
1 ms |
12636 KB |
Output is correct |
13 |
Correct |
1 ms |
12632 KB |
Output is correct |
14 |
Correct |
1 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
12636 KB |
Output is correct |
16 |
Correct |
1 ms |
12636 KB |
Output is correct |
17 |
Correct |
1 ms |
12636 KB |
Output is correct |
18 |
Correct |
1 ms |
12636 KB |
Output is correct |
19 |
Correct |
1 ms |
12636 KB |
Output is correct |
20 |
Correct |
1 ms |
12636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
1 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
1 ms |
12632 KB |
Output is correct |
5 |
Correct |
1 ms |
12636 KB |
Output is correct |
6 |
Correct |
1 ms |
12636 KB |
Output is correct |
7 |
Correct |
1 ms |
12636 KB |
Output is correct |
8 |
Correct |
1 ms |
12636 KB |
Output is correct |
9 |
Correct |
1 ms |
12636 KB |
Output is correct |
10 |
Correct |
1 ms |
12636 KB |
Output is correct |
11 |
Correct |
1 ms |
12636 KB |
Output is correct |
12 |
Correct |
1 ms |
12636 KB |
Output is correct |
13 |
Correct |
1 ms |
12632 KB |
Output is correct |
14 |
Correct |
1 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
12636 KB |
Output is correct |
16 |
Correct |
1 ms |
12636 KB |
Output is correct |
17 |
Correct |
1 ms |
12636 KB |
Output is correct |
18 |
Correct |
1 ms |
12636 KB |
Output is correct |
19 |
Correct |
1 ms |
12636 KB |
Output is correct |
20 |
Correct |
1 ms |
12636 KB |
Output is correct |
21 |
Correct |
2 ms |
12632 KB |
Output is correct |
22 |
Correct |
2 ms |
12636 KB |
Output is correct |
23 |
Correct |
2 ms |
12772 KB |
Output is correct |
24 |
Correct |
2 ms |
12636 KB |
Output is correct |
25 |
Correct |
1 ms |
12636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
17492 KB |
Output is correct |
2 |
Correct |
17 ms |
17500 KB |
Output is correct |
3 |
Correct |
16 ms |
17500 KB |
Output is correct |
4 |
Correct |
14 ms |
15452 KB |
Output is correct |
5 |
Correct |
15 ms |
17500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
21340 KB |
Output is correct |
2 |
Correct |
33 ms |
21340 KB |
Output is correct |
3 |
Correct |
27 ms |
19260 KB |
Output is correct |
4 |
Correct |
26 ms |
21336 KB |
Output is correct |
5 |
Correct |
25 ms |
19280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
23384 KB |
Output is correct |
2 |
Correct |
25 ms |
23376 KB |
Output is correct |
3 |
Correct |
26 ms |
21340 KB |
Output is correct |
4 |
Correct |
27 ms |
23376 KB |
Output is correct |
5 |
Correct |
27 ms |
23380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
1 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
1 ms |
12632 KB |
Output is correct |
5 |
Correct |
1 ms |
12636 KB |
Output is correct |
6 |
Correct |
1 ms |
12636 KB |
Output is correct |
7 |
Correct |
1 ms |
12636 KB |
Output is correct |
8 |
Correct |
1 ms |
12636 KB |
Output is correct |
9 |
Correct |
1 ms |
12636 KB |
Output is correct |
10 |
Correct |
1 ms |
12636 KB |
Output is correct |
11 |
Correct |
17 ms |
17492 KB |
Output is correct |
12 |
Correct |
17 ms |
17500 KB |
Output is correct |
13 |
Correct |
16 ms |
17500 KB |
Output is correct |
14 |
Correct |
14 ms |
15452 KB |
Output is correct |
15 |
Correct |
15 ms |
17500 KB |
Output is correct |
16 |
Correct |
17 ms |
17496 KB |
Output is correct |
17 |
Correct |
18 ms |
17472 KB |
Output is correct |
18 |
Correct |
17 ms |
17500 KB |
Output is correct |
19 |
Correct |
15 ms |
15448 KB |
Output is correct |
20 |
Correct |
16 ms |
17472 KB |
Output is correct |
21 |
Correct |
18 ms |
15324 KB |
Output is correct |
22 |
Correct |
15 ms |
17496 KB |
Output is correct |
23 |
Correct |
16 ms |
17500 KB |
Output is correct |
24 |
Correct |
14 ms |
15300 KB |
Output is correct |
25 |
Correct |
20 ms |
17412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
1 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
1 ms |
12632 KB |
Output is correct |
5 |
Correct |
1 ms |
12636 KB |
Output is correct |
6 |
Correct |
1 ms |
12636 KB |
Output is correct |
7 |
Correct |
1 ms |
12636 KB |
Output is correct |
8 |
Correct |
1 ms |
12636 KB |
Output is correct |
9 |
Correct |
1 ms |
12636 KB |
Output is correct |
10 |
Correct |
1 ms |
12636 KB |
Output is correct |
11 |
Correct |
1 ms |
12636 KB |
Output is correct |
12 |
Correct |
1 ms |
12636 KB |
Output is correct |
13 |
Correct |
1 ms |
12632 KB |
Output is correct |
14 |
Correct |
1 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
12636 KB |
Output is correct |
16 |
Correct |
1 ms |
12636 KB |
Output is correct |
17 |
Correct |
1 ms |
12636 KB |
Output is correct |
18 |
Correct |
1 ms |
12636 KB |
Output is correct |
19 |
Correct |
1 ms |
12636 KB |
Output is correct |
20 |
Correct |
1 ms |
12636 KB |
Output is correct |
21 |
Correct |
2 ms |
12632 KB |
Output is correct |
22 |
Correct |
2 ms |
12636 KB |
Output is correct |
23 |
Correct |
2 ms |
12772 KB |
Output is correct |
24 |
Correct |
2 ms |
12636 KB |
Output is correct |
25 |
Correct |
1 ms |
12636 KB |
Output is correct |
26 |
Correct |
17 ms |
17492 KB |
Output is correct |
27 |
Correct |
17 ms |
17500 KB |
Output is correct |
28 |
Correct |
16 ms |
17500 KB |
Output is correct |
29 |
Correct |
14 ms |
15452 KB |
Output is correct |
30 |
Correct |
15 ms |
17500 KB |
Output is correct |
31 |
Correct |
26 ms |
21340 KB |
Output is correct |
32 |
Correct |
33 ms |
21340 KB |
Output is correct |
33 |
Correct |
27 ms |
19260 KB |
Output is correct |
34 |
Correct |
26 ms |
21336 KB |
Output is correct |
35 |
Correct |
25 ms |
19280 KB |
Output is correct |
36 |
Correct |
25 ms |
23384 KB |
Output is correct |
37 |
Correct |
25 ms |
23376 KB |
Output is correct |
38 |
Correct |
26 ms |
21340 KB |
Output is correct |
39 |
Correct |
27 ms |
23376 KB |
Output is correct |
40 |
Correct |
27 ms |
23380 KB |
Output is correct |
41 |
Correct |
17 ms |
17496 KB |
Output is correct |
42 |
Correct |
18 ms |
17472 KB |
Output is correct |
43 |
Correct |
17 ms |
17500 KB |
Output is correct |
44 |
Correct |
15 ms |
15448 KB |
Output is correct |
45 |
Correct |
16 ms |
17472 KB |
Output is correct |
46 |
Correct |
18 ms |
15324 KB |
Output is correct |
47 |
Correct |
15 ms |
17496 KB |
Output is correct |
48 |
Correct |
16 ms |
17500 KB |
Output is correct |
49 |
Correct |
14 ms |
15300 KB |
Output is correct |
50 |
Correct |
20 ms |
17412 KB |
Output is correct |
51 |
Correct |
35 ms |
21340 KB |
Output is correct |
52 |
Correct |
34 ms |
23008 KB |
Output is correct |
53 |
Correct |
34 ms |
21332 KB |
Output is correct |
54 |
Correct |
36 ms |
23132 KB |
Output is correct |
55 |
Correct |
33 ms |
21340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
1 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
1 ms |
12632 KB |
Output is correct |
5 |
Correct |
1 ms |
12636 KB |
Output is correct |
6 |
Correct |
1 ms |
12636 KB |
Output is correct |
7 |
Correct |
1 ms |
12636 KB |
Output is correct |
8 |
Correct |
1 ms |
12636 KB |
Output is correct |
9 |
Correct |
1 ms |
12636 KB |
Output is correct |
10 |
Correct |
1 ms |
12636 KB |
Output is correct |
11 |
Correct |
1 ms |
12636 KB |
Output is correct |
12 |
Correct |
1 ms |
12636 KB |
Output is correct |
13 |
Correct |
1 ms |
12632 KB |
Output is correct |
14 |
Correct |
1 ms |
12636 KB |
Output is correct |
15 |
Correct |
1 ms |
12636 KB |
Output is correct |
16 |
Correct |
1 ms |
12636 KB |
Output is correct |
17 |
Correct |
1 ms |
12636 KB |
Output is correct |
18 |
Correct |
1 ms |
12636 KB |
Output is correct |
19 |
Correct |
1 ms |
12636 KB |
Output is correct |
20 |
Correct |
1 ms |
12636 KB |
Output is correct |
21 |
Correct |
2 ms |
12632 KB |
Output is correct |
22 |
Correct |
2 ms |
12636 KB |
Output is correct |
23 |
Correct |
2 ms |
12772 KB |
Output is correct |
24 |
Correct |
2 ms |
12636 KB |
Output is correct |
25 |
Correct |
1 ms |
12636 KB |
Output is correct |
26 |
Correct |
17 ms |
17492 KB |
Output is correct |
27 |
Correct |
17 ms |
17500 KB |
Output is correct |
28 |
Correct |
16 ms |
17500 KB |
Output is correct |
29 |
Correct |
14 ms |
15452 KB |
Output is correct |
30 |
Correct |
15 ms |
17500 KB |
Output is correct |
31 |
Correct |
26 ms |
21340 KB |
Output is correct |
32 |
Correct |
33 ms |
21340 KB |
Output is correct |
33 |
Correct |
27 ms |
19260 KB |
Output is correct |
34 |
Correct |
26 ms |
21336 KB |
Output is correct |
35 |
Correct |
25 ms |
19280 KB |
Output is correct |
36 |
Correct |
25 ms |
23384 KB |
Output is correct |
37 |
Correct |
25 ms |
23376 KB |
Output is correct |
38 |
Correct |
26 ms |
21340 KB |
Output is correct |
39 |
Correct |
27 ms |
23376 KB |
Output is correct |
40 |
Correct |
27 ms |
23380 KB |
Output is correct |
41 |
Correct |
17 ms |
17496 KB |
Output is correct |
42 |
Correct |
18 ms |
17472 KB |
Output is correct |
43 |
Correct |
17 ms |
17500 KB |
Output is correct |
44 |
Correct |
15 ms |
15448 KB |
Output is correct |
45 |
Correct |
16 ms |
17472 KB |
Output is correct |
46 |
Correct |
18 ms |
15324 KB |
Output is correct |
47 |
Correct |
15 ms |
17496 KB |
Output is correct |
48 |
Correct |
16 ms |
17500 KB |
Output is correct |
49 |
Correct |
14 ms |
15300 KB |
Output is correct |
50 |
Correct |
20 ms |
17412 KB |
Output is correct |
51 |
Correct |
35 ms |
21340 KB |
Output is correct |
52 |
Correct |
34 ms |
23008 KB |
Output is correct |
53 |
Correct |
34 ms |
21332 KB |
Output is correct |
54 |
Correct |
36 ms |
23132 KB |
Output is correct |
55 |
Correct |
33 ms |
21340 KB |
Output is correct |
56 |
Correct |
360 ms |
68688 KB |
Output is correct |
57 |
Correct |
363 ms |
67388 KB |
Output is correct |
58 |
Correct |
338 ms |
66132 KB |
Output is correct |
59 |
Correct |
339 ms |
66644 KB |
Output is correct |
60 |
Correct |
397 ms |
68948 KB |
Output is correct |