[POJ3067] Japan

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

这题以自己的思路做,在wa了几发后竟然ac了。。。
刚开始wa是因为我以为2条线的端点在city点相交也算一个交点,然后一直写的是res+=query(ts[i].c,m-ts[i].d+1);百思不得其解,后来看百度都是什么求逆序数,再后来才看到一个和我一样的思路,然后他说是从x-1和y-1开始的,所以我才醒悟过来是不算city那个点的,所以应该写成res+=query(ts[i].c-1,m-ts[i].d+1-1);//m-ts[i].d+1,因为倒过来算。
哦,对了,不用去管三线交于一点的情况。。


我的思路:左右(左east,右west)2侧,先按左边的大小排序,相等再按右边的,然后每条线的交点肯定和之前已经连接的线有关,然后只需去查左边的点的上面的连到右边的点的下面的条数,即穿越了几条线
左边从上到下编号从1开始,右边从下到上编号从1开始。关于更新那就反过来更新。

代码

二维树状数组解法,比直接求逆序数的那种慢

/*
又是一道我看不懂题意的题。。
是直接求交点几个么。。不用管三条线交于一点那种情况??

思路:左右2侧,先按左边的大小排序,相等再按右边的,然后每条线的交点肯定和之前已经连接的线有关,然后只需去查左边的点的上面的连到右边的点的下面的条数,即穿越了几条线
左边从上到下编号从1开始,右边从下到上编号从1开始。关于更新那就反过来更新
*/
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int mx=1000;
int n,m;
struct Node
{
    int c,d;//c存east,d存west
}ts[mx*mx];
int jz[mx+3][mx+3];//行表示n的,即east;列表示m的,即west
bool cmp(Node a,Node b)
{
    if (a.c!=b.c) return a.c<b.c;
    return a.d>b.d;//这里改成小于号<也没关系,虽然下面排序时,因为我m-ts[i].d+1会使小的变大,导致右边那列先排了大的数,但是没有关系,因为询问query的时候,加入左边是x这么大,右边是y这么大,但是询问是从x-1,y-1开始的,所以当x相同的时候,y从大到小还是从小到大都是没有关系的
}
int lowbit(int x)
{
    return x&(-x);
}
void updata(int x,int y,int val)
{
    int z=y;
    while (x<=n)//n
    {
        y=z;
        while (y<=m)//m
        {
            jz[x][y]+=val;
            y+=lowbit(y);
        }
        x+=lowbit(x);
    }
}
int query(int x,int y)
{
    int ans=0;
    int z=y;
    while (x>0)
    {
        y=z;
        while (y>0)
        {
            ans+=jz[x][y];
            y-=lowbit(y);
        }
        x-=lowbit(x);
    }
    return ans;
}
int main()
{
    long long res;
    int t,k,a,b,cas=0;
    scanf("%d",&t);
    while (t--)
    {
        res=0;
        scanf("%d%d%d",&n,&m,&k);
        memset(jz,0,sizeof(jz));
        for (int i=1;i<=k;i++)
        {
            scanf("%d%d",&ts[i].c,&ts[i].d);
        }
        sort(ts+1,ts+k+1,cmp);
        for (int i=1;i<=k;i++)
        {
            res+=query(ts[i].c-1,m-ts[i].d+1-1);//m-ts[i].d+1,因为倒过来算,且是从x-1,y-1开始的
            updata(ts[i].c,m-ts[i].d+1,1);
        }
        printf("Test case %d: %lld\n",++cas,res);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值