codeforces961E、Tufurama (主席树)

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

In the third example:

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

 

一、原题地址

点我传送

 

二、大致题意

给出一个序列a[ ],找出符合 a[ i ]>=j && a[ j ]>= i  ( i < j ) 的( i,j )点对数。

 

三、大致思路

有a[ j ]>= i 所以应该在[ 1 , a[ j ] ]的范围内寻找那些原序列中的数a[ i ],满足a[ i ]>= j 。

那么问题就转化为了一个区间寻找大于某数 j 的个数了。立刻当场学习主席树。还有树状数组的做法,但是主席树学得脑壳有点疼。

 

四、代码

#include <iostream>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
typedef long long LL;
const LL INF=0x3f3f3f3f3f3f3f3f;




const int maxn=2e5+5;
int n;
int rt[maxn],RootNum;
struct TreeNode
{
    int lson,rson;
    LL cnt;
}tree[maxn*35];

void update(int pre,int &rt,int l,int r,int p)      //在[l,r]范围内rt版本时插入一个p
{
    rt=++RootNum;
    tree[rt].cnt=tree[pre].cnt+(LL)1;
    tree[rt].lson=tree[pre].lson;
    tree[rt].rson=tree[pre].rson;
    if(l==r) return;
    int mid=(l+r)>>1;
    if(p<=mid) update(tree[pre].lson,tree[rt].lson,l,mid,p);
    else update(tree[pre].rson,tree[rt].rson,mid+1,r,p);
}

LL query(int rt,int L,int R,int ql,int qr)
{
    if(L>=ql&&qr>=R)return tree[rt].cnt;

//    printf("%d %d\n",L,R);
//    printf("%d %d\n",tree[rt].l,tree[rt].r);
    int mid=(L+R)>>1;
    if (qr<=mid) return query(tree[rt].lson,L,mid,ql,qr);
    else if (ql>mid) return query(tree[rt].rson,mid+1,R,ql,qr);
    else return query(tree[rt].lson,L,mid,ql,qr)+query(tree[rt].rson,mid+1,R,ql,qr);
}

int a[maxn];
int main()
{
    scanf("%d",&n);
    RootNum=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        if(a[i]>200000)a[i]=200000;
        update(rt[i-1],rt[i],1,200000,a[i]);
    }
    LL ans=0;
    for(int i=1;i<=n;i++)
    {
        if(a[i]>=i) ans+=query(rt[  min(a[i],i) -1],   1,200000, i,200000);
        else if(a[i]<i)ans+=query(rt[  min(a[i],i) ],   1,200000, i,200000);
    }
    printf("%lld\n",ans);

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值