codeforces627 div3 D Pair of Topics(树状数组+离散化)

D. Pair of Topics

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

The pair of topics $$$i$$$ and $$$j$$$ ($$$i < j$$$) is called good if $$$a_i + a_j > b_i + b_j$$$ (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 \le n \le 2 \cdot 10^5$$$) — the number of topics.

The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the interestingness of the $$$i$$$-th topic for the teacher.

The third line of the input contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le 10^9$$$), where $$$b_i$$$ 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

我不知道我是不是做复杂了

题意:给n对数 ai 和 aj 问有多少对 i<j 使得 a[i] +a[j] > b[i] + b[j]

变一下 有 a[i] - a[j]  + b[i] - b[j] >0

那么存 每一个 a[i] = a[i] - b[j]

遍历j 找i 利用树状数组 在a[i]的地方插1,那么相当于每次要找 当前位置前面 有多少个数是大于(-a[i])的

由于有负数,需要离散化,而且-a[i]也要作为树状数组的一个点,避免离散化出错。

 

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ff(i,a,b) for(int i = a; i <= b; i++)
#define f(i,a,b) for(int i = a; i < b; i++)
typedef pair<int,int> P;
#define ll long long
#define lowbit(x) (x)&(-x)
P p[200010];
int a[400010],b[400010];
int tr[400010];
int n;
int getid(int x){ return lower_bound(b + 1, b + 2 * n + 1, x) - b;}
int sum(int x){
	int ans = 0;
	while(x > 0)
	{
		ans+=tr[x];
		x-=lowbit(x);
	}
	return ans;
}
 
void update(int x,int d){
	while(x <= 2 * n)
	{
		tr[x]+=d;
		x+=lowbit(x);
	}
}
 
int main()
{
    ios::sync_with_stdio(false);
    cin >> n;
    ff(i,1,n) cin >> p[i].fi;
    ff(i,1,n) cin >> p[i].se;
    ff(i,1,n) a[i] = p[i].fi - p[i].se;
    ff(i,1,n) b[i] = a[i];
    ff(i,1,n) b[n + i] = -a[i];
    sort(b + 1, b + 2 * n + 1);
    ll ans = 0;
    ff(i,1,n)
    {
    	int tt = i - 1 - sum(getid(-a[i]));
    	// cout << "sum : " << sum(getid(-a[i])) << endl;
    	ans = ans + tt;
    	// cout << tt << endl;
    	update(getid(a[i]),1);
    }
    cout << ans << endl;
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值