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-;
const int N=+,maxn=+,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=;
while(i>){
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=,n,m,k;
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&n,&m,&k);
memset(c,,sizeof c);
for(int i=;i<k;i++)scanf("%d%d",&s[i].a,&s[i].b);
sort(s,s+k,comp);
ll ans=;
for(int i=;i<k;i++)
{
add(s[i].b);
ans+=(sum(N)-sum(s[i].b));
}
printf("Test case %d: %lld\n",++cnt,ans);
}
return ;
}

poj3067树状数组求逆序数的更多相关文章

  1. poj 2299 Ultra-QuickSort(树状数组求逆序数)

    链接:http://poj.org/problem?id=2299 题意:给出n个数,求将这n个数从小到大排序,求使用快排的需要交换的次数. 分析:由快排的性质很容易发现,只需要求每个数的逆序数累加起 ...

  2. hdu 5147 Sequence II (树状数组 求逆序数)

    题目链接 Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. SGU180 Inversions(树状数组求逆序数)

    题目: 思路:先离散化数据然后树状数组搞一下求逆序数. 离散化的方法:https://blog.csdn.net/gokou_ruri/article/details/7723378 自己对用树状数组 ...

  4. poj 2299 Ultra-QuickSort(树状数组求逆序数+离散化)

    题目链接:http://poj.org/problem?id=2299 Description In this problem, you have to analyze a particular so ...

  5. HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number                         ...

  6. Codeforces645B【树状数组求逆序数】

    题意: 给你1-n的序列,然后有k次机会的操作,每一次你可以选择两个数交换. 求一个最大的逆序数. 思路: 感觉就是最后一个和第一个交换,然后往中间逼近,到最终的序列,用树状数组求一下逆序数. #in ...

  7. HDU 1394 Minimum Inversion Number(线段树/树状数组求逆序数)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  8. hdu 1394 Minimum Inversion Number (裸树状数组 求逆序数 && 归并排序求逆序数)

    题目链接 题意: 给一个n个数的序列a1, a2, ..., an ,这些数的范围是0-n-1, 可以把前面m个数移动到后面去,形成新序列:a1, a2, ..., an-1, an (where m ...

  9. Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem (树状数组求逆序数 变形)

    题目链接 题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a[j]),求i和j的种类数. 我们可以用map预处理出  ...

随机推荐

  1. Grunt usemin

    yeoman/grunt-usemin 用来将 HTML 文件中(或者 templates/views)中没有优化的 script 和 stylesheets 替换为优化过的版本. usemin 暴露 ...

  2. Spring的IoC容器

    Spring是一个轻量级的Java开发框架,其提供的两大基础功能为IoC和AOP,其中IoC为依赖反转(Inversion of Control).IOC容器的基本理念就是"为别人服务&qu ...

  3. 读书笔记 effective C++ Item 40 明智而谨慎的使用多继承

    1. 多继承的两个阵营 当我们谈论到多继承(MI)的时候,C++委员会被分为两个基本阵营.一个阵营相信如果单继承是好的C++性质,那么多继承肯定会更好.另外一个阵营则争辩道单继承诚然是好的,但多继承太 ...

  4. C语言使用clock进行计时

    #include "stdio.h" #include "stdlib.h" #include "time.h" int main( voi ...

  5. 用java写的一个程序,可以调用windows系统中自带的各种工具,例如截图,便签等

    由于图片资源没有上传,所以运行后不会有图片,感兴趣的同学可以@我,我打包上传, package SmallPrograme; import java.awt.*; import java.awt.ev ...

  6. 一个web应用的诞生(11)--列表分页

    上章的结束,若在实际开发过程中,会发现一个问题,那就首页或关注分享,是一下子按时间顺序全部显示出来,这在实际项目中不可能出现的,想想实际中的产品是如何做的? 一般来说,无非是两种,一种是使用页码,来进 ...

  7. JS实现轻量级计算器

    想尝试做一个网页版计算器后,参考了很多博客大神的代码,综合归纳.总结修改,整理如下文. 附:   Demo    源码 一.HTML+CSS 具体结构样式如下图,基本参照手机计算器界面.按钮功能可以查 ...

  8. 用ElasticSearch搭建自己的搜索和分析引擎

    作者:robben,腾讯高级工程师 商业转载请联系腾讯WeTest获得授权,非商业转载请注明出处. 导语:互联网产品中的检索功能随处可见.当你的项目规模是百度大搜|商搜或者微信公众号搜索这种体量的时候 ...

  9. 解决EditText不能撑满全屏的问题及EditText你应该知道的属性

    一般我们要实现去下图一的效果很简单: 两个EditText就搞定 效果图一:   但是我们想让第二个EditText撑满剩余空间怎么做?如效果图二 效果图二: 解决: 使用了ScrollView嵌套L ...

  10. 使用 Http 的 Get 方式读取网络数据

    作为移动平台的应用,一定避免不了与网络交换数据,不论是读取网页数据,还是调用API接口,都必须掌握Http通信技术 代码如下: package zw1; import java.io.Buffered ...