Codeforces-961E-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
0
Input
Copy
3
8 12 7
Output
3
Input
Copy
3
3 2 1
Output
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).

题意:简单讲就是再给出的n个数里面,对于每个数ai,求在区间[i+1,ai]中大于等于i的数有多少个,然后求和;

思路:把原数组离散化,主席树维护[1,i]每个点出现的次数,然后查询(1,ai)-(1,i)即可。模板题。

#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;
int n,arr[maxn],brr[maxn];
struct node
{
    int l,r,ls,rs;
    long long cnt;
}tree[maxn*40];
int rt[maxn*40],cnt;
void build(int &rt,int l,int r)
{
    rt=++cnt;
    tree[rt].l=l;
    tree[rt].r=r;
    tree[rt].cnt=0;
    if(l==r) return;
    int mid=(l+r)>>1;
    build(tree[rt].ls,l,mid);
    build(tree[rt].rs,mid+1,r);
}
void update(int pre,int &rt,int p)
{
    rt=++cnt;
    tree[rt].cnt=tree[pre].cnt+(long long)1;
    tree[rt].l=tree[pre].l;
    tree[rt].r=tree[pre].r;
    tree[rt].ls=tree[pre].ls;
    tree[rt].rs=tree[pre].rs;
    if(tree[rt].l==tree[rt].r) return;
    int mid=(tree[rt].l+tree[rt].r)>>1;
    if(p<=mid) update(tree[pre].ls,tree[rt].ls,p);
    else update(tree[pre].rs,tree[rt].rs,p);
}
long long query(int pre,int rt,int p)
{
    int ll=tree[rt].l;
    int rr=tree[rt].r;
    if(ll==rr) return tree[rt].cnt-tree[pre].cnt;
    int mid=(ll+rr)>>1;
    if(p<=mid) return query(tree[pre].ls,tree[rt].ls,p)+tree[tree[rt].rs].cnt-tree[tree[pre].rs].cnt;
    else return query(tree[pre].rs,tree[rt].rs,p);
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&arr[i]),brr[i]=arr[i];
    sort(brr+1,brr+1+n);
    int sz=unique(brr+1,brr+1+n)-brr-1;
    cnt=0;
    build(rt[0],1,n);
    for(int i=1;i<=n;i++){
        int p=lower_bound(brr+1,brr+1+sz,arr[i])-brr;
        update(rt[i-1],rt[i],p);
    }
    long long ans=0;
    for(int i=1;i<=n;i++){
        if(i+1>arr[i]) continue;
        int pos=lower_bound(brr+1,brr+1+sz,i)-brr;
        ans+=query(rt[i],rt[min(n,arr[i])],pos);
    }
    printf("%lld\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37171272/article/details/79835524