#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
#define endl '\n'
#define pl(var) " [" << #var << ": " << (var) << "] "
#define ll long long
ll int cnt = 0;
struct DSU {
vector<int> p, s;
void init(int n) {
p = vector<int>(n);
s = vector<int>(n, 1);
iota(all(p), 0);
}
int get(int x) {
return p[x] = (p[x] == x ? x : get(p[x]));
}
void unite(int a, int b) {
if (a == b) {
cnt++;
return;
}
a = get(a);
b = get(b);
if (a == b) {
return;
}
if (s[a] > s[b]) swap(a, b);
p[a] = b;
cnt += ((ll int)s[b]) * ((ll int)s[a]);
s[b] += s[a];
}
};
void solve() {
int n; cin >> n;
int q; cin >> q;
vector<int> arr(n); for (int i = 0; i < n; i++) cin >> arr[i];
vector<pair<int, pair<int, int>>> edges;
for (int i = 1; i < n; i++) {
edges.emplace_back(max(arr[i], arr[i - 1]), make_pair(i, i - 1));
}
for (int i = 0; i < n; i++) {
edges.emplace_back(arr[i], make_pair(i, i));
}
sort(all(edges));
vector<pair<int, int>> qq;
for (int i = 0; i < q; i++) {
int a; cin >> a;
qq.emplace_back(a, i);
}
// cout << "Hello" << endl;
sort(all(qq));
vector<ll int> ans(q);
int ptr = 0;
DSU dsu;
dsu.init(n);
for (int i = 0; i < q; i++) {
int hi = qq[i].first;
// cout << pl(hi) << endl;
while (ptr < int(edges.size()) && edges[ptr].first <= hi) {
dsu.unite(edges[ptr].second.first, edges[ptr].second.second);
// cout << pl(edges[ptr].first) << pl(edges[ptr].second.first) << pl(edges[ptr].second.second) << pl(cnt) << endl;
ptr++;
}
ans[qq[i].second] = cnt;
}
for (ll int a : ans) {
cout << a << endl;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
// cin >> T;
while (T--)
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
3916 KB |
Output is correct |
2 |
Correct |
32 ms |
3912 KB |
Output is correct |
3 |
Correct |
31 ms |
3912 KB |
Output is correct |
4 |
Correct |
36 ms |
3872 KB |
Output is correct |
5 |
Correct |
33 ms |
3872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
6340 KB |
Output is correct |
2 |
Correct |
67 ms |
6324 KB |
Output is correct |
3 |
Correct |
54 ms |
6204 KB |
Output is correct |
4 |
Correct |
55 ms |
6488 KB |
Output is correct |
5 |
Correct |
56 ms |
6268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
6272 KB |
Output is correct |
2 |
Correct |
52 ms |
6308 KB |
Output is correct |
3 |
Correct |
55 ms |
6204 KB |
Output is correct |
4 |
Correct |
59 ms |
6504 KB |
Output is correct |
5 |
Correct |
57 ms |
6472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
34 ms |
3916 KB |
Output is correct |
12 |
Correct |
32 ms |
3912 KB |
Output is correct |
13 |
Correct |
31 ms |
3912 KB |
Output is correct |
14 |
Correct |
36 ms |
3872 KB |
Output is correct |
15 |
Correct |
33 ms |
3872 KB |
Output is correct |
16 |
Correct |
28 ms |
3784 KB |
Output is correct |
17 |
Correct |
29 ms |
3912 KB |
Output is correct |
18 |
Correct |
30 ms |
3912 KB |
Output is correct |
19 |
Correct |
28 ms |
3784 KB |
Output is correct |
20 |
Correct |
29 ms |
3900 KB |
Output is correct |
21 |
Correct |
29 ms |
3876 KB |
Output is correct |
22 |
Correct |
27 ms |
3912 KB |
Output is correct |
23 |
Correct |
29 ms |
3908 KB |
Output is correct |
24 |
Correct |
30 ms |
3876 KB |
Output is correct |
25 |
Correct |
33 ms |
3932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
34 ms |
3916 KB |
Output is correct |
27 |
Correct |
32 ms |
3912 KB |
Output is correct |
28 |
Correct |
31 ms |
3912 KB |
Output is correct |
29 |
Correct |
36 ms |
3872 KB |
Output is correct |
30 |
Correct |
33 ms |
3872 KB |
Output is correct |
31 |
Correct |
59 ms |
6340 KB |
Output is correct |
32 |
Correct |
67 ms |
6324 KB |
Output is correct |
33 |
Correct |
54 ms |
6204 KB |
Output is correct |
34 |
Correct |
55 ms |
6488 KB |
Output is correct |
35 |
Correct |
56 ms |
6268 KB |
Output is correct |
36 |
Correct |
74 ms |
6272 KB |
Output is correct |
37 |
Correct |
52 ms |
6308 KB |
Output is correct |
38 |
Correct |
55 ms |
6204 KB |
Output is correct |
39 |
Correct |
59 ms |
6504 KB |
Output is correct |
40 |
Correct |
57 ms |
6472 KB |
Output is correct |
41 |
Correct |
28 ms |
3784 KB |
Output is correct |
42 |
Correct |
29 ms |
3912 KB |
Output is correct |
43 |
Correct |
30 ms |
3912 KB |
Output is correct |
44 |
Correct |
28 ms |
3784 KB |
Output is correct |
45 |
Correct |
29 ms |
3900 KB |
Output is correct |
46 |
Correct |
29 ms |
3876 KB |
Output is correct |
47 |
Correct |
27 ms |
3912 KB |
Output is correct |
48 |
Correct |
29 ms |
3908 KB |
Output is correct |
49 |
Correct |
30 ms |
3876 KB |
Output is correct |
50 |
Correct |
33 ms |
3932 KB |
Output is correct |
51 |
Correct |
60 ms |
6184 KB |
Output is correct |
52 |
Correct |
53 ms |
6000 KB |
Output is correct |
53 |
Correct |
54 ms |
6328 KB |
Output is correct |
54 |
Correct |
62 ms |
6004 KB |
Output is correct |
55 |
Correct |
61 ms |
6328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
34 ms |
3916 KB |
Output is correct |
27 |
Correct |
32 ms |
3912 KB |
Output is correct |
28 |
Correct |
31 ms |
3912 KB |
Output is correct |
29 |
Correct |
36 ms |
3872 KB |
Output is correct |
30 |
Correct |
33 ms |
3872 KB |
Output is correct |
31 |
Correct |
59 ms |
6340 KB |
Output is correct |
32 |
Correct |
67 ms |
6324 KB |
Output is correct |
33 |
Correct |
54 ms |
6204 KB |
Output is correct |
34 |
Correct |
55 ms |
6488 KB |
Output is correct |
35 |
Correct |
56 ms |
6268 KB |
Output is correct |
36 |
Correct |
74 ms |
6272 KB |
Output is correct |
37 |
Correct |
52 ms |
6308 KB |
Output is correct |
38 |
Correct |
55 ms |
6204 KB |
Output is correct |
39 |
Correct |
59 ms |
6504 KB |
Output is correct |
40 |
Correct |
57 ms |
6472 KB |
Output is correct |
41 |
Correct |
28 ms |
3784 KB |
Output is correct |
42 |
Correct |
29 ms |
3912 KB |
Output is correct |
43 |
Correct |
30 ms |
3912 KB |
Output is correct |
44 |
Correct |
28 ms |
3784 KB |
Output is correct |
45 |
Correct |
29 ms |
3900 KB |
Output is correct |
46 |
Correct |
29 ms |
3876 KB |
Output is correct |
47 |
Correct |
27 ms |
3912 KB |
Output is correct |
48 |
Correct |
29 ms |
3908 KB |
Output is correct |
49 |
Correct |
30 ms |
3876 KB |
Output is correct |
50 |
Correct |
33 ms |
3932 KB |
Output is correct |
51 |
Correct |
60 ms |
6184 KB |
Output is correct |
52 |
Correct |
53 ms |
6000 KB |
Output is correct |
53 |
Correct |
54 ms |
6328 KB |
Output is correct |
54 |
Correct |
62 ms |
6004 KB |
Output is correct |
55 |
Correct |
61 ms |
6328 KB |
Output is correct |
56 |
Correct |
666 ms |
56840 KB |
Output is correct |
57 |
Correct |
670 ms |
70676 KB |
Output is correct |
58 |
Correct |
648 ms |
66664 KB |
Output is correct |
59 |
Correct |
666 ms |
68116 KB |
Output is correct |
60 |
Correct |
691 ms |
71892 KB |
Output is correct |