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

求交叉的点数
满足交叉的条件是si<sj&&ei>ej || si>sj&&ei<ej
排好序后就是求终点逆序数了,可以用线段树实现
#include<stdio.h>
#include<string.h>
#include <algorithm>
using namespace std;

#define N 1010
#define M 1000010
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
int T,n,m,k;
int sum[N<<2],a[N];
__int64 ans;

struct node
{
    int x;
    int y;
} s[M];

int cmp(node a,node b)
{
    if(a.y!=b.y)
        return a.y>b.y;
    return a.x<b.x;
}

void Pushup(int rt)
{
    sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}

void Update(int rt,int l,int r,int x)
{
    if(l == r)
    {
        sum[rt]++;
        a[l]++;
        return ;
    }
    int mid = (l + r) >> 1;
    if(x <= mid)
        Update(lson,x);
    else
        Update(rson,x);
    Pushup(rt);
}

int Query(int rt,int l,int r,int L,int R)
{
    if(L <= l && R >= r)
    {
        return sum[rt];
    }
    int mid= (l + r) >> 1;
    int res= 0;
    if(L <= mid) res += Query(lson,L,R);
    if(R > mid ) res += Query(rson,L,R);
    return res;
}

int main()
{
    int i,j,res,cas = 1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&n,&m,&k);
        memset(sum,0,sizeof(sum));
        memset(a,0,sizeof(a));
        ans = 0;
        res = 0;
        for(i = 1; i <= k; ++i)
            scanf("%d %d",&s[i].x,&s[i].y);
        sort(s+1,s+1+k,cmp);
        for(i = 1; i <= k; ++i)
        {
            int tmp = s[i].y;
            if(i>1 && s[i].y == s[i-1].y)
                res++;
            else
                res = 0;
            ans += Query(1,1,n,1,s[i].x)-a[s[i].x]-res;
            Update(1,1,n,s[i].x);
        }
        printf("Test case %d: %I64d\n",cas++,ans);
    }
    return 0;
}

POJ3067:Japan(线段树),布布扣,bubuko.com

时间: 2024-10-19 13:59:47

POJ3067:Japan(线段树)的相关文章

poj-3067 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. C

poj 3067 Japan(线段树)

题目链接:poj 3067 Japan 题目大意:给定N和M,表示东部和西部城市的数量,然后K条铁路,每条铁路连接东西城市,问说会有多少次交点. 解题思路:线段树维护即可,每条边按照x小的,y小的排序,然后每次查询y+1到M的即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1005; #define lson(x) ((

POJ3067 Japan【树状数组】【逆序数】

题目链接: http://poj.org/problem?id=3067 题目大意: 有两排的城市,一排N个城市,编号为1~N,一排M个城市,编号为1~M.这两排城市之间有K条路. 路都是直线连接,问:这些路,有多少道路是相交的,并且焦点不是城市所在的点,求出交点个数. 思路: 树状数组的思想.参考网上的图,先将所有边(u,v)按u升序排列,如果u相同,则按v升序排列.可 以看出来,路(u1,v1)和路(u2,v2)如果有交点的话,u1 > u2 并且 v1 < v2,或者 u1 < u

POJ3067 Japan 树状数组的应用

这题以前做过,用的线段树,现在用树状数组做一次, 题意:给你n个城市在日本左边,m个城市在日本右边,然后k条路,问你这k条路有几个交点,注意城市的序号其实就是一维坐标所在位置,所以就是两条平行的数轴,上面有点,而且之间有连线,问你有多少交点 一开始不好想把,这种题目也就排排序来试试看了,先对要修建的公路进行排序,然后再看这样是否可以更加方便的求出交点的数论,取路的左边点为先决条件从小到大排列,若相等则按照右边点来升序排列,然后以左边点为序号 和value值为1进行插入树状数组,先求出当前已经插入

两边点连直线求交点总数 树状数组或线段树 poj 3067 Japan

http://poj.org/problem?id=3067 Japan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23602   Accepted: 6369 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

poj3067 Japan(树状数组)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://poj.org/problem?id=3067 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

hdu1540 Tunnel Warfare 线段树/树状数组

During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally speaking, villages connected by tunnels lay in a line. Except the two at the ends, every village was directly con

【线段树区间合并】HDU1540-Tunnel Warfare

一.题目 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally speaking, villages connected by tunnels lay in a line. Except the two at the ends, every village

hdu 1540 Tunnel Warfare 线段树 单点更新,查询区间长度,区间合并

Tunnel Warfare Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1540 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Gene