我是靠谱客的博主 苗条白云,最近开发中收集的这篇文章主要介绍Pair of Topics CodeForces - 1324D(逆序+树状数组),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

The next lecture in a high school requires two topics to be discussed. The i-th topic is interesting by ai units for the teacher and by bi units for the students.

The pair of topics i and j (i<j) is called good if ai+aj>bi+bj (i.e. it is more interesting for the teacher).

Your task is to find the number of good pairs of topics.

Input
The first line of the input contains one integer n (2≤n≤2⋅105) — the number of topics.

The second line of the input contains n integers a1,a2,…,an (1≤ai≤109), where ai is the interestingness of the i-th topic for the teacher.

The third line of the input contains n integers b1,b2,…,bn (1≤bi≤109), where bi is the interestingness of the i-th topic for the students.

Output
Print one integer — the number of good pairs of topic.

Examples
Input
5
4 8 2 6 2
4 5 4 1 3
Output
7
Input
4
1 3 2 4
1 3 2 4
Output
0
思路:由于i<j,所以我们从后往前来,这样就可以了。题目要求ai+aj>bi+bj,那么也就是ai-bi>bj-aj。由于有可能是负数,因此我们离散化bj-aj,然后随时更新到树状数组中,然后对于每一个ai-bi求树状数组中有多少个数小于它。注意开 long long。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxx=2e5+100;
struct node{
int x;
int y;
}p[maxx];
int c[maxx];
int a[maxx];
int n;
inline int lowbit(int x){return x&-x;}
inline void update(int x,int v)
{
while(x<maxx)
{
c[x]+=v;
x+=lowbit(x);
}
}
inline int query(int x)
{
int sum=0;
while(x)
{
sum+=c[x];
x-=lowbit(x);
}
return sum;
}
int main()
{
scanf("%d",&n);
memset(c,0,sizeof(c));
for(int i=1;i<=n;i++) scanf("%d",&p[i].x);
for(int i=1;i<=n;i++) scanf("%d",&p[i].y);
for(int i=1;i<=n;i++) a[i]=p[i].y-p[i].x;
sort(a+1,a+1+n);
ll ans=0,num;
for(int i=n;i>=1;i--)
{
num=p[i].x-p[i].y;
int pos=lower_bound(a+1,a+1+n,num)-a;
ans+=(ll)query(pos-1);
pos=lower_bound(a+1,a+1+n,p[i].y-p[i].x)-a;
update(pos,1);
}
cout<<ans<<endl;
return 0;
}

努力加油a啊,(o)/~

最后

以上就是苗条白云为你收集整理的Pair of Topics CodeForces - 1324D(逆序+树状数组)的全部内容,希望文章能够帮你解决Pair of Topics CodeForces - 1324D(逆序+树状数组)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部