Newcoder 4 A.Contest(逆序對-BIT)

Description

nn支隊伍一共參加了三場比賽。

一支隊伍xx認爲自己比另一支隊伍yy強當且僅當xx在至少一場比賽中比yy的排名高。

求有多少組(x,y)(x,y),使得xx自己覺得比yy強,yy自己也覺得比xx強。

$ (x, y), (y, x)$算一組。

Input

第一行一個整數nn,表示隊伍數; 接下來nn行,每行三個整數a[i],b[i],c[i]a[i], b[i], c[i],分別表示ii在第一場、第二場和第三場比賽中的名次;nn 最大不超過200000200000

Output

輸出一個整數表示滿足條件的(x,y)(x,y)數;64bit64bit請用lldlld

Sample Input

4
1 3 1
2 2 4
4 1 2
3 4 3

Sample Output

5

Solution

(x,y)(x,y)之間必然是xx兩敗一勝或兩勝一敗,考慮因a,b,ca,b,c其中之一敗因a,b,ca,b,c其中另一勝的二元組個數之和(即二元組逆序對個數),那麼一組合法解(x,y)(x,y)會被計算四次,所求答案除以四即爲答案,時間複雜度O(nlogn)O(nlogn)

Code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=200005;
struct BIT 
{
	#define lowbit(x) (x&(-x))
	int b[maxn],n;
	void init(int _n)
	{
		n=_n;
		for(int i=1;i<=n;i++)b[i]=0;
	}
	void update(int x,int v)
	{
		while(x<=n)
		{
			b[x]+=v;
			x+=lowbit(x);
		}
	}
	int query(int x)
	{
		int ans=0;
		while(x)
		{
			ans+=b[x];
			x-=lowbit(x);
		}
		return ans;
	}
}bit;
int n,x[3][maxn];
P a[maxn];
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d%d%d",&x[0][i],&x[1][i],&x[2][i]);
	ll ans=0;
	for(int i=0;i<3;i++)
		for(int j=0;j<3;j++)
			if(i!=j)
			{
				for(int k=1;k<=n;k++)a[k]=P(x[i][k],x[j][k]);
				sort(a+1,a+n+1);
				bit.init(n);
				for(int k=n;k>=1;k--)
				{
					ans+=bit.query(a[k].second);
					bit.update(a[k].second,1);
				}
			}
	printf("%lld\n",ans/4);
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章