HLG 1400 汽车比赛

题目链接:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1400

结构体排序+树状数组模板..

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int c[100009];
struct point
{
    int x;
    int v;
} p[100000];
bool cmp(point p1,point p2)
{

    if(p1.x !=p2.x)
        return p1.x>p2.x;
    else
        return p1.v<p2.v;
}
int lowbit(int x)
{
    return x & (-x);
}
void update(int i,int val)
{
    while(i<=100000)
    {
        c[i]+=val;
        i+=lowbit(i);
    }

}
int Sum(int i)
{
    int s=0;
    while(i>0)
    {
        s += c[i];
        i -= lowbit(i);
    }
    return s;
}
int main()
{
    //freopen("input.txt","r",stdin);
    int i,n;
    while(scanf("%d",&n)!=EOF)
    {

        memset(c,0,sizeof(c));
        long long int ans = 0;
        for(i=0; i<n; i++)
            scanf("%d%d",&p[i].x,&p[i].v);
        sort(p,p+n,cmp);
        for(i=0; i<n; i++)
        {
            ans += Sum(p[i].v);
            update(p[i].v+1,1);
        }
        printf("%lld\n",ans);
    }
    return 0;
}

 

posted @ 2014-03-12 20:25  霖‘  阅读(110)  评论(0编辑  收藏  举报