http://codeforces.com/problemset/problem/961/E
一看这种题就知道用树状数组
一开始推错了
后来才发现一开始的算法有问题
其实不是太难吧
不太明白为什么是E题
需要一点点思维
有一点树状数组求逆序对的感觉
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n;
const ll N=4e5+10;
ll c[N];
inline void read(ll &x){char ch;bool ok;
for(ok=0,ch=getchar();!isdigit(ch);ch=getchar()) if(ch=='-') ok=1;
for(x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());if(ok) x=-x;}
ll lowbit(ll i){return i&(-i);}
void add(ll i,ll value){while(i<=N){c[i]+=value;i+=lowbit(i);}}
ll sum(ll i){ll sum=0;while(i>0){sum+=c[i];i-=lowbit(i);}return sum;}
struct P{
ll x,y;
}A[N];
bool cmp(P a,P b){
if(a.x==b.x) return a.y>b.y;
return a.x>b.x;
}
int main(){
cin>>n;
for(ll i=1;i<=n;++i){
read(A[i].x);
if(A[i].x>n) A[i].x=n;
A[i].y=i;
A[i+n].x=i;
A[i+n].y=-A[i].x;
}
sort(A+1,A+1+2*n,cmp);
ll ans=0;
for(ll i=1;i<=n*2;++i){
// cout<<A[i].x<<" "<<A[i].y<<endl;
if(A[i].y<0){
// cout<<sum(min(A[i].x,-A[i].y))<<endl;
ans+=sum(min(A[i].x,-A[i].y));
if(A[i].x<=-A[i].y) --ans;
}
else{
add(A[i].y,1);
}
}
cout<<ans<<endl;
}