• POJ 3067 Japan 树状数组求逆序对


    题目大意:有两排城市,这两排城市之间有一些路相互连接着,求有多少条路相互交叉。


    思路:把全部的路先依照x值从小到大排序,x值同样的依照y值从小到大排序,然后插入边的时候,先找有多少比自己y值小的,这些边的x值一定比自己大,也就是一个逆序对,然后统计起来。记得答案要用long long (__int64)


    CODE:


    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #define MAX 1010
    using namespace std;
    
    struct Complex{
    	int x,y;
    
    	bool operator <(const Complex &a)const {
    		if(x != a.x)	return x < a.x;
    		return y < a.y;
    	}
    }point[MAX * MAX];
    
    int cases;
    int m,n,cnt;
    __int64 ans;
    
    __int64 fenwick[MAX];
    
    inline void Initialize();
    
    inline void Fix(int x);
    inline __int64 GetSum(int x);
    
    int main()
    {
    	cin >> cases;
    	for(int T = 1;T <= cases; ++T) {
    		scanf("%d%d%d",&m,&n,&cnt);
    		Initialize();
    		for(int i = 1;i <= cnt; ++i)
    			scanf("%d%d",&point[i].x,&point[i].y);
    		sort(point + 1,point + cnt + 1);
    		for(int i = 1;i <= cnt; ++i) {
    			ans += GetSum(point[i].y + 1);
    			Fix(point[i].y);
    		}
    		cout << "Test case " << T << ": " << ans << endl;
    	}
    	return 0;
    }
    
    inline void Initialize()
    {
    	ans = 0;
    	memset(fenwick,0,sizeof(fenwick));
    }
    
    inline void Fix(int x)
    {
    	for(;x;x -= x&-x)
    		fenwick[x]++;
    }
    inline __int64 GetSum(int x)
    {
    	__int64 re = 0;
    	for(;x <= n;x += x&-x)
    		re += fenwick[x];
    	return re;
    }



  • 相关阅读:
    Build a pile of Cubes
    一键升级所有pip过期库
    AWGN
    调制详解——待完善
    BASK、BFSK、BPSK调制方法的Matlab程序实现
    tomcat运行问题解决方法
    ehcache简单使用
    MySQL 数据库中用户表中口令登陆设置
    和自己赛跑的人
    中文词频统计
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4320639.html
Copyright © 2020-2023  润新知