您的位置:首页 > 编程语言 > C语言/C++

POJ 3067 Japan (树状数组)

2015-01-01 10:46 351 查看
题目地址:POJ 3067

按x为第一关键字从小到大排序,再按y为第二关键字从小到大排序,然后用y来建立树状数组,每次找比y大的就是每次更新的交点数。

代码如下:

#include <iostream>
#include <string.h>
#include <math.h>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <set>
#include <stdio.h>
using namespace std;
#define LL __int64
int a[11000], m;
struct node
{
int x, y;
}fei[1000000];
int cmp(node f1, node f2)
{
if(f1.x==f2.x) return f1.y<f2.y;
return f1.x<f2.x;
}
int lowbit(int x)
{
return x&(-x);
}
void Update(int x)
{
while(x<=m){
a[x]++;
x+=lowbit(x);
}
}
int Sum(int x)
{
LL ans=0;
while(x>0){
ans+=a[x];
x-=lowbit(x);
}
return ans;
}
int main()
{
int t, n, k, i, num=0;
LL ans;
scanf("%d",&t);
while(t--){
num++;
scanf("%d%d%d",&n,&m,&k);
for(i=0;i<k;i++){
scanf("%d%d",&fei[i].x,&fei[i].y);
}
sort(fei,fei+k,cmp);
ans=0;
memset(a,0,sizeof(a));
for(i=0;i<k;i++){
ans+=i-Sum(fei[i].y);
Update(fei[i].y);
//printf("%I64d\n",ans);
}
printf("Test case %d: %I64d\n",num,ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息