zoukankan      html  css  js  c++  java
  • POJ 3067 Japan(树状数组)

                                                                                  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+10;
    const int M = 24005;
    const int mod=1e9+7;
    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=0;
        while(k>0){
            sum+=tree[k];
            k-=k&(-k);
        }
        return sum;
    }
    int main() {
        int T;
        scanf("%d",&T);
        for(int t=1;t<=T;t++){
            met(tree,0);met(a,0);
            scanf("%d%d%d",&n,&m,&k);
            for(int i=1;i<=k;i++){
                scanf("%d%d",&a[i].x,&a[i].y);
            }
            sort(a+1,a+1+k);
            ll ans=0;
            for(int i=1;i<=k;i++){
                ans+=Sum(a[i].x-1);
                add(a[i].x,1);
            }
            printf("Test case %d: %lld
    ",t,ans);
        }
        return 0;
    }
  • 相关阅读:
    Js五子棋游戏
    换肤
    jqzoom实战
    iphone开发笔记
    腾讯召实习生笔试题
    ajax( 汇率转化)
    20140805&nbsp;17:24
    加载上一级目录下的图片文件并得到加载图片的大小
    精确碰撞检测 bitmapData.hitTest
    buttonMode 与mouseChildren【替换鼠标之后不响应鼠标事件】
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/6071497.html
Copyright © 2011-2022 走看看