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 season 2 episode 1);

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

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

In the third example:

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

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






发布了259 篇原创文章 · 获赞 100 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/my_sunshine26/article/details/79831362