#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
const int N = 1e6 + 5;
pii a[N];
int b[N],par[N],sz[N],ans;
bool f[N];
void make_set(int v) {
par[v] = v;
sz[v] = 1;
}
int find_set(int v) {
if (v == par[v]) return v;
int p = find_set(par[v]);
par[v] = p;
return p;
}
void union_sets(int a,int b) {
a = find_set(a),b = find_set(b);
if (a == b) return;
ans -= sz[a] * (sz[a] - 1) / 2 + sz[b] * (sz[b] - 1) / 2;
if (sz[a] < sz[b]) swap(a,b);
sz[a] += sz[b];
par[b] = a;
ans += sz[a] * (sz[a] - 1) / 2;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n,q;
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> a[i].fi;
a[i].se = i;
}
sort(a + 1,a + n + 1);
for (int i = 1; i <= q; i++) cin >> b[i];
sort(b + 1,b + q + 1);
int j = 1;
for (int i = 1; i <= n; i++) make_set(i);
for (int i = 1; i <= q; i++) {
while (j <= n && a[j].fi <= b[i]) {
ans++;
if (a[j].se > 1 && f[a[j].se - 1]) union_sets(a[j].se,a[j].se - 1);
if (a[j].se < n && f[a[j].se + 1]) union_sets(a[j].se,a[j].se + 1);
f[a[j].se] = true;
j++;
}
cout << ans << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Incorrect |
1 ms |
8540 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Incorrect |
1 ms |
8540 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Incorrect |
1 ms |
8540 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
10588 KB |
Output is correct |
2 |
Correct |
16 ms |
10720 KB |
Output is correct |
3 |
Correct |
16 ms |
10584 KB |
Output is correct |
4 |
Correct |
14 ms |
10712 KB |
Output is correct |
5 |
Correct |
15 ms |
10712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
25 ms |
11612 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
26 ms |
11556 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Correct |
16 ms |
10588 KB |
Output is correct |
12 |
Correct |
16 ms |
10720 KB |
Output is correct |
13 |
Correct |
16 ms |
10584 KB |
Output is correct |
14 |
Correct |
14 ms |
10712 KB |
Output is correct |
15 |
Correct |
15 ms |
10712 KB |
Output is correct |
16 |
Correct |
17 ms |
10588 KB |
Output is correct |
17 |
Correct |
17 ms |
10708 KB |
Output is correct |
18 |
Correct |
14 ms |
10588 KB |
Output is correct |
19 |
Correct |
13 ms |
10588 KB |
Output is correct |
20 |
Correct |
14 ms |
10588 KB |
Output is correct |
21 |
Correct |
15 ms |
10708 KB |
Output is correct |
22 |
Correct |
13 ms |
10588 KB |
Output is correct |
23 |
Correct |
14 ms |
10588 KB |
Output is correct |
24 |
Correct |
13 ms |
10588 KB |
Output is correct |
25 |
Correct |
14 ms |
10708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Incorrect |
1 ms |
8540 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Incorrect |
1 ms |
8540 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |