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 树状数组的更多相关文章

  1. poj3067 Japan(树状数组)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:id=3067">http://poj.org/problem? id=3067 Descri ...

  2. poj 3067 - Japan(树状数组)

    先按第一个数从大到小排序,相等的情况下,第二个数按照从大到小排序..... 预处理后,照着树状数组写就行了... 注意:k的最大值应取1000*1000 代码如下: include <cstdi ...

  3. poj3067 Japan 树状数组求逆序对

    题目链接:http://poj.org/problem?id=3067 题目就是让我们求连线后交点的个数 很容易想到将左端点从小到大排序,如果左端点相同则右端点从小到大排序 那么答案即为逆序对的个数 ...

  4. CDOJ 838 母仪天下 树状数组 (2014数据结构专题

    母仪天下 Time Limit: 1 Sec  Memory Limit: 162 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/838 Descrip ...

  5. POJ 3067 Japan 树状数组求逆序对

    题目大意:有两排城市,这两排城市之间有一些路相互连接着,求有多少条路相互交叉. 思路:把全部的路先依照x值从小到大排序,x值同样的依照y值从小到大排序,然后插入边的时候,先找有多少比自己y值小的,这些 ...

  6. POJ 3067 Japan (树状数组 && 控制变量)

    题意: 西海岸和东海岸有分别有n (1~n)个和m (1~m)个城市, 两个海岸的城市之间有k条公路连通, 公路会相交, 现在给出城市和公路的信息问你由这些公路组成的复杂交通有多少个交点 (如果两个条 ...

  7. POJ 3067 Japan (树状数组求逆序对)

    POJ - 3067 题意:有(1-n)个城市自上到下在左边, 另有(1-m)个城市自上到下在右边,共有m条高速公路,现求这m条直线的交点个数,交点不包括在城市处相交. 题解:先将高速公路读入,然后按 ...

  8. POJ 3067 Japan(树状数组)

                                                                                  Japan   Time Limit: 10 ...

  9. POJ 3067 - Japan - [归并排序/树状数组(BIT)求逆序对]

    Time Limit: 1000MS Memory Limit: 65536K Description Japan plans to welcome the ACM ICPC World Finals ...

随机推荐

  1. IT项目量化管理:细化、量化与图形化 与 中国IT项目实施困惑

    IT项目开发和实施的组织先后在组织中引入项目管理模型的管理制度.流程和方法,但收入甚微.大量的IT项目依然面临着无休止的需求蔓延与频繁加班.项目工期失控.质量低下等典型的项目失控现象.对项目引入量化意 ...

  2. Android横竖屏切换总结

    Android横竖屏切换总结(Android资料) Android横竖屏要解决的问题应该就两个: 一.布局问题 二.重新载入问题 1.布局问题:如果不想让软件在横竖屏之间切换,最简单的办法就是在项目的 ...

  3. Andriod x86 4,4

    用 vbox 装了下Andriod x86 4,4 ,能用的App真心少,反正我只是用来看看安卓浏览器里网站效果,海豚浏览器还有qq浏览器x86版,还能用用,主要要换UA为iphone和ipad之类的 ...

  4. bzoj1855

    让我们继续练习dp 首先这道题约束条件很多 但实际上方程还是很好写的,f[i,j]表示第i天时拥有j只股票的最大收益 令p=max(0,i-k-1) 上一次较交易 易得f[i,j]=max(f[i-1 ...

  5. BZOJ2500: 幸福的道路

    题解: 一道不错的题目. 树DP可以求出从每个点出发的最长链,复杂度O(n) 然后就变成找一个数列里最长的连续区间使得最大值-最小值<=m了. 成了这题:http://www.cnblogs.c ...

  6. vijos1603迷宫

    这题的构思太巧妙了: 经典题目8 给定一个有向图,问从A点恰好走k步(允许重复经过边)到达B点的方案数mod p的值    把给定的图转为邻接矩阵,即A(i,j)=1当且仅当存在一条边i->j. ...

  7. ZBreak

    https://github.com/atskyline/ZBreak 最近用电脑用的多,总觉得有必要2个小时休息一会.就花了一点点时间写了这个小东西如果连续使用电脑超过2个小时会弹出一个窗口提示. ...

  8. Kernel 中的 GPIO 定义和控制

    最近要深一步用到GPIO口控制,写个博客记录下Kernel层的GPIO学习过程! 一.概念 General Purpose Input Output (通用输入/输出)简称为GPIO,或 总线扩展器. ...

  9. ARM Linux系统的时钟机制

    1. Linux下有两类时钟: 1.1 实时钟RTC 它由板上电池驱动的“Real Time Clock”也叫做RTC或者叫CMOS时钟,硬件时钟.当操作系统关机的时候,用这个来记录时间,但是对于运行 ...

  10. 关于SQL中的Update语句

    今天在SQL数据库操作时需要将一张表中的数据Update到另一张表中去, 可是用我以往的写法确怎么也不能成功.代码如下: update table1 a set a.Col1=b.Col2 from ...