Codeforces Round # 627 (Div. 3) D. Pair of Topics (binary / direct traversal)

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

The pair of topics ii and jj (i<ji<j ) is called good if ai+aj>bi+bjai+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 nn (2n21052≤n≤2⋅105 ) — the number of topics.

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

The third line of the input contains nn integers b1,b2,,bnb1,b2,…,bn (1bi1091≤bi≤109 ), where bibi is the interestingness of the ii -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 
This is a routine question about the item shift into ai-bi, then first of ai-bi = ci sort. To ci + cj> 0, one of the two must be at least 0, it is again descending sweep, when ci is greater than 0, with the first number is greater than upper_bound -ci found, so this will be able to guarantee interval Lane number greater than 0, the answer to the cumulative length on the line.
In fact, you can also double-pointer while moving to the middle, so write a little long-winded relatively small but constant can live long live ....
#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)
        { 
            Years + i = pos; 
        } 
    } 
    Cout << years; 
 }

 



Guess you like

Origin www.cnblogs.com/lipoicyclic/p/12590480.html