#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 = 1e5 + 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
file("pilot");
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.
Compilation message
pilot.cpp: In function 'int main()':
pilot.cpp:13:57: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
pilot.cpp:78:4: note: in expansion of macro 'file'
78 | file("pilot");
| ^~~~
pilot.cpp:13:90: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
pilot.cpp:78:4: note: in expansion of macro 'file'
78 | file("pilot");
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2512 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2648 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2512 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2648 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
2392 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2512 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2648 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
2392 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2392 KB |
Output is correct |
18 |
Correct |
1 ms |
2648 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2512 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2648 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
2392 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2392 KB |
Output is correct |
18 |
Correct |
1 ms |
2648 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2392 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2392 KB |
Output is correct |
25 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
3672 KB |
Output is correct |
2 |
Correct |
18 ms |
4160 KB |
Output is correct |
3 |
Correct |
15 ms |
3668 KB |
Output is correct |
4 |
Correct |
14 ms |
3672 KB |
Output is correct |
5 |
Correct |
16 ms |
3672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
5516 KB |
Output is correct |
2 |
Correct |
39 ms |
5460 KB |
Output is correct |
3 |
Correct |
27 ms |
5460 KB |
Output is correct |
4 |
Correct |
28 ms |
5432 KB |
Output is correct |
5 |
Correct |
29 ms |
5568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
5456 KB |
Output is correct |
2 |
Correct |
37 ms |
5460 KB |
Output is correct |
3 |
Correct |
38 ms |
5644 KB |
Output is correct |
4 |
Correct |
28 ms |
5720 KB |
Output is correct |
5 |
Correct |
30 ms |
5712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2512 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2648 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
16 ms |
3672 KB |
Output is correct |
12 |
Correct |
18 ms |
4160 KB |
Output is correct |
13 |
Correct |
15 ms |
3668 KB |
Output is correct |
14 |
Correct |
14 ms |
3672 KB |
Output is correct |
15 |
Correct |
16 ms |
3672 KB |
Output is correct |
16 |
Correct |
16 ms |
3676 KB |
Output is correct |
17 |
Correct |
13 ms |
3932 KB |
Output is correct |
18 |
Correct |
15 ms |
3952 KB |
Output is correct |
19 |
Correct |
13 ms |
3672 KB |
Output is correct |
20 |
Correct |
15 ms |
3652 KB |
Output is correct |
21 |
Correct |
16 ms |
3656 KB |
Output is correct |
22 |
Correct |
14 ms |
3648 KB |
Output is correct |
23 |
Correct |
16 ms |
3672 KB |
Output is correct |
24 |
Correct |
14 ms |
3672 KB |
Output is correct |
25 |
Correct |
14 ms |
3672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2512 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2648 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
2392 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2392 KB |
Output is correct |
18 |
Correct |
1 ms |
2648 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2392 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2392 KB |
Output is correct |
25 |
Correct |
1 ms |
2392 KB |
Output is correct |
26 |
Correct |
16 ms |
3672 KB |
Output is correct |
27 |
Correct |
18 ms |
4160 KB |
Output is correct |
28 |
Correct |
15 ms |
3668 KB |
Output is correct |
29 |
Correct |
14 ms |
3672 KB |
Output is correct |
30 |
Correct |
16 ms |
3672 KB |
Output is correct |
31 |
Correct |
40 ms |
5516 KB |
Output is correct |
32 |
Correct |
39 ms |
5460 KB |
Output is correct |
33 |
Correct |
27 ms |
5460 KB |
Output is correct |
34 |
Correct |
28 ms |
5432 KB |
Output is correct |
35 |
Correct |
29 ms |
5568 KB |
Output is correct |
36 |
Correct |
29 ms |
5456 KB |
Output is correct |
37 |
Correct |
37 ms |
5460 KB |
Output is correct |
38 |
Correct |
38 ms |
5644 KB |
Output is correct |
39 |
Correct |
28 ms |
5720 KB |
Output is correct |
40 |
Correct |
30 ms |
5712 KB |
Output is correct |
41 |
Correct |
16 ms |
3676 KB |
Output is correct |
42 |
Correct |
13 ms |
3932 KB |
Output is correct |
43 |
Correct |
15 ms |
3952 KB |
Output is correct |
44 |
Correct |
13 ms |
3672 KB |
Output is correct |
45 |
Correct |
15 ms |
3652 KB |
Output is correct |
46 |
Correct |
16 ms |
3656 KB |
Output is correct |
47 |
Correct |
14 ms |
3648 KB |
Output is correct |
48 |
Correct |
16 ms |
3672 KB |
Output is correct |
49 |
Correct |
14 ms |
3672 KB |
Output is correct |
50 |
Correct |
14 ms |
3672 KB |
Output is correct |
51 |
Correct |
43 ms |
5456 KB |
Output is correct |
52 |
Correct |
35 ms |
5200 KB |
Output is correct |
53 |
Correct |
48 ms |
5456 KB |
Output is correct |
54 |
Correct |
34 ms |
5200 KB |
Output is correct |
55 |
Correct |
36 ms |
5468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2512 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2648 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
2392 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2392 KB |
Output is correct |
18 |
Correct |
1 ms |
2648 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2392 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2392 KB |
Output is correct |
25 |
Correct |
1 ms |
2392 KB |
Output is correct |
26 |
Correct |
16 ms |
3672 KB |
Output is correct |
27 |
Correct |
18 ms |
4160 KB |
Output is correct |
28 |
Correct |
15 ms |
3668 KB |
Output is correct |
29 |
Correct |
14 ms |
3672 KB |
Output is correct |
30 |
Correct |
16 ms |
3672 KB |
Output is correct |
31 |
Correct |
40 ms |
5516 KB |
Output is correct |
32 |
Correct |
39 ms |
5460 KB |
Output is correct |
33 |
Correct |
27 ms |
5460 KB |
Output is correct |
34 |
Correct |
28 ms |
5432 KB |
Output is correct |
35 |
Correct |
29 ms |
5568 KB |
Output is correct |
36 |
Correct |
29 ms |
5456 KB |
Output is correct |
37 |
Correct |
37 ms |
5460 KB |
Output is correct |
38 |
Correct |
38 ms |
5644 KB |
Output is correct |
39 |
Correct |
28 ms |
5720 KB |
Output is correct |
40 |
Correct |
30 ms |
5712 KB |
Output is correct |
41 |
Correct |
16 ms |
3676 KB |
Output is correct |
42 |
Correct |
13 ms |
3932 KB |
Output is correct |
43 |
Correct |
15 ms |
3952 KB |
Output is correct |
44 |
Correct |
13 ms |
3672 KB |
Output is correct |
45 |
Correct |
15 ms |
3652 KB |
Output is correct |
46 |
Correct |
16 ms |
3656 KB |
Output is correct |
47 |
Correct |
14 ms |
3648 KB |
Output is correct |
48 |
Correct |
16 ms |
3672 KB |
Output is correct |
49 |
Correct |
14 ms |
3672 KB |
Output is correct |
50 |
Correct |
14 ms |
3672 KB |
Output is correct |
51 |
Correct |
43 ms |
5456 KB |
Output is correct |
52 |
Correct |
35 ms |
5200 KB |
Output is correct |
53 |
Correct |
48 ms |
5456 KB |
Output is correct |
54 |
Correct |
34 ms |
5200 KB |
Output is correct |
55 |
Correct |
36 ms |
5468 KB |
Output is correct |
56 |
Runtime error |
18 ms |
7484 KB |
Execution killed with signal 11 |
57 |
Halted |
0 ms |
0 KB |
- |