题意:给出 车辆起始位置X i 和速度 V i求超车次数
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
struct node
{
int v,p;
}q[100001];
int cmp(struct node a,struct node b)
{
if(a.p==b.p)return a.v>b.v;
return a.p<b.p;
}
long long a[100005];
int maxn;
int lowbit(int x)
{
return (x)&(-x);
}
void add(int pos)
{
while (pos <= maxn)
{
a[pos] += 1;
pos += lowbit(pos);
}
}
long long sum(int pos)
{
long long s = 0;
while (pos > 0)
{
s += a[pos];
pos -= lowbit(pos);
}
return s;
}
int main()
{
int n,i;
long long res;
while(scanf("%d",&n)!=EOF)
{
res=0;
maxn=0;
memset(a,0,sizeof(a));
for(i=1;i<=n;i++)
{
scanf("%d%d",&q[i].p,&q[i].v);
if(maxn<q[i].v)maxn=q[i].v;
}
sort(q,q+n+1,cmp);
for(i=1;i<=n;i++)
{
add(q[i].v);
res=res+(i-sum(q[i].v));
}
printf("%lld\n",res);
}
return 0;
}
hrbust1400 11年省赛 树状数组(逆序数)
最新推荐文章于 2020-03-10 13:11:06 发布