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>
#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL)
#define file(a) freopen(a".inp","r",stdin); freopen(a".out", "w",stdout)
#define fi first
#define se second
#define endl "\n"
#define len(s) int32_t(s.length())
#define MASK(k)(1LL<<(k))
#define TASK "test"
using namespace std;
typedef tuple<int, int, int> tpiii;
typedef pair<double, double> pdd;
typedef pair<int, int> pii;
typedef long long ll;
const long long OO = 1e18;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const int MX = 1000005;
struct DSU{
int n;
vector<int> e;
DSU(int n=0){
this->n = n;
this->e.assign(n+5, 0);
}
int fp(int u){
return e[u] < 0? u : e[u] = fp(e[u]);
}
void unon(int u, int v){
u = fp(u);
v = fp(v);
if(u == v) return;
if(e[u] > e[v]) swap(u, v);
e[u] += e[v];
e[v] = u;
}
int cost(int u){
u = fp(u);
return e[u]*(e[u]-1)/2;
}
};
int n, q;
int res[MX];
pii a[MX];
pii b[MX];
void nhap(){
cin >> n >> q;
for(int i=1; i<=n; i++) cin >> a[i].fi, a[i].se = i;
for(int i=1; i<=q; i++) cin >> b[i].fi, b[i].se = i;
}
void solve(){
sort(a+1, a+1+n);
sort(b+1, b+1+q);
DSU dsu(n);
int ans = 0;
for(int i=1, j=1; j<=q; j++){
while(i<=n && a[i].fi <= b[j].fi){
dsu.e[a[i].se] = -1;
if(dsu.e[a[i].se-1]) ans -= dsu.cost(a[i].se - 1), dsu.unon(a[i].se, a[i].se - 1);
if(dsu.e[a[i].se+1]) ans -= dsu.cost(a[i].se + 1), dsu.unon(a[i].se, a[i].se + 1);
ans += dsu.cost(a[i].se);
i++;
}
res[b[j].se] = ans;
}
for(int i=1; i<=q; i++) cout << res[i] << endl;
}
int32_t main(){
fastIO;
//file(TASK);
nhap();
solve();
return 0;
}
# | 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... |