이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |