#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 |
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 |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
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 |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
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 |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
2 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 |
51 ms |
3868 KB |
Output is correct |
2 |
Correct |
51 ms |
3776 KB |
Output is correct |
3 |
Correct |
52 ms |
3784 KB |
Output is correct |
4 |
Correct |
47 ms |
3844 KB |
Output is correct |
5 |
Correct |
47 ms |
3776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
90 ms |
6284 KB |
Output is correct |
2 |
Correct |
94 ms |
6352 KB |
Output is correct |
3 |
Correct |
90 ms |
6268 KB |
Output is correct |
4 |
Correct |
91 ms |
6388 KB |
Output is correct |
5 |
Correct |
89 ms |
6140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
6336 KB |
Output is correct |
2 |
Correct |
97 ms |
6212 KB |
Output is correct |
3 |
Correct |
93 ms |
6224 KB |
Output is correct |
4 |
Correct |
99 ms |
6460 KB |
Output is correct |
5 |
Correct |
100 ms |
6340 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 |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
51 ms |
3868 KB |
Output is correct |
12 |
Correct |
51 ms |
3776 KB |
Output is correct |
13 |
Correct |
52 ms |
3784 KB |
Output is correct |
14 |
Correct |
47 ms |
3844 KB |
Output is correct |
15 |
Correct |
47 ms |
3776 KB |
Output is correct |
16 |
Correct |
45 ms |
3852 KB |
Output is correct |
17 |
Correct |
49 ms |
3784 KB |
Output is correct |
18 |
Correct |
51 ms |
3880 KB |
Output is correct |
19 |
Correct |
47 ms |
3844 KB |
Output is correct |
20 |
Correct |
52 ms |
3828 KB |
Output is correct |
21 |
Correct |
45 ms |
3856 KB |
Output is correct |
22 |
Correct |
48 ms |
3852 KB |
Output is correct |
23 |
Correct |
49 ms |
3880 KB |
Output is correct |
24 |
Correct |
47 ms |
3784 KB |
Output is correct |
25 |
Correct |
49 ms |
3860 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 |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
2 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 |
51 ms |
3868 KB |
Output is correct |
27 |
Correct |
51 ms |
3776 KB |
Output is correct |
28 |
Correct |
52 ms |
3784 KB |
Output is correct |
29 |
Correct |
47 ms |
3844 KB |
Output is correct |
30 |
Correct |
47 ms |
3776 KB |
Output is correct |
31 |
Correct |
90 ms |
6284 KB |
Output is correct |
32 |
Correct |
94 ms |
6352 KB |
Output is correct |
33 |
Correct |
90 ms |
6268 KB |
Output is correct |
34 |
Correct |
91 ms |
6388 KB |
Output is correct |
35 |
Correct |
89 ms |
6140 KB |
Output is correct |
36 |
Correct |
101 ms |
6336 KB |
Output is correct |
37 |
Correct |
97 ms |
6212 KB |
Output is correct |
38 |
Correct |
93 ms |
6224 KB |
Output is correct |
39 |
Correct |
99 ms |
6460 KB |
Output is correct |
40 |
Correct |
100 ms |
6340 KB |
Output is correct |
41 |
Correct |
45 ms |
3852 KB |
Output is correct |
42 |
Correct |
49 ms |
3784 KB |
Output is correct |
43 |
Correct |
51 ms |
3880 KB |
Output is correct |
44 |
Correct |
47 ms |
3844 KB |
Output is correct |
45 |
Correct |
52 ms |
3828 KB |
Output is correct |
46 |
Correct |
45 ms |
3856 KB |
Output is correct |
47 |
Correct |
48 ms |
3852 KB |
Output is correct |
48 |
Correct |
49 ms |
3880 KB |
Output is correct |
49 |
Correct |
47 ms |
3784 KB |
Output is correct |
50 |
Correct |
49 ms |
3860 KB |
Output is correct |
51 |
Correct |
101 ms |
6244 KB |
Output is correct |
52 |
Correct |
97 ms |
6024 KB |
Output is correct |
53 |
Correct |
90 ms |
6300 KB |
Output is correct |
54 |
Correct |
91 ms |
5904 KB |
Output is correct |
55 |
Correct |
92 ms |
6272 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 |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
2 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 |
51 ms |
3868 KB |
Output is correct |
27 |
Correct |
51 ms |
3776 KB |
Output is correct |
28 |
Correct |
52 ms |
3784 KB |
Output is correct |
29 |
Correct |
47 ms |
3844 KB |
Output is correct |
30 |
Correct |
47 ms |
3776 KB |
Output is correct |
31 |
Correct |
90 ms |
6284 KB |
Output is correct |
32 |
Correct |
94 ms |
6352 KB |
Output is correct |
33 |
Correct |
90 ms |
6268 KB |
Output is correct |
34 |
Correct |
91 ms |
6388 KB |
Output is correct |
35 |
Correct |
89 ms |
6140 KB |
Output is correct |
36 |
Correct |
101 ms |
6336 KB |
Output is correct |
37 |
Correct |
97 ms |
6212 KB |
Output is correct |
38 |
Correct |
93 ms |
6224 KB |
Output is correct |
39 |
Correct |
99 ms |
6460 KB |
Output is correct |
40 |
Correct |
100 ms |
6340 KB |
Output is correct |
41 |
Correct |
45 ms |
3852 KB |
Output is correct |
42 |
Correct |
49 ms |
3784 KB |
Output is correct |
43 |
Correct |
51 ms |
3880 KB |
Output is correct |
44 |
Correct |
47 ms |
3844 KB |
Output is correct |
45 |
Correct |
52 ms |
3828 KB |
Output is correct |
46 |
Correct |
45 ms |
3856 KB |
Output is correct |
47 |
Correct |
48 ms |
3852 KB |
Output is correct |
48 |
Correct |
49 ms |
3880 KB |
Output is correct |
49 |
Correct |
47 ms |
3784 KB |
Output is correct |
50 |
Correct |
49 ms |
3860 KB |
Output is correct |
51 |
Correct |
101 ms |
6244 KB |
Output is correct |
52 |
Correct |
97 ms |
6024 KB |
Output is correct |
53 |
Correct |
90 ms |
6300 KB |
Output is correct |
54 |
Correct |
91 ms |
5904 KB |
Output is correct |
55 |
Correct |
92 ms |
6272 KB |
Output is correct |
56 |
Execution timed out |
1056 ms |
56880 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |