【CF961E】Tufurama

题目

题目描述
有一天Polycarp决定重看他最喜爱的电视剧《Tufurama》。当他搜索“在线全高清免费观看Tufurama第3季第7集”却只得到第7季第3集的结果时,他很惊讶。这让Polycarp感到疑惑——如果有天他决定重看整个系列却无法找到正确的剧集观看,那该怎么办呢?Polycarp现在想统计一下他被迫用不同方案搜索同一剧集的次数。

电视连续剧有nn 季(从11 到nn 编号),第ii 季有a_ia
i
​ 集(从11 到a_ia
i
​ 编号)。Polycarp认为如果有一对xx 和yy (x<yx<y ),使第xx 季第yy 集、第yy 季第xx 集存在,那么其中一个搜索就会包含错误的内容。请帮助Polycarp统计这样的数对的数量吧!

输入输出格式
输入格式
第一行,一个整数n(1 \leq n \leq 2 \cdot 10^5)n(1≤n≤2⋅10
5
) ,表示季数。

第二行,nn 个用空格隔开的整数a_1, a_2, … , a_n (1 \leq a_i \leq 10^9)a
1
​ ,a
2
​ ,…,a
n
​ (1≤a
i
​ ≤10
9
) ,表示每一季的集数。

输出格式
只有一行,一个整数,表示xx 和yy (x<yx<y ),使第xx 季第yy 集、第yy 季第xx 集存在的数对的数量。

说明
在样例2中可能的对数:

x=1, y=2x=1,y=2 (第1季第2集第2季第1集)
x=2, y=3x=2,y=3 (第2季第3集第3季第2集)
x=1, y=3x=1,y=3 (第1季第3集第3季第1集)
在样例3中:

x=1, y=2x=1,y=2 (第1季第2集第2季第1集)
x=1, y=3x=1,y=3 (第1季第3集第3季第1集)
感谢@月见之兔 提供的翻译

题目描述
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 n seasons (numbered 1 1 through n n ), the i i -th season has a_{i} a
i
​ episodes (numbered 1 1 through a_{i} a
i
​ ). Polycarp thinks that if for some pair of integers x x and y y ( x<y x<y ) exist both season x x episode y y and season y y episode x x then one of these search queries will include the wrong results. Help Polycarp to calculate the number of such pairs!

输入输出格式
输入格式:
The first line contains one integer n n (1<=n<=2·10^{5}) (1<=n<=2⋅10
5
) — the number of seasons.

The second line contains n n integers separated by space a_{1},a_{2},…,a_{n} a
1
​ ,a
2
​ ,…,a
n
​ (1<=a_{i}<=10^{9}) (1<=a
i
​ <=10
9
) — number of episodes in each season.

输出格式:
Print one integer — the number of pairs x x and y y ( x<y x<y ) such that there exist both season x x episode y y and season y y episode x x .

输入输出样例
输入样例#1: 复制
5
1 2 3 4 5
输出样例#1: 复制
0
输入样例#2: 复制
3
8 12 7
输出样例#2: 复制
3
输入样例#3: 复制
3
3 2 1
输出样例#3: 复制
2
说明
Possible pairs in the second example:

x=1 x=1 , y=2 y=2 (season 1 episode 2 season 2 episode 1);
x=2 x=2 , y=3 y=3 (season 2 episode 3 season 3 episode 2);
x=1 x=1 , y=3 y=3 (season 1 episode 3 season 3 episode 1).
In the third example:

x=1 x=1 , y=2 y=2 (season 1 episode 2 season 2 episode 1);
x=1 x=1 , y=3 y=3 (season 1 episode 3 season 3 episode 1).

思路

这题树状数组随便搞搞就过了

代码

#include<bits/stdc++.h>
using namespace std;
long long ans;
int n,now,a[200035],f[200035];
struct node
{
    int a,i;
}p[200035];
bool cmp(node x,node y)
{
	return x.a<y.a;
}
int lowbit(int x)
{
	return x&-x;
}
void ins(int x, int c)
{
	for(; x<=n+1; x+=lowbit(x)) f[x]+=c;
}
int query(int x)
{
    int ret = 0;
    for(; x; x-=lowbit(x))
        ret += f[x];
    return ret;
}
int main()
{
    scanf("%d",&n);
	now=1;
    for(int i=1; i<=n; i++)
        scanf("%d",&a[i]),a[i]=min(a[i],n+1),p[i].a=a[i],p[i].i=i,ins(i,1);
    sort(p+1,p+n+1,cmp);
    for(int i=1; i<=n; i++)
    {
        while(now<=n && p[now].a < i)ins(p[now++].i, -1);
        ans += query(a[i]);
        if (a[i] >= i)ans--;
    }
    cout << ans / 2 << endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值