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
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).

思路:用主席树统计[i+1,a[i]]中有多少个数大于等于i,累计求和就是答案。

#include<bits/stdc++.h>
using namespace std;
const int MAX=2e5+10;
const int MOD=1e9+7;
const double PI=acos(-1.0);
typedef long long ll;
vector<int>v;
struct lenka
{
    int L,R,sum;
}A[MAX*40];
int tot,root[MAX],a[MAX];
void init()
{
    tot=1;
    root[0]=0;
    A[0].L=A[0].R=A[0].sum=0;
}
void build(int x,int&rt,int l,int r)
{
    A[tot++]=A[rt];
    rt=tot-1;
    A[rt].sum++;
    if(l==r)return;
    if(x<=(l+r)/2)build(x,A[rt].L,l,(l+r)/2);
    else build(x,A[rt].R,(l+r)/2+1,r);
}
int ask(int l,int r,int x,int y,int k)
{
    if(l==r)return A[y].sum-A[x].sum;
    if(k>(l+r)/2)return ask((l+r)/2+1,r,A[x].R,A[y].R,k);
    return A[A[y].R].sum-A[A[x].R].sum+ask(l,(l+r)/2,A[x].L,A[y].L,k);
}
int getid(int x){return lower_bound(v.begin(),v.end(),x)-v.begin()+1;}
int main()
{
    int n;
    cin>>n;
    init();
    for(int i=1;i<=n;i++)scanf("%d",&a[i]),v.push_back(a[i]);
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
    for(int i=1;i<=n;i++)
    {
        root[i]=root[i-1];
        build(getid(a[i]),root[i],1,n);
    }
    ll ans=0;
    for(int i=1;i<=n;i++)
    {
        if(min(a[i],n)>=i)ans+=ask(1,n,root[i],root[min(n,a[i])],getid(i));
    }
    cout<<ans<<endl;
    return 0;
}


猜你喜欢

转载自blog.csdn.net/mitsuha_/article/details/80428226