您的位置:首页 > 其它

poj 3067 Japan(树状数组)

2015-08-04 12:08 225 查看
Japan

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 23111Accepted: 6232
Description

Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Japan is tall island with N cities on the East coast and M cities on the West coast (M <= 1000, N <= 1000). K superhighways will be build. Cities on each coast are numbered 1, 2, ... from North to South. Each superhighway is straight line and connects city on the East coast with city of the West coast. The funding for the construction is guaranteed by ACM. A major portion of the sum is determined by the number of crossings between superhighways. At most two superhighways cross at one location. Write a program that calculates the number of the crossings between superhighways.
Input

The input file starts with T - the number of test cases. Each test case starts with three numbers – N, M, K. Each of the next K lines contains two numbers – the numbers of cities connected by the superhighway. The first one is the number of the city on the East coast and second one is the number of the city of the West coast.
Output

For each test case write one line on the standard output:
Test case (case number): (number of crossings)
Sample Input

1
3 4 4
1 4
2 3
3 2
3 1

Sample Output

Test case 1: 5

Source

Southeastern Europe 2006

树状数组第三题。
这道题可以先排序,x升序,x相同的时候,y升序
然后求y中的逆序对数。
大概知道了怎么用树状数组求逆序对。
[b]下标(从0开始)-比a[i]小的=a[i]与之前形成的逆序对数。[/b]
具体见代码注释

/*************************************************************************
> File Name: code/poj/3067.cpp
> Author: 111qqz
> Email: rkz2013@126.com
> Created Time: 2015年08月04日 星期二 10时49分06秒
************************************************************************/

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<stack>
#define y0 abc111qqz
#define y1 hust111qqz
#define yn hez111qqz
#define j1 cute111qqz
#define tm crazy111qqz
#define lr dying111qqz
using namespace std;
#define REP(i, n) for (int i=0;i<int(n);++i)
typedef long long LL;
typedef unsigned long long ULL;
const int inf = 0x7fffffff;
const int N=1E3+5;

int n,m,k;
int t[N*N/2];
struct R
{
int x,y;
}r[N*N/2];

bool cmp(R a,R b)
{
if (a.x<b.x) return true;
if (a.x==b.x&&a.y<b.y) return true;
return false;
}
int lowbit(int x)
{
return x&(-x);
}
void update (int x,int c)
{
for ( int i = x ; i < N ; i = i + lowbit(i))
{
t[i] = t[i] + c;
}
}
LL sum ( int x)
{
int res = 0 ;
for ( int i = x ; i >= 1 ; i = i - lowbit(i))
{
res = res + t[i];
}
return res;
}
int main()
{
int T;
cin>>T;
LL ans ;
int cas = 0;
while (T--)
{
memset(t,0,sizeof(t));
memset(r,0,sizeof(r));
ans =  0 ;
cas++;
scanf("%d %d %d",&n,&m,&k);
for ( int i = 0 ; i  < k ; i ++ )
{
scanf("%d %d",&r[i].x,&r[i].y);
}
sort(r,r+k,cmp);
for ( int i = 0 ; i < k;  i ++ )  //起点终点相同不算crossing
{
//  update(r[i].x,1);
// if (r[i].x==r[i-1].x||r[i].y==r[i-1].y) continue;
ans = ans +i- sum(r[i].y);  //求y的逆序对数,好好理解这行代码!
//    cout<<"ans:"<<ans<<endl;
update(r[i].y,1);
}
printf("Test case %d: %lld\n",cas,ans);

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: