This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
long long ans = 0;
const long long MXN = 1000005;
long long par[MXN], gs[MXN];
bool used[MXN];
void init() {
for (long long i=0; i<MXN; i++) par[i] = i;
}
long long parent(long long n) {
if (n == par[n]) return n;
return par[n] = parent(par[n]);
}
bool arc(long long a, long long b) {
return parent(a) == parent(b);
}
long long pa(long long n) {
return n * (n+1) / 2;
}
void connect(long long a, long long b) {
if (arc(a, b)) return;
long long ax = parent(a);
long long bx = parent(b);
long long oa = gs[ax];
long long ob = gs[bx];
ans -= pa(oa);
ans -= pa(ob);
ans += pa(oa+ob);
gs[ax] += gs[bx];
par[bx] = ax;
}
void prop(long long n) {
if (used[n-1] && used[n]) {
connect(n-1, n);
}
if (used[n] && used[n+1]) {
connect(n, n+1);
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
long long n, q;
cin >> n >> q;
vector<pair<long long, long long> > ar(n);
for (long long i=0; i<n; i++) {
long long x;
cin >> x;
ar[i] = make_pair(x, i+1);
}
vector<pair<long long, long long> > qs;
for (long long i=0; i<q; i++) {
long long x;
cin >> x;
qs.push_back(make_pair(x, i));
}
memset(used, 0, sizeof used);
sort(ar.begin(), ar.end());
sort(qs.begin(), qs.end());
long long nxt = 0;
long long res[q];
init();
for (long long i=0; i<q; i++) {
long long qx = qs[i].first;
long long pos = qs[i].second;
while (nxt < n && ar[nxt].first <= qx) {
ans++;
used[ar[nxt].second] = true;
gs[ar[nxt].second] = 1;
prop(ar[nxt].second);
nxt++;
}
res[pos] = ans;
}
for (long long i=0; i<q; i++) cout << res[i] << '\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... |