#include <bits/stdc++.h>
using namespace std;
#define MASK(x) (1LL << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0, _n = n; i < _n; ++i)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define FORE(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define debug(...) "[" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
template <class A, class B> bool minimize(A &a, B b) { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b) { if (a < b) { a = b; return true; } return false; }
long long f(int x) { return 1LL * x * (x - 1) / 2; }
struct disjoint_set {
vector <int> lab;
disjoint_set (int n = 0) : lab(n + 1, -1) {}
int find(int u) { return lab[u] < 0 ? u : lab[u] = find(lab[u]); }
bool merge(int u, int v) {
u = find(u); v = find(v);
if(u == v) return false;
if(lab[u] > lab[v]) swap(u, v);
lab[u] += lab[v];
lab[v] = u;
return true;
}
bool same_component (int u, int v) { return find(u) == find(v); }
int component_size(int i) { return -lab[find(i)]; }
};
const int MAXN = 1e6 + 5;
int N, Q, dd[MAXN];
long long ans[MAXN], total_pair;
pair <int, int> a[MAXN], q[MAXN];
void you_make_it(void) {
cin >> N >> Q;
FOR(i, 1, N) cin >> a[i].first, a[i].second = i;
FOR(i, 1, Q) cin >> q[i].first, q[i].second = i;
sort(a + 1, a + N + 1), sort(q + 1, q + Q + 1);
int j = 0;
disjoint_set dsu(N);
auto merge = [&] (int u, int v) {
int a = dsu.component_size(u), b = dsu.component_size(v);
if(dsu.merge(u, v)) {
total_pair -= f(a) + f(b);
total_pair += f(a + b);
}
};
auto add = [&] (int i) {
dd[i] = 1;
if(i < N and dd[i + 1]) merge(i, i + 1);
if(i > 1 and dd[i - 1]) merge(i, i - 1);
};
FOR(i, 1, Q) {
auto [x, id] = q[i];
while(j <= N and a[j].first <= x) {
add(a[j++].second);
}
ans[id] = total_pair + j - 1;
}
FOR(i, 1, Q) cout << ans[i] << '\n';
}
signed main() {
#ifdef LOCAL
freopen("TASK.inp", "r", stdin);
freopen("TASK.out", "w", stdout);
#endif
auto start_time = chrono::steady_clock::now();
cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
you_make_it();
auto end_time = chrono::steady_clock::now();
cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl;
return (0 ^ 0);
}
// Dream it. Wish it. Do it.
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4440 KB |
Output is correct |
8 |
Correct |
1 ms |
4440 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4440 KB |
Output is correct |
8 |
Correct |
1 ms |
4440 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4440 KB |
Output is correct |
15 |
Correct |
1 ms |
4440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4440 KB |
Output is correct |
8 |
Correct |
1 ms |
4440 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4440 KB |
Output is correct |
15 |
Correct |
1 ms |
4440 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4440 KB |
Output is correct |
19 |
Correct |
1 ms |
4440 KB |
Output is correct |
20 |
Correct |
1 ms |
4440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4440 KB |
Output is correct |
8 |
Correct |
1 ms |
4440 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4440 KB |
Output is correct |
15 |
Correct |
1 ms |
4440 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4440 KB |
Output is correct |
19 |
Correct |
1 ms |
4440 KB |
Output is correct |
20 |
Correct |
1 ms |
4440 KB |
Output is correct |
21 |
Correct |
1 ms |
4440 KB |
Output is correct |
22 |
Correct |
1 ms |
4440 KB |
Output is correct |
23 |
Correct |
1 ms |
4440 KB |
Output is correct |
24 |
Correct |
1 ms |
4440 KB |
Output is correct |
25 |
Correct |
1 ms |
4440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
7000 KB |
Output is correct |
2 |
Correct |
16 ms |
7000 KB |
Output is correct |
3 |
Correct |
16 ms |
7000 KB |
Output is correct |
4 |
Correct |
14 ms |
6748 KB |
Output is correct |
5 |
Correct |
15 ms |
7000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
10832 KB |
Output is correct |
2 |
Correct |
29 ms |
10832 KB |
Output is correct |
3 |
Correct |
28 ms |
10832 KB |
Output is correct |
4 |
Correct |
28 ms |
10840 KB |
Output is correct |
5 |
Correct |
29 ms |
10832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
10876 KB |
Output is correct |
2 |
Correct |
28 ms |
10832 KB |
Output is correct |
3 |
Correct |
29 ms |
10792 KB |
Output is correct |
4 |
Correct |
28 ms |
10832 KB |
Output is correct |
5 |
Correct |
31 ms |
10840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4440 KB |
Output is correct |
8 |
Correct |
1 ms |
4440 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
16 ms |
7000 KB |
Output is correct |
12 |
Correct |
16 ms |
7000 KB |
Output is correct |
13 |
Correct |
16 ms |
7000 KB |
Output is correct |
14 |
Correct |
14 ms |
6748 KB |
Output is correct |
15 |
Correct |
15 ms |
7000 KB |
Output is correct |
16 |
Correct |
14 ms |
7000 KB |
Output is correct |
17 |
Correct |
13 ms |
7000 KB |
Output is correct |
18 |
Correct |
16 ms |
7000 KB |
Output is correct |
19 |
Correct |
13 ms |
7000 KB |
Output is correct |
20 |
Correct |
15 ms |
7000 KB |
Output is correct |
21 |
Correct |
15 ms |
6744 KB |
Output is correct |
22 |
Correct |
13 ms |
6744 KB |
Output is correct |
23 |
Correct |
14 ms |
7000 KB |
Output is correct |
24 |
Correct |
14 ms |
7000 KB |
Output is correct |
25 |
Correct |
14 ms |
7004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4440 KB |
Output is correct |
8 |
Correct |
1 ms |
4440 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4440 KB |
Output is correct |
15 |
Correct |
1 ms |
4440 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4440 KB |
Output is correct |
19 |
Correct |
1 ms |
4440 KB |
Output is correct |
20 |
Correct |
1 ms |
4440 KB |
Output is correct |
21 |
Correct |
1 ms |
4440 KB |
Output is correct |
22 |
Correct |
1 ms |
4440 KB |
Output is correct |
23 |
Correct |
1 ms |
4440 KB |
Output is correct |
24 |
Correct |
1 ms |
4440 KB |
Output is correct |
25 |
Correct |
1 ms |
4440 KB |
Output is correct |
26 |
Correct |
16 ms |
7000 KB |
Output is correct |
27 |
Correct |
16 ms |
7000 KB |
Output is correct |
28 |
Correct |
16 ms |
7000 KB |
Output is correct |
29 |
Correct |
14 ms |
6748 KB |
Output is correct |
30 |
Correct |
15 ms |
7000 KB |
Output is correct |
31 |
Correct |
28 ms |
10832 KB |
Output is correct |
32 |
Correct |
29 ms |
10832 KB |
Output is correct |
33 |
Correct |
28 ms |
10832 KB |
Output is correct |
34 |
Correct |
28 ms |
10840 KB |
Output is correct |
35 |
Correct |
29 ms |
10832 KB |
Output is correct |
36 |
Correct |
28 ms |
10876 KB |
Output is correct |
37 |
Correct |
28 ms |
10832 KB |
Output is correct |
38 |
Correct |
29 ms |
10792 KB |
Output is correct |
39 |
Correct |
28 ms |
10832 KB |
Output is correct |
40 |
Correct |
31 ms |
10840 KB |
Output is correct |
41 |
Correct |
14 ms |
7000 KB |
Output is correct |
42 |
Correct |
13 ms |
7000 KB |
Output is correct |
43 |
Correct |
16 ms |
7000 KB |
Output is correct |
44 |
Correct |
13 ms |
7000 KB |
Output is correct |
45 |
Correct |
15 ms |
7000 KB |
Output is correct |
46 |
Correct |
15 ms |
6744 KB |
Output is correct |
47 |
Correct |
13 ms |
6744 KB |
Output is correct |
48 |
Correct |
14 ms |
7000 KB |
Output is correct |
49 |
Correct |
14 ms |
7000 KB |
Output is correct |
50 |
Correct |
14 ms |
7004 KB |
Output is correct |
51 |
Correct |
37 ms |
10576 KB |
Output is correct |
52 |
Correct |
42 ms |
10364 KB |
Output is correct |
53 |
Correct |
35 ms |
10620 KB |
Output is correct |
54 |
Correct |
34 ms |
10372 KB |
Output is correct |
55 |
Correct |
35 ms |
10576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4440 KB |
Output is correct |
8 |
Correct |
1 ms |
4440 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4440 KB |
Output is correct |
15 |
Correct |
1 ms |
4440 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4440 KB |
Output is correct |
19 |
Correct |
1 ms |
4440 KB |
Output is correct |
20 |
Correct |
1 ms |
4440 KB |
Output is correct |
21 |
Correct |
1 ms |
4440 KB |
Output is correct |
22 |
Correct |
1 ms |
4440 KB |
Output is correct |
23 |
Correct |
1 ms |
4440 KB |
Output is correct |
24 |
Correct |
1 ms |
4440 KB |
Output is correct |
25 |
Correct |
1 ms |
4440 KB |
Output is correct |
26 |
Correct |
16 ms |
7000 KB |
Output is correct |
27 |
Correct |
16 ms |
7000 KB |
Output is correct |
28 |
Correct |
16 ms |
7000 KB |
Output is correct |
29 |
Correct |
14 ms |
6748 KB |
Output is correct |
30 |
Correct |
15 ms |
7000 KB |
Output is correct |
31 |
Correct |
28 ms |
10832 KB |
Output is correct |
32 |
Correct |
29 ms |
10832 KB |
Output is correct |
33 |
Correct |
28 ms |
10832 KB |
Output is correct |
34 |
Correct |
28 ms |
10840 KB |
Output is correct |
35 |
Correct |
29 ms |
10832 KB |
Output is correct |
36 |
Correct |
28 ms |
10876 KB |
Output is correct |
37 |
Correct |
28 ms |
10832 KB |
Output is correct |
38 |
Correct |
29 ms |
10792 KB |
Output is correct |
39 |
Correct |
28 ms |
10832 KB |
Output is correct |
40 |
Correct |
31 ms |
10840 KB |
Output is correct |
41 |
Correct |
14 ms |
7000 KB |
Output is correct |
42 |
Correct |
13 ms |
7000 KB |
Output is correct |
43 |
Correct |
16 ms |
7000 KB |
Output is correct |
44 |
Correct |
13 ms |
7000 KB |
Output is correct |
45 |
Correct |
15 ms |
7000 KB |
Output is correct |
46 |
Correct |
15 ms |
6744 KB |
Output is correct |
47 |
Correct |
13 ms |
6744 KB |
Output is correct |
48 |
Correct |
14 ms |
7000 KB |
Output is correct |
49 |
Correct |
14 ms |
7000 KB |
Output is correct |
50 |
Correct |
14 ms |
7004 KB |
Output is correct |
51 |
Correct |
37 ms |
10576 KB |
Output is correct |
52 |
Correct |
42 ms |
10364 KB |
Output is correct |
53 |
Correct |
35 ms |
10620 KB |
Output is correct |
54 |
Correct |
34 ms |
10372 KB |
Output is correct |
55 |
Correct |
35 ms |
10576 KB |
Output is correct |
56 |
Correct |
398 ms |
49824 KB |
Output is correct |
57 |
Correct |
391 ms |
51536 KB |
Output is correct |
58 |
Correct |
380 ms |
50256 KB |
Output is correct |
59 |
Correct |
393 ms |
50736 KB |
Output is correct |
60 |
Correct |
419 ms |
52392 KB |
Output is correct |