2016 UESTC Training for Data Structures R - Japan 树状数组求逆序数

R - Japan

Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
Submit  Status

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 N cities on the

 East coast and  M cities on the West coast ( 1M10000 1N10000 ).  K  superhighways will be 

build.( 1K1000000  ) 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 and output

Sample Input Sample Output
1
3 4 4
1 4
2 3
3 2
3 1
Test case 1: 5

Hint

The data used in this problem is unofficial data prepared by pfctgeorge. So any mistake here does not imply mistake in the offcial judge data.

Source

2016 UESTC Training for Data Structures  Problem D

My Solution

与2016 UESTC Training for Data Structures E - 卿学姐与城堡的墙 树状数组求逆序对、离散化 那题很相似,不过这里不计左右边界上的点

又因为这里( 1M10000 1N10000 ) 所以不用离散化,所以直接对右边排序

如果a.vy != b.vy 则 return a.vy < b.vy

如果a.vy != b.vy 则 return a.uy < b.vy  uy小的往上拉,大的往下拉,这样在计算的时候就不会算到这个交点了

复杂度 O(K*logn)


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxk = 1000000 + 8;
const int maxn = 10000 + 8;

int N, M, K;               //具体操作的时候要用所以放到全局
struct segment{
    long long vy, uy;
} seg[maxk];

bool cmpv(const segment& a, const segment& b)
{
    if(a.vy != b.vy) return a.vy < b.vy;
    else return a.uy < b.uy;                  //!!!!!!小的往上拉,大的往上拉,这样算的时候这里的交点就不会算进去了
}



//!树状数组求逆序对
int Tree[maxn];

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

void add(int x, int value)
{
    for(int i = x; i <= N; i += lowbit(i))
        Tree[i] += value;
}

int get(int x)
{
    int sum = 0;
    for(int i = x; i; i -= lowbit(i))
        sum += Tree[i];
    return sum;
}

int main()
{
    #ifdef LOCAL
    freopen("a.txt", "r", stdin);
    #endif // LOCAL
    int T, kase = 0;
    scanf("%d", &T);
    while(T--){
        long long ans = 0;
        scanf("%d%d%d", &N, &M, &K);
        for(int i = 0; i < K; i++){
            scanf("%lld%lld", &seg[i].uy, &seg[i].vy);
        }
        sort(seg, seg + K, cmpv);
    //
        memset(Tree, 0, sizeof Tree);
        for(int i = 1; i <= K; i++){
            add(seg[i-1].uy, 1);
            ans += i - get(seg[i-1].uy);

        }//cout<<ans<<endl;
        //!!!!!!从E题到这里是, E 的 N 全部要改成这里的 K 呀, 不要漏了
        printf("Test case %d: %lld", ++kase, ans);
        if(T) printf("\n");

    }
    return 0;
}

Thank you!

                                                                                                                                               ------from ProLights


你可能感兴趣的:(ACM,for,Data,Training,uestc,2016,structures,树状数组求逆序数)