poj 3067 Japan(树状数组)
2015-08-04 12:08
225 查看
Japan
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
树状数组第三题。
这道题可以先排序,x升序,x相同的时候,y升序
然后求y中的逆序对数。
大概知道了怎么用树状数组求逆序对。
[b]下标(从0开始)-比a[i]小的=a[i]与之前形成的逆序对数。[/b]
具体见代码注释
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 23111 | Accepted: 6232 |
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
树状数组第三题。
这道题可以先排序,x升序,x相同的时候,y升序
然后求y中的逆序对数。
大概知道了怎么用树状数组求逆序对。
[b]下标(从0开始)-比a[i]小的=a[i]与之前形成的逆序对数。[/b]
具体见代码注释
/************************************************************************* > File Name: code/poj/3067.cpp > Author: 111qqz > Email: rkz2013@126.com > Created Time: 2015年08月04日 星期二 10时49分06秒 ************************************************************************/ #include<iostream> #include<iomanip> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<string> #include<map> #include<set> #include<queue> #include<vector> #include<stack> #define y0 abc111qqz #define y1 hust111qqz #define yn hez111qqz #define j1 cute111qqz #define tm crazy111qqz #define lr dying111qqz using namespace std; #define REP(i, n) for (int i=0;i<int(n);++i) typedef long long LL; typedef unsigned long long ULL; const int inf = 0x7fffffff; const int N=1E3+5; int n,m,k; int t[N*N/2]; struct R { int x,y; }r[N*N/2]; bool cmp(R a,R b) { if (a.x<b.x) return true; if (a.x==b.x&&a.y<b.y) return true; return false; } int lowbit(int x) { return x&(-x); } void update (int x,int c) { for ( int i = x ; i < N ; i = i + lowbit(i)) { t[i] = t[i] + c; } } LL sum ( int x) { int res = 0 ; for ( int i = x ; i >= 1 ; i = i - lowbit(i)) { res = res + t[i]; } return res; } int main() { int T; cin>>T; LL ans ; int cas = 0; while (T--) { memset(t,0,sizeof(t)); memset(r,0,sizeof(r)); ans = 0 ; cas++; scanf("%d %d %d",&n,&m,&k); for ( int i = 0 ; i < k ; i ++ ) { scanf("%d %d",&r[i].x,&r[i].y); } sort(r,r+k,cmp); for ( int i = 0 ; i < k; i ++ ) //起点终点相同不算crossing { // update(r[i].x,1); // if (r[i].x==r[i-1].x||r[i].y==r[i-1].y) continue; ans = ans +i- sum(r[i].y); //求y的逆序对数,好好理解这行代码! // cout<<"ans:"<<ans<<endl; update(r[i].y,1); } printf("Test case %d: %lld\n",cas,ans); } return 0; }
相关文章推荐
- TCP套接字编程—具体流程梳理
- Linphone 网络电话二次开发
- 智捷课堂改版啦!
- Web 通信 之 长连接、长轮询(long polling)
- 按钮点击事件,打开新的Activity
- 关于虚拟字符设备驱动的学习笔记globalmem
- POJ 3281【拆点 && 最大流经典建图】
- 当页面关闭或刷新时提示用户
- C# 使用TimeSpan计算两个时间差
- C#将隐私信息(银行账户,身份证号码)中间部分特殊字符替换成*
- 详解Ruby on Rails中的Cucumber使用
- 黑马程序员-----OC学习之内存管理
- linq用法
- cloudstack_template_snapshot_volume
- [Ptrace]Linux内存替换(三)运行控制
- 初学者很实用:数据库连接池之c3p0入门
- Virtualbox虚拟机网络配置(NAT + Host-only - Bridged)
- iOS中NSArray, NSSet, NSDictionary的使用
- Codeforces 148 D Bag of mice(概率DP)
- Couldn't get lock for %t/vertx.log