cdoj 383 japan 树状数组
Japan
Time Limit: 1 Sec
Memory Limit: 256 MB
题目连接
http://acm.uestc.edu.cn/#/problem/show/383
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 (1≤M≤10000, 1≤N≤10000). K superhighways will be build.(1≤K≤1000000) 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
HINT
题意
左边有n个城市,右边有m个城市,然后连了k条线,问你一共有多少个交点
题解:
这种题一般和逆序数有关,于是就逆序数咯
排序之后,保证序列
代码:
//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <bitset>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200500
#define mod 1001
#define eps 1e-9
#define pi 3.1415926
int Num;
//const int inf=0x7fffffff;
const ll inf=;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************* int d[maxn];
int N = ;
struct node
{
int x,y;
};
bool cmp(node a,node b)
{
if(a.x==b.x)
return a.y>b.y;
return a.x>b.x;
}
node a[maxn];
int lowbit(int x){return x&(-x);}
void updata(int x)
{
while(x<=N)
{
d[x]+=;
x+=lowbit(x);
}
}
int sum(int x)
{
int ans = ;
while(x)
{
ans+=d[x];
x -= lowbit(x);
}
return ans;
}
int n,m,k;
int main()
{
int t=read();
for(int cas=;cas<=t;cas++)
{
memset(a,,sizeof(a));
memset(d,,sizeof(d));
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=k;i++)
a[i].x=read(),a[i].y=read();
sort(a+,a+k+,cmp);
int ans = ;
for(int i=;i<=k;i++)
{
ans+=sum(a[i].y-);
updata(a[i].y);
}
printf("Test case %d: %d\n",cas,ans); }
}
cdoj 383 japan 树状数组的更多相关文章
- poj3067 Japan(树状数组)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:id=3067">http://poj.org/problem? id=3067 Descri ...
- poj 3067 - Japan(树状数组)
先按第一个数从大到小排序,相等的情况下,第二个数按照从大到小排序..... 预处理后,照着树状数组写就行了... 注意:k的最大值应取1000*1000 代码如下: include <cstdi ...
- poj3067 Japan 树状数组求逆序对
题目链接:http://poj.org/problem?id=3067 题目就是让我们求连线后交点的个数 很容易想到将左端点从小到大排序,如果左端点相同则右端点从小到大排序 那么答案即为逆序对的个数 ...
- CDOJ 838 母仪天下 树状数组 (2014数据结构专题
母仪天下 Time Limit: 1 Sec Memory Limit: 162 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/838 Descrip ...
- POJ 3067 Japan 树状数组求逆序对
题目大意:有两排城市,这两排城市之间有一些路相互连接着,求有多少条路相互交叉. 思路:把全部的路先依照x值从小到大排序,x值同样的依照y值从小到大排序,然后插入边的时候,先找有多少比自己y值小的,这些 ...
- POJ 3067 Japan (树状数组 && 控制变量)
题意: 西海岸和东海岸有分别有n (1~n)个和m (1~m)个城市, 两个海岸的城市之间有k条公路连通, 公路会相交, 现在给出城市和公路的信息问你由这些公路组成的复杂交通有多少个交点 (如果两个条 ...
- POJ 3067 Japan (树状数组求逆序对)
POJ - 3067 题意:有(1-n)个城市自上到下在左边, 另有(1-m)个城市自上到下在右边,共有m条高速公路,现求这m条直线的交点个数,交点不包括在城市处相交. 题解:先将高速公路读入,然后按 ...
- POJ 3067 Japan(树状数组)
Japan Time Limit: 10 ...
- POJ 3067 - Japan - [归并排序/树状数组(BIT)求逆序对]
Time Limit: 1000MS Memory Limit: 65536K Description Japan plans to welcome the ACM ICPC World Finals ...
随机推荐
- Eclipse导入项目
导入Eclipse项目 File->Import...->Existing Projects into Workspace->Next->Browse...->Finis ...
- js生成二维码参数设置
jquery qrcode使用方法 $(selector).qrcode({ width: 100, height: 100, color: '#3a3', text: 'http://larsjun ...
- 史上最全面的FRM与CFA的区别对比分析,适合新人看
简单地自我介绍:本人于纽约完成了研究生阶段的学习后,在华尔街混迹了几年的时间,已获取FRM证书,正在积极准备CFA. 上海财经大学FRM培训中心前言导读 经常看到CFA持证人平均年收入为$XXX之类的 ...
- poj2823Sliding Window(线段树求最值)
链接 裸线段树 这题时间卡的挺棒 #include <iostream> #include<cstdio> #include<cstring> #include&l ...
- extends:类似于java中的继承特征,extends="struts-default"
extends:类似于java中的继承特征,extends="struts-default"就是继承struts-default.xml,它里面定义了许多跳转类型.拦截器等一些常用 ...
- [原]Unity3D深入浅出 - 光源组件(Light)
Unity中提供了四种光源: Directional light: 方向光,类似太阳的日照效果. Point light: 点光源,类似蜡烛. Spotlight: 聚光灯,类似手电筒. Area L ...
- spring-security用户权限认证框架
大家知道在spring中有一个基于acegi开发的spring-security的权限管理模块,它是一个轻量级框架. SpringSecurity能以声明的方式来保护Web应用程序的URL访问,只需简 ...
- 【转】iOS中16进制转10进制
原文网址:http://www.voidcn.com/blog/u012198553/article/p-4976772.html /// 将十六进制的字符串转化为NSData - (NSData ) ...
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.4.5
Suppose it is known that $\scrM$ is an invariant subspace for $A$. What invariant subspaces for $A\o ...
- UPC OJ 一道水题 STL
Problem C: 字符串游戏 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 10 Solved: 3 [Submit][Status][Web ...