#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define lowbit(x) x&-x
#define LL long long
#define maxn 1000005
using namespace std;
struct node
{
int pos;
int val;
}B[maxn];
int V[maxn];
bool cmp(node a,node b)
{
if(a.pos!=b.pos) return a.pos<b.pos;
else return a.val>b.val;
}
void upd(int k,int m)
{
while(k<=m)
{
V[k]++;
k+=lowbit(k);
}
}
LL sum(int k)
{
LL ans=0;
while(k>0)
{
ans+=V[k];
k-=lowbit(k);
}
return ans;
}
int main()
{
ios::sync_with_stdio(0);
int n;
while(cin>>n)
{
int m=0;
memset(V,0,sizeof(V));
for(int i=1;i<=n;i++)
{
cin>>B[i].pos>>B[i].val;
if(B[i].val>m)m=B[i].val;
}
sort(B+1,B+1+n,cmp);
LL res=0;
for(int i=1;i<=n;i++)//每次计算在第i辆车前面(包括自己)有多少个速度小于自己的车。(i-sum)即为超车次数。
{
upd(B[i].val,m);
res+=i-sum(B[i].val);
}
cout<<res<<endl;
}
}
hrbsut 1400 (树状数组)
最新推荐文章于 2022-02-22 21:46:47 发布