One day Polycarp decided to rewatch his absolute favourite episode of well-known TV series "Tufurama". He was pretty surprised when he got results only for season 7 episode 3 with his search query of "Watch Tufurama season 3 episode 7 online full hd free". This got Polycarp confused — what if he decides to rewatch the entire series someday and won't be able to find the right episodes to watch? Polycarp now wants to count the number of times he will be forced to search for an episode using some different method.
TV series have n seasons (numbered 1 through n), the i-th season has ai episodes (numbered 1 through ai). Polycarp thinks that if for some pair of integers x and y (x < y) exist both season x episode y and season y episode x then one of these search queries will include the wrong results. Help Polycarp to calculate the number of such pairs!
The first line contains one integer n (1 ≤ n ≤ 2·105) — the number of seasons.
The second line contains n integers separated by space a1, a2, ..., an (1 ≤ ai ≤ 109) — number of episodes in each season.
Print one integer — the number of pairs x and y (x < y) such that there exist both season x episode y and season y episode x.
5 1 2 3 4 5
0
3 8 12 7
3
3 3 2 1
2
Possible pairs in the second example:
- x = 1, y = 2 (season 1 episode 2
season 2 episode 1);
- x = 2, y = 3 (season 2 episode 3
season 3 episode 2);
- x = 1, y = 3 (season 1 episode 3
season 3 episode 1).
In the third example:
- x = 1, y = 2 (season 1 episode 2
season 2 episode 1);
- x = 1, y = 3 (season 1 episode 3
season 3 episode 1).
求出满足a[i]>=j&&a[j]>=i的有序对< i , j >的对数(i<j)。 树状数组维护当前j位置前有多少个i位置数不满足a[i]>=j 。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define x0 x0___
#define y0 y0___
#define pb push_back
#define SZ(X) ((int)X.size())
#define mp make_pair
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pli pair<ll,int>
#define pil pair<int,ll>
#define ALL(X) X.begin(),X.end()
#define RALL(X) X.rbegin(),X.rend()
#define rep(i,j,k) for(int i = j;i <= k;i ++)
#define per(i,j,k) for(int i = j;i >= k;i --)
#define mem(a,p) memset(a,p,sizeof(a))
const ll MOD = 1E9 + 7;
ll qmod(ll a,ll b,ll c) {ll res=1;a%=c; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%c;a=a*a%c;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
template<typename T, typename S>
void upmax(T& a,S b){if(a<b) a=b;}
template<typename T, typename S>
void upmin(T& a,S b){if(a>b) a=b;}
template<typename T>
void W(T b){cout << b << endl;}
void gettle() {while(1);}
void getre() {int t=0;t/=t;}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const int N = 2E5 + 7;
int n;
int f[N];
int id[N], a[N];
void add(int x)
{
for(;x<=n;x+=x&-x){
f[x] ++;
}
}
int ask(int x)
{
int s = 0;
for(;x;x-=x&-x){
s += f[x];
}
return s;
}
int main()
{
scanf("%d", &n);
rep (i,1,n) {
scanf("%d", &a[i]);
id[i] = i;
upmin(a[i], n);
}
sort(id+1,id+1+n,[](const int &A,const int&B){return a[A] < a[B];});
int p = 0;
ll res = 0;
rep (i,2,n) {
int j = min(i-1, a[i]);
for(;p + 1 <= n && a[id[p+1]] < i;){ //若当前位置小于i,那么显然他对后面的贡献为0。
p++;
add(id[p]);
}
res += j - ask(j);
}
printf("%lld\n", res);
return 0;
}