POJ 3067 Japan(树状数组)

时间:2023-02-12 15:45:43
                                                                              Japan
 
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 26544   Accepted: 7206

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
[题意]给你一个二部图及一些边,问你线段之间有多少组线段是相交的,交点在顶点的不算。
首先说一下这题数据有问题,应该是10的6次方。然后看看这题,又是统计,而且统计次数还很大,那就可以用树状数组来做了。
首先将y从大到小排序,若y相等则将x从大到小排序,这样,当统计到当前(x,y)时,在x 前面有多少x就把加多少到ans
因为对于两条线段(x1,y1),(x2,y2),若y1>y2&&x1<x2那么他们就相交。接下来就是树状数组来统计了。。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
typedef long long ll;
using namespace std;
const int N = 2e6+;
const int M = ;
const int mod=1e9+;
ll tree[N];
int n,m,k;
struct man{
int x,y;
bool operator< (const man &it)const{
if(y==it.y)return x>it.x;
return y>it.y;
}
}a[N];
void add(int k,int num){
while(k<=n){
tree[k]+=num;
k+=k&(-k);
}
}
ll Sum(int k){
ll sum=;
while(k>){
sum+=tree[k];
k-=k&(-k);
}
return sum;
}
int main() {
int T;
scanf("%d",&T);
for(int t=;t<=T;t++){
met(tree,);met(a,);
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=k;i++){
scanf("%d%d",&a[i].x,&a[i].y);
}
sort(a+,a++k);
ll ans=;
for(int i=;i<=k;i++){
ans+=Sum(a[i].x-);
add(a[i].x,);
}
printf("Test case %d: %lld\n",t,ans);
}
return ;
}