(POJ 3067)Japan 树状数组

Japan
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 27518 Accepted: 7447
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

题意:
在东西岸两边各有n,m个城市。城市从北到南一次编号为1,2,3…。现在在两岸之间建k条路,每条路直接相连东岸的x城市和西岸的y城市,问你所有的路共有多少个交点?

分析:

相交的条件:设有两条路(xi,yi)和(xj,yj),当满足xj > xi && yj < yi 时两条路径就有一个交点了。

所有我们只要将所有的路按照x小的排在前面,x相等的y小的排在前面,那么题目就转换为了求对于每一条路,在他前面的路中的y值比当前路的y值大的路的个数的总和了。(其实就是求y值的逆序对数了)

所以我们使用树状数组求解就可以了。

注意:
题目中可能有重复的路,所以要特别处理
题目最大的坑点就是数组要开到一百万才行,否则一直runtime error….

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

const int maxn = 1000010;
struct node
{
    int x,y;
}nodes[maxn];

int n,m,k,c[maxn];
long long ans[maxn];

int lowbit(int x)
{
    return x & (-x);
}

void update(int x)
{
    while(x <= maxn)
    {
        c[x]++;
        x += lowbit(x);
    }
}

int getsum(int x)
{
    int sum = 0;
    while(x > 0)
    {
        sum += c[x];
        x -= lowbit(x);
    }
    return sum;
}

int cmp(node a,node b)
{
    if(a.x < b.x) return 1;
    else if(a.x == b.x && a.y < b.y) return 1;
    return 0;
}

int main()
{
    int t,cas = 1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&k);
        memset(c,0,sizeof(c));
        memset(ans,0,sizeof(ans));
        for(int i=0;i<k;i++) scanf("%d%d",&nodes[i].x,&nodes[i].y);
        sort(nodes,nodes+k,cmp);
        for(int i=0;i<k;i++)
        {
            if(i > 0 && nodes[i].x == nodes[i-1].x && nodes[i].y == nodes[i-1].y)  //处理相同路的情况
            {
                ans[i] = ans[i-1];
                update(nodes[i].y);
                continue;
            }
            int t = i - getsum(nodes[i].y);
            ans[i] = t;
            update(nodes[i].y);
        }
        long long anssum = 0;
        for(int i=0;i<k;i++) anssum += ans[i];
        printf("Test case %d: %lld\n",cas++,anssum);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值