Educational Codeforces Round 41 (Rated for Div. 2) E. 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 = 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[x]>=y&&a[y]>=x&&x<y的对数。

用c[i]记录大于等于(等于i的元素的下标)的个数。

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<math.h>
using namespace std;
#define MAXN 200005
int n,t[MAXN],c[MAXN];
int p[MAXN];
vector<int> vec[MAXN];
int lowbit(int i)
{
    return i&(-i);
}
void add(int i,int x)
{
    while(i<=n)
    {
        c[i]+=x;
        i+=lowbit(i);
    }
}
int sum(int i)
{
    int res=0;
    while(i)
    {
        res+=c[i];
        i-=lowbit(i);
    }
    return res;
}
int main()
{
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
        vec[i].clear();
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&p[i]);
        if(p[i]>n) vec[n].push_back(i);
        else vec[p[i]].push_back(i);//存储等于p[i]的元素下标
    }
    long long ans=0;
    memset(c,0,sizeof c);
    for(int i=n; i>=1; i--)//从大到小枚举i
    {
        for(int j=0; j<vec[i].size(); j++)//将等于i的元素添加进去
            add(vec[i][j],1);
        ans+=sum(min(i-1,p[i]));//查询下标小于i(在i之前),且小于等于p[i](与第i个点可以互相到达)的个数
    }
    printf("%lld\n",ans);

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值