#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<ll int> p, s;
void init(ll int n) {
p = vector<ll int>(n);
s = vector<ll int>(n, 1);
iota(all(p), 0);
}
ll int get(ll int x) {
return p[x] = (p[x] == x ? x : get(p[x]));
}
void unite(ll int a, ll 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 += s[b] * s[a];
s[b] += s[a];
}
};
void solve() {
ll int n; cin >> n;
ll int q; cin >> q;
vector<ll int> arr(n); for (ll int i = 0; i < n; i++) cin >> arr[i];
vector<pair<ll int, pair<ll int, ll int>>> edges;
for (ll int i = 1; i < n; i++) {
edges.emplace_back(max(arr[i], arr[i - 1]), make_pair(i, i - 1));
}
for (ll int i = 0; i < n; i++) {
edges.emplace_back(arr[i], make_pair(i, i));
}
sort(all(edges));
vector<pair<ll int, ll int>> qq;
for (ll int i = 0; i < q; i++) {
ll int a; cin >> a;
qq.emplace_back(a, i);
}
// cout << "Hello" << endl;
sort(all(qq));
vector<ll int> ans(q);
ll int ptr = 0;
DSU dsu;
dsu.init(n);
for (ll int i = 0; i < q; i++) {
ll 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);
ll 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 |
1 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 |
1 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 |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 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 |
1 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 |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 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 |
1 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 |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
7316 KB |
Output is correct |
2 |
Correct |
54 ms |
7948 KB |
Output is correct |
3 |
Correct |
53 ms |
7868 KB |
Output is correct |
4 |
Correct |
50 ms |
7884 KB |
Output is correct |
5 |
Correct |
52 ms |
7740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
10544 KB |
Output is correct |
2 |
Correct |
95 ms |
11720 KB |
Output is correct |
3 |
Correct |
107 ms |
11592 KB |
Output is correct |
4 |
Correct |
97 ms |
11952 KB |
Output is correct |
5 |
Correct |
93 ms |
11540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
97 ms |
10668 KB |
Output is correct |
2 |
Correct |
108 ms |
11844 KB |
Output is correct |
3 |
Correct |
98 ms |
11764 KB |
Output is correct |
4 |
Correct |
102 ms |
12156 KB |
Output is correct |
5 |
Correct |
99 ms |
12076 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 |
1 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 |
58 ms |
7316 KB |
Output is correct |
12 |
Correct |
54 ms |
7948 KB |
Output is correct |
13 |
Correct |
53 ms |
7868 KB |
Output is correct |
14 |
Correct |
50 ms |
7884 KB |
Output is correct |
15 |
Correct |
52 ms |
7740 KB |
Output is correct |
16 |
Correct |
51 ms |
7748 KB |
Output is correct |
17 |
Correct |
52 ms |
7956 KB |
Output is correct |
18 |
Correct |
54 ms |
7952 KB |
Output is correct |
19 |
Correct |
47 ms |
7740 KB |
Output is correct |
20 |
Correct |
50 ms |
7936 KB |
Output is correct |
21 |
Correct |
50 ms |
7844 KB |
Output is correct |
22 |
Correct |
50 ms |
7896 KB |
Output is correct |
23 |
Correct |
52 ms |
7868 KB |
Output is correct |
24 |
Correct |
48 ms |
7840 KB |
Output is correct |
25 |
Correct |
50 ms |
7800 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 |
1 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 |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
58 ms |
7316 KB |
Output is correct |
27 |
Correct |
54 ms |
7948 KB |
Output is correct |
28 |
Correct |
53 ms |
7868 KB |
Output is correct |
29 |
Correct |
50 ms |
7884 KB |
Output is correct |
30 |
Correct |
52 ms |
7740 KB |
Output is correct |
31 |
Correct |
92 ms |
10544 KB |
Output is correct |
32 |
Correct |
95 ms |
11720 KB |
Output is correct |
33 |
Correct |
107 ms |
11592 KB |
Output is correct |
34 |
Correct |
97 ms |
11952 KB |
Output is correct |
35 |
Correct |
93 ms |
11540 KB |
Output is correct |
36 |
Correct |
97 ms |
10668 KB |
Output is correct |
37 |
Correct |
108 ms |
11844 KB |
Output is correct |
38 |
Correct |
98 ms |
11764 KB |
Output is correct |
39 |
Correct |
102 ms |
12156 KB |
Output is correct |
40 |
Correct |
99 ms |
12076 KB |
Output is correct |
41 |
Correct |
51 ms |
7748 KB |
Output is correct |
42 |
Correct |
52 ms |
7956 KB |
Output is correct |
43 |
Correct |
54 ms |
7952 KB |
Output is correct |
44 |
Correct |
47 ms |
7740 KB |
Output is correct |
45 |
Correct |
50 ms |
7936 KB |
Output is correct |
46 |
Correct |
50 ms |
7844 KB |
Output is correct |
47 |
Correct |
50 ms |
7896 KB |
Output is correct |
48 |
Correct |
52 ms |
7868 KB |
Output is correct |
49 |
Correct |
48 ms |
7840 KB |
Output is correct |
50 |
Correct |
50 ms |
7800 KB |
Output is correct |
51 |
Correct |
97 ms |
11908 KB |
Output is correct |
52 |
Correct |
100 ms |
11696 KB |
Output is correct |
53 |
Correct |
97 ms |
11912 KB |
Output is correct |
54 |
Correct |
96 ms |
11652 KB |
Output is correct |
55 |
Correct |
98 ms |
11812 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 |
1 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 |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
58 ms |
7316 KB |
Output is correct |
27 |
Correct |
54 ms |
7948 KB |
Output is correct |
28 |
Correct |
53 ms |
7868 KB |
Output is correct |
29 |
Correct |
50 ms |
7884 KB |
Output is correct |
30 |
Correct |
52 ms |
7740 KB |
Output is correct |
31 |
Correct |
92 ms |
10544 KB |
Output is correct |
32 |
Correct |
95 ms |
11720 KB |
Output is correct |
33 |
Correct |
107 ms |
11592 KB |
Output is correct |
34 |
Correct |
97 ms |
11952 KB |
Output is correct |
35 |
Correct |
93 ms |
11540 KB |
Output is correct |
36 |
Correct |
97 ms |
10668 KB |
Output is correct |
37 |
Correct |
108 ms |
11844 KB |
Output is correct |
38 |
Correct |
98 ms |
11764 KB |
Output is correct |
39 |
Correct |
102 ms |
12156 KB |
Output is correct |
40 |
Correct |
99 ms |
12076 KB |
Output is correct |
41 |
Correct |
51 ms |
7748 KB |
Output is correct |
42 |
Correct |
52 ms |
7956 KB |
Output is correct |
43 |
Correct |
54 ms |
7952 KB |
Output is correct |
44 |
Correct |
47 ms |
7740 KB |
Output is correct |
45 |
Correct |
50 ms |
7936 KB |
Output is correct |
46 |
Correct |
50 ms |
7844 KB |
Output is correct |
47 |
Correct |
50 ms |
7896 KB |
Output is correct |
48 |
Correct |
52 ms |
7868 KB |
Output is correct |
49 |
Correct |
48 ms |
7840 KB |
Output is correct |
50 |
Correct |
50 ms |
7800 KB |
Output is correct |
51 |
Correct |
97 ms |
11908 KB |
Output is correct |
52 |
Correct |
100 ms |
11696 KB |
Output is correct |
53 |
Correct |
97 ms |
11912 KB |
Output is correct |
54 |
Correct |
96 ms |
11652 KB |
Output is correct |
55 |
Correct |
98 ms |
11812 KB |
Output is correct |
56 |
Execution timed out |
1074 ms |
111544 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |