codeforce 961E

E. Tufurama
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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!

Input

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.

Output

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.

Examples
input
Copy
5
1 2 3 4 5
output
Copy
0
input
Copy
3
8 12 7
output
Copy
3
input
Copy
3
3 2 1
output
Copy
2
Note

Possible pairs in the second example:

  1. x = 1y = 2 (season 1 episode 2  season 2 episode 1);
  2. x = 2y = 3 (season 2 episode 3  season 3 episode 2);
  3. x = 1y = 3 (season 1 episode 3  season 3 episode 1).

In the third example:

  1. x = 1y = 2 (season 1 episode 2  season 2 episode 1);
  2. x = 1y = 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值