Smile

Smile

常用链接

统计

最新评论

树状数组 japan

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int const max_n = 1010;

typedef struct node{
    int x;
    int y;
}node;

node a[max_n*max_n];
int num[max_n];
int b[max_n*max_n];
__int64 ans;
int t,n,m,k,max_y;

bool cmp(node na,node nb){
    if(na.x<nb.x)
        return true;
    if( na.x == nb.x )
        if( na.y<nb.y)
            return true;
    return false;
}

int lowbit( int t){//设t的末尾有k个0,返回值为2的k次方
    return  t & (t^(t-1));
}

void modify(int pos,int add){//修改操作
    while ( pos <= max_y) {//变量范围为1至最大的值
        num[pos] += add;
        pos += lowbit(pos);
    }
}

int getsum(int end)
{
    int sum=0;
    while (end>0) {
        sum+=num[end];
        end-=lowbit(end);
    }
    return sum;
}

int main(){
    while( ~scanf("%d",&t) ){
        int id = 0;
        while(t--){
            scanf("%d%d%d",&n,&m,&k);
            max_y = -1;
            for( int i=0; i<k; i++ ){
                scanf("%d%d",&a[i].x,&a[i].y);
                if( max_y < a[i].y )
                    max_y = a[i].y;
            }
            sort(a,a+k,cmp);
            for( int i=0; i<k; i++ )
                b[i+1] = a[i].y;
            ans = 0;
            memset(num,0,sizeof(num));
            for( int i=1; i<=k; i++ ){
                modify(b[i],1);//将b[i]之后的加1操作,表示比它小
                ans += i - getsum(b[i]);//getsum得到是比b[i]小的个数。
            }
            printf("Test case %d: %I64d\n",++id,ans);
        }
    }
    return 0;
}

posted on 2011-08-09 21:50 Smile3 阅读(73) 评论(0)  编辑 收藏 引用 所属分类: 其他题目模板