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 vi vector<int>
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
using namespace std;
const int N = 2e5+5;
int MX[4*N],sum[4*N];
vector<int> v,v1;
void build(int v,int tl,int tr) {
if (tl == tr) {
MX[v] = v1[tl];
sum[v] = 1;
return;
}
int tm = (tl + tr)/2;
build(v*2,tl,tm);
build(v*2+1,tm+1,tr);
sum[v] = sum[v*2] + sum[v*2+1];
MX[v] = max(MX[v*2],MX[v*2+1]);
}
long long ans;
int idx;
void upd(int v,int tl,int tr,int val) {
if (tl == tr) {
sum[v] = 0;
MX[v] = -1;
idx = tl;
return;
}
int tm = (tl + tr)/2;
if (MX[v*2+1] >= val) {
upd(v*2+1,tm+1,tr,val);
}else
upd(v*2,tl,tm,val);
sum[v] = sum[v*2] + sum[v*2+1];
MX[v] = max(MX[v*2],MX[v*2+1]);
}
int get(int v,int tl,int tr,int pos) {
if (pos > tr) return 0;
if (pos <= tl) return sum[v];
int tm = (tl + tr)/2;
return get(v*2,tl,tm,pos) +
get(v*2+1,tm+1,tr,pos);
}
main() {
int n;
cin >> n;
v.resize(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
--v[i];
}
vi g = v;
v1 = v;
sort(g.begin(),g.end());
// INVALID
for (int i = 0; i < n; i++)
if (g[i] < i) {
cout <<-1;
return 0;
}//
build(1,0,n-1);
for (int i = n-1; i >= 0; i--) {
upd(1,0,n-1,i);
ans += get(1,0,n-1,idx);
}
cout << ans;
return 0;
}
Compilation message (stderr)
Main.cpp:49:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
49 | main() {
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |