poj 3067 Japan 树状数组求逆序数

Japan
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 18267 Accepted: 4933

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
 
题意:日本有东海岸和西海岸,东海岸有n个岛,编号为1-n,西海岸有m个岛,编号为1-m,有k条直路将东面的岛和西面的岛连接起来。给出每条路连接哪两个岛:东面A、西面B,要求出这些路形成了多少个交点(最多两条直线交于一点)。
思路:画图可以看出,如果将每条路连接的两个岛A、B化为x、y,作出二维坐标,每条路就看出了一个点,那么每个点将与它左上方(不能是正上方或正左边)的点相交,计算出每个点有多少个左上方的点即可。按y从大到小排序,保证前面的点都在某个点的上方,y相等时按x从大到小排序,注意x不能从小到大排序,因为不能统计正左边的。然后求x的逆序数即可
 
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <cmath>
#include <cstdlib>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define ll __int64
#define eps 1e-6

using namespace std;

const int INF = 1000000007;
const int maxn = 1000005;
struct node
{
    int x,y;
}road[maxn];
int n, m, k;
ll c[1005];
bool cmp(node a, node b)
{
    if(a.y != b.y) return a.y > b.y;
    return a.x > b.x;
}
int lowbit(int x)
{
    return x & (-x);
}
void add(int x)
{
    for(int i = x; i < n; i += lowbit(i))
    c[i]++;
}
ll sum(int x)
{
    ll ret = 0;
    for(int i = x; i > 0; i -= lowbit(i))
    ret += c[i];
    return ret;
}
int main()
{
    int t, ca = 0;
    scanf("%d", &t);
    while(t--)
    {
        memset(c, 0, sizeof(c));
        scanf("%d%d%d", &n, &m, &k);
        for(int i = 0; i < k; i++)
        scanf("%d%d", &road[i].x, &road[i].y);
        sort(road, road + k, cmp);

        ll ans = 0;
        for(int i = 0; i < k; i++)
        {
            ans += sum(road[i].x - 1);
            add(road[i].x);
        }
        printf("Test case %d: %I64d\n", ++ca, ans);
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值