poj3067

来源:互联网 发布:淘宝专柜代购5折可信吗 编辑:程序博客网 时间:2024/06/13 05:30
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cstring>
#define MAX 1000010


using namespace std;


int c[MAX];
int T,N,M,K;
long long ans;


struct road
{
    int x;
    int y;
}a[MAX];


bool cmp(road a,road b)
{
    if (a.x<b.x) return 1;
    if (a.x==b.x) return a.y<b.y;
    return 0;
}


int lowbit(int x)
{
    return x&(-x);
}


void add(int po,int val)
{
    while (po<=M)
    {
        c[po]+=val;
        po+=lowbit(po);
    }
}


long long  getsum(int po)
{
    long long  sum=0;
    while (po>0)
    {
        sum+=c[po];
        po-=lowbit(po);
    }
    return sum;
}


int main()
{
    freopen("in.txt","r",stdin);
    cin>>T;
    for (int j=1;j<=T;j++)
    {
        scanf("%d%d%d",&N,&M,&K);
        for (int i=1;i<=K;i++) scanf("%d%d",&a[i].x,&a[i].y);
        memset(c,0,sizeof(c));
        ans=0;
        sort(a+1,a+K+1,cmp);
        for (int i=1;i<=K;i++)
        {
            add(a[i].y,1);
            ans+=i-getsum(a[i].y);
        }
        printf("Test case %d: %lld\n",j,ans);
    }
    //cout << "Hello world!" << endl;
    return 0;
}
/*题意:日本计划在东边的城市和西边的城市中建路,东边的点x从1.....n,西边的点y从1..........m,求这些点连起来后有多少交叉
  树状数组,按x为第一关键字升序排序,按y为第二关键字升序排序(减去的时候可以把x相同的项减去),求出在 当前x 前面的x对应的y比 当前y 小的个数,
  再用i-减去,得到比y大的个数
*/
0 0
原创粉丝点击