zoukankan      html  css  js  c++  java
  • poj3067树状数组求逆序数

    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
    题意:给一组成对的点求相交的线段
    题解:逆序数(虽然我也不知道这是个啥),先排序按右边小的排,右边相同按左边小的排,保证不会因为右边点相同而计算重复,然后求sum(N)-sum(s[i].b)(好像是逆序数);
    一开始思路都是对的,结果写太搓了,TLE了,无奈之下看题解,居然和我写的一模一样。。。。(代码能力太差》。《)
    #include<map>
    #include<set>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<vector>
    #include<cstdio>
    #include<iomanip>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define pi acos(-1)
    #define ll long long
    #define mod 1000000007
    #define ls l,m,rt<<1
    #define rs m+1,r,rt<<1|1
    
    using namespace std;
    
    const double g=10.0,eps=1e-9;
    const int N=1000+5,maxn=32000+5,inf=0x3f3f3f3f;
    
    struct edge{
        int a,b;
    }s[N*N];
    int c[N];
    
    bool comp(const edge &x,const edge &y)
    {
        if(x.a!=y.a)return x.a<y.a;
        return x.b<y.b;
    }
    void add(int i)
    {
        while(i<N){
            c[i]++;
            i+=i&(-i);
        }
    }
    ll sum(int i)
    {
        ll ans=0;
        while(i>0){
            ans+=c[i];
            i-=i&(-i);
        }
        return ans;
    }
    int main()
    {
     /*   ios::sync_with_stdio(false);
        cin.tie(0);*/
     //   cout<<setiosflags(ios::fixed)<<setprecision(2);
        int t,cnt=0,n,m,k;
        scanf("%d",&t);
        while(t--){
            scanf("%d%d%d",&n,&m,&k);
            memset(c,0,sizeof c);
            for(int i=0;i<k;i++)scanf("%d%d",&s[i].a,&s[i].b);
            sort(s,s+k,comp);
            ll ans=0;
            for(int i=0;i<k;i++)
            {
                add(s[i].b);
                ans+=(sum(N)-sum(s[i].b));
            }
            printf("Test case %d: %lld
    ",++cnt,ans);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    数组中的逆序对
    第一个只出现一次的字符
    丑数
    把数组排成最小的数
    整数中出现1的个数
    连续子数组最大和
    JS之window对象
    JS之递归(例题:猴子吃桃)
    JS中函数的基础知识
    JS数组2(冒泡排列、数组里面查找数据)
  • 原文地址:https://www.cnblogs.com/acjiumeng/p/6801766.html
Copyright © 2011-2022 走看看