Codeforces 961E - Tufurama

E. Tufurama

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

第一反应是主席树:

注意unique的使用

/**求出满足a[i]>=j&&a[j]>=i的有序对< i , j >的对数(i<j)。**/
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn = 200000;
typedef long long int ll;
int n,a[maxn+5],b[maxn+5],th[maxn+5];
int tot,sz,root[maxn+5],ls[maxn*50+5],rs[maxn*50+5],sum[maxn*50+5];
void build(int &v,int l,int r)
{
    v=++tot;
    sum[v] = 0;
    if(l==r) return;
    int mid = (l+r)>>1;
    build(ls[v],l,mid);
    build(rs[v],mid+1,r);
}
void updata(int &v,int last,int l,int r,int q)
{
    v = ++tot;
    ls[v] = ls[last];
    rs[v] = rs[last];
    sum[v] = sum[last]+1;
    if(l==r) return;
    int mid = (l+r)>>1;
    if(q<=mid) updata(ls[v],ls[last],l,mid,q);
    if(q>mid) updata(rs[v],rs[last],mid+1,r,q);
}
int query(int v,int last,int l,int r,int L,int R)
{
    if(r<L||l>R) return 0;
    int mid = (l+r)>>1;
    if(L<=l&&r<=R) return sum[v] - sum[last];
    int cost = 0;
    if(mid>=L) cost+=query(ls[v],ls[last],l,mid,L,R);
    if(mid<R) cost+=query(rs[v],rs[last],mid+1,r,L,R);
    return cost;
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]), b[i] = a[i];
    sort(b+1,b+n+1);//*********
    sz = unique(b + 1, b + n + 1) - (b + 1);// unique返回一个个重复的位置
    for(int i=1;i<=n;i++)
    {
        th[i] = lower_bound(b+1,b+sz+1,a[i]) - b;
    }
    build(root[0],1,sz);
    for(int i=1;i<=n;i++) updata(root[i],root[i-1],1,sz,th[i]);
    ll ans = 0;
    for(int i=1;i<=n;i++)
    {
        int l = i, r = min(a[i],n), q;
        if(l >= r) continue;
        q = lower_bound(b+1,b+sz+1,i) - b;
        ans +=query(root[r],root[l],1,sz,q,sz);
    }
    cout<<ans<<endl;
    return 0;
}

树状数组:

/**
求出满足a[i]>=j&&a[j]>=i的有序对< i , j >的对数(i<j)。
换一个突破口改变就这么大:
对j,满足a[j]>=i,找到最大的i,此时只要在 1~i 中找到a[i] > j的数有多少就行了,
**/
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
using namespace std;
const int maxn = 200000;
typedef long long int ll;
int n,a[maxn+5];
int C[maxn+5];
vector<int>g[maxn+5];
int lowbit(int x){return x&-x;}
int Sum(int x)
{
    int ret = 0;
    while(x>0) ret+=C[x],x-=lowbit(x);
    return ret;
}
void updata(int x,int d)
{
    while(x<=n){
        C[x]+=d;x+=lowbit(x);
    }
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        a[i] = min(n,a[i]);
        g[min(i-1,a[i])].push_back(i);
    }
    ll ans = 0;
    for(int i=1;i<=n;i++)
    {
        updata(a[i],1);
        for(int j=0;j<g[i].size();j++)
        {
            ans+=Sum(n) - Sum(g[i][j]-1);
        }
    }
    cout<<ans<<endl;
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值