# Codeforces Round #627 (Div. 3) D. Pair of Topics（二分/直接遍历）

The next lecture in a high school requires two topics to be discussed. The

The pair of topics

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

Input

The first line of the input contains one integer

The second line of the input contains

The third line of the input contains

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这种题套路就是移一下项转化为ai-bi，然后先对ai-bi=ci进行排序。若要ci+cj>0，这两个起码要有一个为0，所以从大到小扫一遍，当ci大于0时，用upper_bound找到第一个大于-ci的数，这样一定能保证这个区间里的数的和大于0，把长度累加到答案里就行。其实也可以双指针同时往中间移动，这么写啰嗦一点不过常数比较小....能过万岁
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
struct good
{
int a,b;
}g[200005];
int res[200005];
int n;

int main()
{
cin>>n;
int i;
for(i=1;i<=n;i++)scanf("%d",&g[i].a);
for(i=1;i<=n;i++)scanf("%d",&g[i].b);
for(i=1;i<=n;i++)res[i]=g[i].a-g[i].b;
sort(res+1,res+n+1);
long long ans=0;
for(i=n;i>=1;i--)
{
if(res[i<0])break;
int pos=upper_bound(res+1,res+i+1,-res[i])-res;
if(pos>=1&&pos<i)
{
ans+=i-pos;
}
}
cout<<ans;
} 

Codeforces Round #627 (Div. 3) D. Pair of Topics（二分/直接遍历）

(0)
(0)