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;
}

引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值