• 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;
    }
  • 相关阅读:
    Windows下 如何添加开机启动项
    Android在 普通类(非Activity,多数为Adapter) 中 传输数据为空值 解决方法 :在startActivity 用 intent传输数据
    Android 从ImageView中获取Bitmap对象方法
    剑指offer(纪念版)读书笔记【实时更新】
    剑指offer(纪念版) 面试题3:二维数组中的查找
    C++ sizeof 误区 大公司面试题
    51 nod 1521 一维战舰 时间复杂度O(n),同 Codeforces 567D. One-Dimensional Battle Ships 有详细注释
    51nod 1126 求递推序列的第N项 思路:递推模拟,求循环节。详细注释
    51nod 1451 合法三角形 判斜率去重,时间复杂度O(n^2)
    关于JetBrains CLion 激活 (CLion License Activation)的解决办法,带hosts详细修改
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/6071497.html
Copyright © 2020-2023  润新知