POJ3067:Japan(树状数组求逆序对)
2015-06-23 21:38
337 查看
Description
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
Sample Output
Source
Southeastern Europe 2006
题意:
两边分别有n,m个点,每边点的编号从上到下位1,2,3。。。
现在有K条边连接两边的点,最多只有两条边相交于一点而不会出现三条边以上交于一点的状况,问一共有几个交点
思路:
相交的情况必然是在在Li<Lj的情况下,Ri>Rj
所以在对L,R排序之后,只需要看R里面逆序数的多少即可
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
Source
Southeastern Europe 2006
题意:
两边分别有n,m个点,每边点的编号从上到下位1,2,3。。。
现在有K条边连接两边的点,最多只有两条边相交于一点而不会出现三条边以上交于一点的状况,问一共有几个交点
思路:
相交的情况必然是在在Li<Lj的情况下,Ri>Rj
所以在对L,R排序之后,只需要看R里面逆序数的多少即可
#include <iostream> #include <stdio.h> #include <string.h> #include <string> #include <stack> #include <queue> #include <map> #include <set> #include <vector> #include <math.h> #include <bitset> #include <list> #include <algorithm> #include <climits> using namespace std; #define lson 2*i #define rson 2*i+1 #define LS l,mid,lson #define RS mid+1,r,rson #define UP(i,x,y) for(i=x;i<=y;i++) #define DOWN(i,x,y) for(i=x;i>=y;i--) #define MEM(a,x) memset(a,x,sizeof(a)) #define W(a) while(a) #define gcd(a,b) __gcd(a,b) #define LL long long #define N (1005*1005) #define INF 0x3f3f3f3f #define EXP 1e-8 #define lowbit(x) (x&-x) const int mod = 1e9+7; struct node { int l,r,id; } a ; int n,c ,x,y; int cmp(node a,node b) { if(a.l!=b.l) return a.l<b.l; return a.r<b.r; } int sum(int x) { int ret = 0; while(x>0) { ret+=c[x]; x-=lowbit(x); } return ret; } void add(int x,int d) { while(x<=y) { c[x]+=d; x+=lowbit(x); } } LL ans; int main() { int i,j,k,l,r,t,cas = 1; scanf("%d",&t); while(t--) { scanf("%d%d%d",&x,&y,&n); MEM(c,0); for(i = 1; i<=n; i++) { scanf("%d%d",&a[i].l,&a[i].r); } sort(a+1,a+1+n,cmp); ans = 0; for(i = 1; i<=n; i++) { add(a[i].r,1); ans+=sum(y)-sum(a[i].r); } printf("Test case %d: %lld\n",cas++,ans); } return 0; }
相关文章推荐
- leetcode-52-N-Queens II
- 使用xtrabackup备份恢复Mariadb数据库
- Java多线程 小知识 守护线程(后台线程)setDaemon、join方法、优先级、yield方法
- nefu 520
- 火狐的旺旺和支付宝控件总是需要反复点击确认?
- 基于卷积神经网络的数据预处理以及模型参数保存
- self.view.frame.size.height = 100f; 这样写没法通过编译,编译器会报错"expression is not assignable"
- [MCS-51] 指令系统
- linux基础3——与XP共享文件夹的设置
- Android Looper.class
- Android开发之Intent
- iOS开发学习第二十二课——内存管理
- UIEdgeInsetsMake 和 UIEdgeInsetsMake
- MySQL使用需要关注编码问题
- PreparedStatement不能用来设置表名、字段名
- linux 下zabbix 监控简单配置
- 2015/6/23 浪潮过去,我才来
- wordpress学习二:源码目录结构和启动流程
- 学期项目类图
- ubuntu安装openssh-server 报依赖错误的解决过程