#define fast ios::sync_with_stdio(false); cin.tie(0)
#define foru(i, k, n) for (int i = k; i < n; i++)
#define ford(i, k, n) for (int i = k; i >= n; i--)
#define pb push_back
#define mp make_pair
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <bitset>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int sz = 1e6 + 5;
struct dsu {
int f[sz], p[sz], rk[sz];
dsu(int n) {
foru(i, 0, n) { f[i] = i; p[i] = 1; rk[i] = 1; }
}
int father(int src) {
if (f[src] != src)return f[src] = father(f[src]);
else return src;
}
bool unite(int a, int b) {
int fa = father(a), fb = father(b);
if (fa == fb)return 0;
if (rk[fa] < rk[fb])swap(fa, fb);
rk[fa] = max(rk[fa], rk[fb] + 1);
f[fb] = fa;
p[fa] += p[fb];
return 1;
}
};
int n, q;
ll h[sz];
pii g[sz];
pii qu[sz];
ll ans[sz];
bitset<sz> on;
void input() {
cin >> n >> q;
foru(i, 0, n) {
cin >> h[i]; g[i].first = h[i];
g[i].second = i;
}
foru(i, 0, q) {
cin >> qu[i].first;
qu[i].second = i;
}
sort(qu, qu + q);
sort(g, g + n);
}
ll calc(ll x) {
return (x * (x + 1)) / 2;
}
int main() {
fast;
input();
dsu d(n);
ll ret = 0;
int ind = 0;
foru(i, 0, q) {
while (ind < n && h[g[ind].second] <= qu[i].first) {
int px = g[ind].second;
on[px] = 1;
if (px && on[px - 1]) {
ret -= calc(d.p[d.father(px - 1)]);
d.unite(px, px - 1);
}
if (px != n - 1 && on[px + 1]) {
ret -= calc(d.p[d.father(px + 1)]);
d.unite(px, px + 1);
}
ret += calc(d.p[d.father(px)]);
ind++;
}
ans[qu[i].second] = ret;
}
foru(i, 0, q)cout << ans[i] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 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 |
1 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 |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 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 |
1 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 |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 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 |
1 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 |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 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 |
1 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 |
492 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 |
23 ms |
3564 KB |
Output is correct |
2 |
Correct |
24 ms |
3692 KB |
Output is correct |
3 |
Correct |
23 ms |
3584 KB |
Output is correct |
4 |
Correct |
22 ms |
3436 KB |
Output is correct |
5 |
Correct |
24 ms |
3564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
6636 KB |
Output is correct |
2 |
Correct |
44 ms |
6636 KB |
Output is correct |
3 |
Correct |
45 ms |
6528 KB |
Output is correct |
4 |
Correct |
44 ms |
6636 KB |
Output is correct |
5 |
Correct |
43 ms |
6508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
6636 KB |
Output is correct |
2 |
Correct |
44 ms |
6636 KB |
Output is correct |
3 |
Correct |
44 ms |
6636 KB |
Output is correct |
4 |
Correct |
46 ms |
6764 KB |
Output is correct |
5 |
Correct |
48 ms |
6892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 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 |
1 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 |
23 ms |
3564 KB |
Output is correct |
12 |
Correct |
24 ms |
3692 KB |
Output is correct |
13 |
Correct |
23 ms |
3584 KB |
Output is correct |
14 |
Correct |
22 ms |
3436 KB |
Output is correct |
15 |
Correct |
24 ms |
3564 KB |
Output is correct |
16 |
Correct |
21 ms |
3436 KB |
Output is correct |
17 |
Correct |
22 ms |
3820 KB |
Output is correct |
18 |
Correct |
21 ms |
3840 KB |
Output is correct |
19 |
Correct |
19 ms |
3436 KB |
Output is correct |
20 |
Correct |
21 ms |
3820 KB |
Output is correct |
21 |
Correct |
22 ms |
3436 KB |
Output is correct |
22 |
Correct |
20 ms |
3564 KB |
Output is correct |
23 |
Correct |
22 ms |
3692 KB |
Output is correct |
24 |
Correct |
19 ms |
3564 KB |
Output is correct |
25 |
Correct |
21 ms |
3564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 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 |
1 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 |
492 KB |
Output is correct |
24 |
Correct |
1 ms |
492 KB |
Output is correct |
25 |
Correct |
1 ms |
492 KB |
Output is correct |
26 |
Correct |
23 ms |
3564 KB |
Output is correct |
27 |
Correct |
24 ms |
3692 KB |
Output is correct |
28 |
Correct |
23 ms |
3584 KB |
Output is correct |
29 |
Correct |
22 ms |
3436 KB |
Output is correct |
30 |
Correct |
24 ms |
3564 KB |
Output is correct |
31 |
Correct |
43 ms |
6636 KB |
Output is correct |
32 |
Correct |
44 ms |
6636 KB |
Output is correct |
33 |
Correct |
45 ms |
6528 KB |
Output is correct |
34 |
Correct |
44 ms |
6636 KB |
Output is correct |
35 |
Correct |
43 ms |
6508 KB |
Output is correct |
36 |
Correct |
48 ms |
6636 KB |
Output is correct |
37 |
Correct |
44 ms |
6636 KB |
Output is correct |
38 |
Correct |
44 ms |
6636 KB |
Output is correct |
39 |
Correct |
46 ms |
6764 KB |
Output is correct |
40 |
Correct |
48 ms |
6892 KB |
Output is correct |
41 |
Correct |
21 ms |
3436 KB |
Output is correct |
42 |
Correct |
22 ms |
3820 KB |
Output is correct |
43 |
Correct |
21 ms |
3840 KB |
Output is correct |
44 |
Correct |
19 ms |
3436 KB |
Output is correct |
45 |
Correct |
21 ms |
3820 KB |
Output is correct |
46 |
Correct |
22 ms |
3436 KB |
Output is correct |
47 |
Correct |
20 ms |
3564 KB |
Output is correct |
48 |
Correct |
22 ms |
3692 KB |
Output is correct |
49 |
Correct |
19 ms |
3564 KB |
Output is correct |
50 |
Correct |
21 ms |
3564 KB |
Output is correct |
51 |
Correct |
55 ms |
6600 KB |
Output is correct |
52 |
Correct |
53 ms |
6508 KB |
Output is correct |
53 |
Correct |
54 ms |
6508 KB |
Output is correct |
54 |
Correct |
52 ms |
6380 KB |
Output is correct |
55 |
Correct |
56 ms |
6636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 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 |
1 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 |
492 KB |
Output is correct |
24 |
Correct |
1 ms |
492 KB |
Output is correct |
25 |
Correct |
1 ms |
492 KB |
Output is correct |
26 |
Correct |
23 ms |
3564 KB |
Output is correct |
27 |
Correct |
24 ms |
3692 KB |
Output is correct |
28 |
Correct |
23 ms |
3584 KB |
Output is correct |
29 |
Correct |
22 ms |
3436 KB |
Output is correct |
30 |
Correct |
24 ms |
3564 KB |
Output is correct |
31 |
Correct |
43 ms |
6636 KB |
Output is correct |
32 |
Correct |
44 ms |
6636 KB |
Output is correct |
33 |
Correct |
45 ms |
6528 KB |
Output is correct |
34 |
Correct |
44 ms |
6636 KB |
Output is correct |
35 |
Correct |
43 ms |
6508 KB |
Output is correct |
36 |
Correct |
48 ms |
6636 KB |
Output is correct |
37 |
Correct |
44 ms |
6636 KB |
Output is correct |
38 |
Correct |
44 ms |
6636 KB |
Output is correct |
39 |
Correct |
46 ms |
6764 KB |
Output is correct |
40 |
Correct |
48 ms |
6892 KB |
Output is correct |
41 |
Correct |
21 ms |
3436 KB |
Output is correct |
42 |
Correct |
22 ms |
3820 KB |
Output is correct |
43 |
Correct |
21 ms |
3840 KB |
Output is correct |
44 |
Correct |
19 ms |
3436 KB |
Output is correct |
45 |
Correct |
21 ms |
3820 KB |
Output is correct |
46 |
Correct |
22 ms |
3436 KB |
Output is correct |
47 |
Correct |
20 ms |
3564 KB |
Output is correct |
48 |
Correct |
22 ms |
3692 KB |
Output is correct |
49 |
Correct |
19 ms |
3564 KB |
Output is correct |
50 |
Correct |
21 ms |
3564 KB |
Output is correct |
51 |
Correct |
55 ms |
6600 KB |
Output is correct |
52 |
Correct |
53 ms |
6508 KB |
Output is correct |
53 |
Correct |
54 ms |
6508 KB |
Output is correct |
54 |
Correct |
52 ms |
6380 KB |
Output is correct |
55 |
Correct |
56 ms |
6636 KB |
Output is correct |
56 |
Correct |
694 ms |
62444 KB |
Output is correct |
57 |
Correct |
701 ms |
62328 KB |
Output is correct |
58 |
Correct |
643 ms |
58604 KB |
Output is correct |
59 |
Correct |
664 ms |
60268 KB |
Output is correct |
60 |
Correct |
705 ms |
63980 KB |
Output is correct |