第二天叫醒我的不是闹钟,是梦想!

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

树状数组:先排序,再求逆序数

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=1e6+10;
const int maxn=1015;
int n,m,k,t;
int vis[N];
int c[N];
typedef long long ll;
struct node
{int a,b;
}a[N];
int lowbit(int x)
{return x&(-x);
}
void add(int i,int x)
{while(i<=maxn){c[i]+=x;i+=lowbit(i);}
}
int sum(int i)
{int res=0;while(i>0){res+=c[i];i-=lowbit(i);}return res;
}bool cmp(const node &a,const node &b)
{if(a.b==b.b) return a.a<b.a;return a.b<b.b;
}
int main()
{scanf("%d",&t);int p=0;while(t--){memset(c,0,sizeof c);cin>>n>>m>>k;for(int i=1;i<=k;i++){scanf("%d %d",&a[i].a,&a[i].b);}sort(a+1,a+1+k,cmp);ll ans=0;for(int i=1;i<=k;i++){ans=ans+sum(maxn)-sum(a[i].a);add(a[i].a,1);}printf("Test case %d: %lld\n",++p,ans);}return 0;
}

POJ - 3067相关推荐

  1. 树状数组 之 poj 3067

    这道题和 poj 2481 极其相似...但wa了几次... 原因: 1)对于结果和存储树状数组求和的变量,采用 long long 来保存: 2)从东海岸 number x,到西海岸 number ...

  2. poj 3067 Japan

    Japan 题目链接:点击打开链接 解题思路:自己本人也是刚刚接触树状数组,并且也知道树状数组可以求逆序数,但是第一眼还是看不透这个题能用逆序数. 首先,这个题有个规律,凡是能有交叉的线,都有一个规律 ...

  3. poj 3067 树状数组

    这题的关键是方法.怎么才是相交的呢?一种方法是线段a的左边比线段b的左边小,同时a的右边比b的右边大.这时就可以构成一个相交.于是,我们可以根据左边的值来做降序排列,然后根据线段树来统计crosses ...

  4. Japan POJ - 3067

    题目链接 题意就是: 在大海的西岸和东岸分别有沿着南北方向的城市,给出了k条从东岸到西岸的路,求这些路的交点. 线段树,挺基础的吧. 挺水的吧. AC   code #include<iostr ...

  5. POJ 3067 Japan【树状数组】

    题目 Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. ...

  6. POJ 2481 Cows

    传送门:POJ 2481 Cows Time Limit: 3000MS Memory Limit: 65536K Problem Description Farmer John's cows hav ...

  7. 【转载】树状数组题目

    先提个注意点,由于Lowbit(0) = 0,这会导致x递增的那条路径发生死循环,所有当树状数组中可能出现0时,我们都全部加一,这样可以避免0带来的麻烦-- 简单:       POJ 2299 Ul ...

  8. 如此好的树状数组学习资料

    树状数组学习系列1 之 初步分析--czyuan原创 其实学树状数组说白了就是看那张图,那张树状数组和一般数组的关系的,看懂了基本就没问题了,推荐下面这个教程:http://www.topcoder. ...

  9. 浅谈线段树(Segment Tree)

    线段树的概念与性质 线段树首先是一棵树,而且是二叉树.树上的每个节点对应于一个区间[a,b],a,b通常为整数.同一层的节点所代表的区间,互相不重叠.并且同一层的区间加起来是连续的区间,叶子节点的区间 ...

最新文章

  1. html文字中横线_谈PPT课件中自定义动画应用之内容控制
  2. 【Splay】洛谷3372 【模板】线段树 1
  3. Java 并发编程——Executor框架和线程池原理
  4. 史上最强翻译器,没有之一,不接受反驳
  5. [TODO]Python拾遗(二)
  6. 每日一笑 | 为什么椅子总是最乱的?
  7. 三星系统和鸿蒙系统,又一设备直升鸿蒙系统,现有操作系统被抛弃,和三星的想法一样!...
  8. ffmpeg文档3:播放声音
  9. 更改自身web项目的图标(默认为tomcat的小喵咪)
  10. 翟东平微信公众号视频教程JAVA201810版3/3阶段-翟东平-专题视频课程
  11. 白塞尔公式_如何设计像乌塞尔这样的800人的婚礼
  12. HTML将某几个文字添加下划线 并标红
  13. 51单片机驱动ds12887c语言,DS12887(时钟日历芯片) c语言驱动程序
  14. 10款开发常用的代码编辑器
  15. KittenCode编程平台
  16. 55 - 字符流中第一个不反复的字符
  17. HTTP_REFERER的用法及伪造及去掉REFERER方法
  18. 大前端relative和absolute详解
  19. 软件专利交底书到底应该怎么写?
  20. 5个最佳React动画库

热门文章

  1. Java图形化GUI界面
  2. SQLI DUMB SERIES-4
  3. 剑网3一直连接不上服务器,win10系统剑网3连不上服务器的解决方案
  4. 上周AI热点回顾:AI“模拟”出暗物质、AI挖掘毕加索秘密、CPU在大型神经网络超越V100 GPU...
  5. oracle ebcdic 转换,使用sqlldr导入EBCDIC格式数据并新增Oracle字符集
  6. 无人机开发-图传技术浅析
  7. 湿寒阳虚体质如何艾灸
  8. 关闭msmpeng_关闭Windows Defender与性能提升测试
  9. 【uView】picker 地区选择
  10. 通过Nginx访问静态页面