Codeforces 961E - Tufurama 【树状数组】


E. Tufurama

time limit per test   2 seconds

memory limit per test      256 megabytes

One day Polycarpdecided to rewatch his absolute favourite episode of well-known TV series"Tufurama". He was pretty surprised when he got results only forseason 7 episode 3 with his search query of "Watch Tufurama season 3episode 7 online full hd free". This got Polycarp confused — what if hedecides to rewatch the entire series someday and won't be able to find theright episodes to watch? Polycarp now wants to count the number of times hewill 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 ofintegers 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. HelpPolycarp to calculate the number of such pairs!

Input

The first linecontains one integer n (1  ≤ n  ≤  2·105) — the number of seasons.

The second linecontains 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

5
1 2 3 4 5

Output

0

Input

3
8 12 7

Output

3

Input

3
3 2 1

Output

2

Note

Possible pairsin the second example:

1.x = 1, y = 2 (season 1episode 2 http://codeforces.com/predownloaded/6d/68/6d688d2e34ee08e84e8960c09f780884a8c46a28.pngseason 2 episode 1);

2.x = 2, y = 3 (season 2episode 3 http://codeforces.com/predownloaded/6d/68/6d688d2e34ee08e84e8960c09f780884a8c46a28.pngseason 3 episode 2);

3.x = 1, y = 3 (season 1episode 3 http://codeforces.com/predownloaded/6d/68/6d688d2e34ee08e84e8960c09f780884a8c46a28.pngseason 3 episode 1).

In the third example:

1.x = 1, y = 2 (season 1episode 2 http://codeforces.com/predownloaded/6d/68/6d688d2e34ee08e84e8960c09f780884a8c46a28.pngseason 2 episode 1);

2.x = 1, y = 3 (season 1episode 3 http://codeforces.com/predownloaded/6d/68/6d688d2e34ee08e84e8960c09f780884a8c46a28.pngseason 3 episode 1).

 


【题目链接】 link


【题意】

求出满足a[i]>=j&&a[j]>=i的有序对< i , j >的对数(i<j)。


【思路】

先记录满足i<=a[j]的最大下标idx(idx<j),然后vec[idx].push_back(j)

这保证了第二个条件。

然后依次在树状数组中插入a[i],并查询满足a[i]>=j的j的个数。

(为了利用树状数组,需要把a[i]压缩,因为大于n的a[i]等同于n)

#include <cstdio>
#include <ctime>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)

typedef long long ll;
const int maxn = 200005;
const ll mod = 1e9+7;
const ll INF = 1e18;
const double eps = 1e-6;

int n;
ll a[maxn];
vector<int>vec[maxn];
ll tree[maxn];

ll lowbit(ll x)
{
    return x&(-x);
}

ll query(int pos)
{
    ll ans=0;
    while(pos)
    {
        ans+=tree[pos];
        pos-=lowbit(pos);
    }
    return ans;
}

void update(int pos,ll val)
{
    while(pos<maxn)
    {
        tree[pos]+=val;
        pos+=lowbit(pos);
    }
}

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
        a[i]=min(a[i],(ll)n);
        vec[min((ll)i-1,a[i])].push_back(i);
    }
    ll ans=0;
    for(int i=1;i<=n;i++)
    {
        update(a[i],1);
        for(auto it:vec[i])
        {
            ans+=query(n)-query(it-1); //[it,n]范围内的个数
        }
    }
    printf("%lld\n",ans);
}






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值