题目

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.

题目大意

T组数据,每一组数据都是左右两边有若干个城市,现在需要在左右两边各取一座城市修建公路,城市按照序号大小顺序排列,两边的城市均在[1,1000]之间,输出最后公路的交界点数目。

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

分析

由分析可以知道,当点对为(1,2),(2,3),(3,4)时,并没有产生交界点。只有(1,3),(2,1)这样的情况时产生了交界点。在这里,x的数据1,2按照升序,但是y的数据3,1则是降序,则计算交界点的数目问题变成了求取逆序对数目的问题,按照x升序的顺序去输入。城市按照x,y的点对形式给出,城市x在左,y在右,那么,我们可以先将其按照x升序排列,x相等时,再按照y升序排列。
计算y对应的前缀和获得非逆序数目,并更新该y对应的值。可采用树状数组进行处理。
树状数组

代码

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int a[1010];
int n,m;
struct E{int x,y;
}edge[1000010];
bool cmp(const E a,const E b){//x优先,y其次升序排列if(a.x<b.x) return true;else if(a.x==b.x) return a.y<b.y;return false;
}
int lowbit(int x){return (-x)&x;
}
void add(int i){//点更新while(i<=m){a[i]++;i+=lowbit(i);}
}
int getsum(int i){//查询前缀和int ans = 0;while(i>0){ans+=a[i];i-=lowbit(i);}return ans;
}
int main(){int t,k;scanf("%d",&t);for(int i= 1;i<=t;++i){scanf("%d %d %d",&n,&m,&k);for(int j= 0;j<k;++j) scanf("%d %d",&edge[j].x,&edge[j].y);sort(edge,edge+k,cmp);//排序long long ans = 0;for(int j = 0;j<k;++j){ans+=j-getsum(edge[j].y);//getsum计算出前缀和为非逆序数目add(edge[j].y);//按照x升序的顺序更新数据}printf("Test case %d: %lld\n",i,ans);memset(a,0,sizeof a);}return 0;
}

POJ 3067 Japan【树状数组】相关推荐

  1. Japan树状数组求逆序数

    http://poj.org/problem?id=3067 给出一个序列 a1 a2 a3 ···an 问此序列中逆序对的个数? 逆序对:i<j,ai>aj 方法:就是用树状数组搞,从后 ...

  2. POJ 2481 Cows POJ 2352 Stars(树状数组妙用)

    题目链接:POJ 2481 Cows POJ 2352 Stars 发现这两个题目都跟求逆序数有着异曲同工之妙,通过向树状数组中插入点的位置,赋值为1,或者++,然后通过求和来判断比当前 点 &quo ...

  3. POJ 2299 Ultra-QuickSort(树状数组 + 离散)

    链接:http://poj.org/problem?id=2299 题意:给出N个数组成的数列A(0 <= A[i] <= 999,999,999),求该数列逆序对的数量. 分析:题目所谓 ...

  4. POJ 2299 Ultra-QuickSort(树状数组+离散化)

    题目大意: 就是说,给你一个序列,然后让你求出这个序列有多少个逆序对,所谓逆序对就是对于这个序列中的元素有a[i]>a[j] 且i<j存在. 其实原题是这样说的,给你一个序列,让你用最少的 ...

  5. POJ 1990 (树状数组入门)

    MooFest Every year, Farmer John's N (1 <= N <= 20,000) cows attend "MooFest",a socia ...

  6. 树状数组 之 poj 3067

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

  7. poj 2352 Stars 线段树(先建后查/边建边查)/树状数组三种方法思路详解,带你深入了解线段树难度⭐⭐⭐★

    poj 2352 Stars 目录 poj 2352 Stars 1.树状数组 2.线段树,先建树后查找 3.线段树,边建树边查找 Description Astronomers often exam ...

  8. poj 3321 Apple Tree(dfs序+树状数组求和模型)

    题目链接:http://poj.org/problem?id=3321 解题思路: 先dfs求出序列,将子树转化到dfs序列的区间内,接下来就是简单的树状数组求和模型了.水题. #include< ...

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

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

最新文章

  1. 洛谷P1919 【模板】A*B Problem升级版(FFT)
  2. mxnet转pytorch预训练
  3. opencv 图像雾检测_OpenCV图像处理-基于OpenPose的关键点检测
  4. Visual Basic 9.0 前沿播报·静态篇(一)局部变量类型推测和数组初始化器
  5. hive 插入数据映射到hbase_大数据基础知识:Hadoop分布式系统介绍
  6. 超详细图解!【MySQL进阶篇】存储过程,视图,索引,函数,触发器
  7. 【LeetCode】【HOT】105. 从前序与中序遍历序列构造二叉树(哈希表+递归)
  8. SQL数据库语言基础之SqlServer数据表的六大约束(主键、外键、检查、非空、唯一性、默认值约束)的创建
  9. SFP光模块与SFP+、XFP、QSFP、GBIC、BIDI的区别
  10. 机器学习方法三要素理解:模型、策略、算法
  11. 阿里云服务器docker安装网心云容器魔方
  12. erp中三大订单CO、PO、MO各是代表什么?
  13. em在聊天中是什么意思_emmmm是什么意思 聊天中emmmm是什么意思什么梗
  14. 数据抓取软件是如何抓取数据的?
  15. 通过OpenSSL解析X509证书基本项
  16. 本题要求编写程序,对输入的一个整数,从高位开始逐位分割并输出它的各位数字。输入格式:输入在一行中给出一个长整型范围内的非负整数。输出格式:从高位开始逐位输出该整数的各位数字,每个数字后面有一个空格
  17. 二叉树:表达式二叉树转换成中缀式(括弧处理)
  18. Vue双向绑定:原理篇(详细)
  19. c51抢答器程序汇编语言,c51单片机汇编语言单片机八位抢答器程序
  20. 干货笔记,数据仓库工具箱

热门文章

  1. 赛轮转债上市价格预测
  2. 基于51单片机的智能汽车雨刮器的程序设计proteus仿真
  3. 2020国开c语言程序设计1075,中央电大秋季C语言程序设计期末试卷及答案代码1075,01(7页)-原创力文档...
  4. 怎么用几何画板制作图形平移和旋转
  5. VBA锁定单元格并记录单元格修改日志无bug篇
  6. 你不可不知的《哈利波特》秘密(五)
  7. java 发送邮件添加附件,Java实现带附件的邮件发送功能
  8. 【knex】 knex.js中 orderBy多个字段排序
  9. ROS下利用realsense采集RGBD图像合成点云
  10. 深圳南山区的篮球场大全