Japan
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 24151   Accepted: 6535

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

1
3 4 4
1 4
2 3
3 2
3 1

Sample Output

Test case 1: 5
题意:两列数字连线,统计线段交叉的产生的点数(交于同一个数字的不算)。两线段相交等价于两端点的值一大一小,一小一大。
#include <cstdio>
#include <string.h>
#include <algorithm>
using namespace std;
const int MAXN=;
struct Node{
int east,west;
}road[MAXN];
int k;
int bit[MAXN];
bool comp(Node no1,Node no2)
{
if(no1.east!=no2.east)
{
return no1.east < no2.east;
}
else
{
return no1.west < no2.west;
}
}
void add(int i,int x)
{
while(i<MAXN)
{
bit[i]+=x;
i+=(i&-i);
}
}
int sum(int i)
{
int s=;
while(i>)
{
s+=bit[i];
i-=(i&-i);
}
return s;
}
int main()
{
int T;
scanf("%d",&T);
for(int cas=;cas<=T;cas++)
{
memset(bit,,sizeof(bit));
scanf("%*d%*d%d",&k);
for(int i=;i<k;i++)
{
scanf("%d",&road[i].east);
scanf("%d",&road[i].west);
}
sort(road,road+k,comp);
long long res=;
for(int i=k-;i>=;i--)
{
res+=sum(road[i].west-);//注意-1,交于同一点不计crossing
add(road[i].west,);
}
printf("Test case %d: %lld\n",cas,res);
} return ;
}

POJ3067(树状数组:统计数字出现个数)的更多相关文章

  1. POJ2481(树状数组:统计数字 出现个数)

    Cows Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 15405   Accepted: 5133 Description ...

  2. POJ3928(树状数组:统计数字出现个数)

    Ping pong Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2641   Accepted: 978 Descript ...

  3. HDU 5997 rausen loves cakes(启发式合并 + 树状数组统计答案)

    题目链接  rausen loves cakes 题意  给出一个序列和若干次修改和查询.修改为把序列中所有颜色为$x$的修改为$y$, 查询为询问当前$[x, y]$对应的区间中有多少连续颜色段. ...

  4. poj Ping pong LA 4329 (树状数组统计数目)

    Ping pong Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2302   Accepted: 879 Descript ...

  5. poj3067树状数组求逆序数

    Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Jap ...

  6. [bzoj1901][zoj2112][Dynamic Rankings] (整体二分+树状数组 or 动态开点线段树 or 主席树)

    Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has ...

  7. HDU 4417 - Super Mario ( 划分树+二分 / 树状数组+离线处理+离散化)

    题意:给一个数组,每次询问输出在区间[L,R]之间小于H的数字的个数. 此题可以使用划分树在线解决. 划分树可以快速查询区间第K小个数字.逆向思考,判断小于H的最大的一个数字是区间第几小数,即是答案. ...

  8. BZOJ 2683: 简单题(CDQ分治 + 树状数组)

    BZOJ2683: 简单题(CDQ分治 + 树状数组) 题意: 你有一个\(N*N\)的棋盘,每个格子内有一个整数,初始时的时候全部为\(0\),现在需要维护两种操作: 命令 参数限制 内容 \(1\ ...

  9. FZOJ 2245 动态树(离散+离线+ 树状数组)

    Problem 2245 动态树 Accept: 17    Submit: 82Time Limit: 3000 mSec    Memory Limit : 65536 KB  Problem D ...

随机推荐

  1. iPhone,iPad如何获取WIFI名称即SSID

    本文转载至 http://blog.csdn.net/wbw1985/article/details/20530281  2010年开始苹果清理了一批APP Store上的WIFI扫描软件, 缘由语焉 ...

  2. python 基础 1.4 python运算符

    一. 布尔值: 1>True 2>False       二.关系运算符 “=” (a=b):把b的值赋给a.等号赋值   “==”(a==b): 判断a与b是否相等.返回Trule或Fl ...

  3. windowsphone8.1学习笔记之应用数据(三)

    之前说了如何操作文本文件,如果是图片文件或者其他的二进制文件则需要操作文件的Stream或者Buffer数据.就需要用到DataReader和DataWriter这两个类了,这个的好好的练一下,以后的 ...

  4. restlet验证

    1 restlet有无认证对比 无认证: 客户端发起请求 -----> 服务器路由 -----> 访问服务端资源 有认证: 客户端发起请求 -----> 认证 ----->服务 ...

  5. mysql 查看或者修改数据库密码

    首先启动命令行 1.在命令行运行:taskkill /f /im mysqld-nt.exe 下面的操作是操作mysql中bin目录下的一些程序,如果没有配置环境变量的话,需要切换到mysql的bin ...

  6. Django的模型层(2)---多表操作

    多表操作 创建模型 实例:我们来假定下面这些概念,字段和关系 作者模型:一个作者有姓名和年龄. 作者详细模型:把作者的详情放到详情表,包含生日,手机号,家庭住址等信息.作者详情模型和作者模型之间是一对 ...

  7. Mac平台下的抓包神器 —— Charles

    在开发界,“抓包”这个词想必大家耳熟能详.通过抓包工具,能够获取设备在网络通讯过程中的交换数据包.在 Windows 平台上,笔者使用较多的是 Fiddler 工具,但是由于 Fiddle 使用 C# ...

  8. redhat5.1上安装oracle 10.2g客户端及配置使用

    一)安装 1.命令 rpm -q gcc make binutils setarch compat-db compat-gcc compat-gcc-c++ compat-libstdc++ comp ...

  9. SPOJ - GSS1 —— 线段树 (结点信息合并)

    题目链接:https://vjudge.net/problem/SPOJ-GSS1 GSS1 - Can you answer these queries I #tree You are given ...

  10. ASP.NET 4.0 页面 ValidateRequest="false" 失效不起作用

    当ASP.NET 2.0升级到 ASP.NET 4.0后,页面的 ValidateRequest="false" 不起作用. 因为 ASP.NET 4.0 请求验证被提前到IHtt ...