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. LRU java实现

    实现LRU缓存,用到了一个链表和 HashMap, HashMap保证了get/set的时间复杂度是O(1), 链表用来记录 最近最少使用的元素,以便用来淘汰. package lru; /** * ...

  2. iOS中数组遍历的方法及比較

    数组遍历是编码中非经常见的一种需求.我们来扒一拔iOS里面都有什么样的方法来实现,有什么特点. 由于iOS是兼容C语言的.所以C语言里面的最最常见的for循环遍历是没有问题的. 本文中用的数组是获取的 ...

  3. CI去掉 URL 中的 index.php

    首先,你要清楚自己的 Web 服务器是 Apache,支持 mod_rewrite 查找httpd.conf中是否开启了mod_rewrite.so 然后,在 CI 根目录下新建立一个配置文件,命名为 ...

  4. create-react-app创建项目报错SyntaxError: Unexpected end of JSON input while parsing near '...ttachment":false,"tar' npm代理

    SyntaxError: Unexpected end of JSON input while parsing near '...ttachment":false,"tar' 错误 ...

  5. zookeeper启动流程简单梳理

    等着測试童鞋完工,顺便里了下zookeeper的启动流程 zk3.4.6 启动脚本里面 nohup "$JAVA" "-Dzookeeper.log.dir=${ZOO_ ...

  6. Java 集成域登陆

    参考: 1. JAVA中使用jcifs集成AD域用户认证 2. https://liaosy.iteye.com/blog/1887092 3.其他库 https://github.com/Waffl ...

  7. pycharm连git和gitee

    http://www.cnblogs.com/feixuelove1009/p/5955332.html https://www.58jb.com/html/171.html

  8. t60替换alt,super,ctrl

    发现T60的左边在ctrl 与 alt 有个win 键,所以就进行了映射 网上有一个把alt->ctrl, super-> alt, ctrl->super的script, 见 ht ...

  9. cmake使用第三方库

    1 link_directories和target_link_libraries 1.1 link_directories 告诉linker去这些目录去找library. 1.2 target_lin ...

  10. Objective-c中的delegate浅析

    delegate初探 在ios开发中,我们常常会用到类似例如以下的对话框: 因此,例如以下这段代码我们也就非常熟悉了: - (IBAction)showSheet:(id)sender { UIAct ...