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. 【转载】【selenium+Python WebDriver】之元素定位

    总结: 感谢: “煜妃”<Selenuim+Python之元素定位总结及实例说明> “Huilaojia123”<selenium WebDriver定位元素学习总结> “上海 ...

  2. JAVA程序开发按位运算的记录

    忘记在哪里看到一个面试题:把int a,b的值互换,不能使用临时变量.刚开始完全懵逼,脑子里面全是浆糊,不知道如何下手.查看答案后猛地一惊,心想居然还有这种操作,真是叹为观止,真的感觉自己的基础是如此 ...

  3. linux memcached php 整合

    http://blog.csdn.net/liruxing1715/article/details/8269563

  4. oracle数据库表格操作

    create table dept--创建表格( deptno number(2) primary key, dname varchar2(9) check(dname=Upper(dname)), ...

  5. C#判断WebService接口是否可用

    using MSXML2; public bool InterfaceEnble() { string url = "http://localhost:81/WebServiceLogin. ...

  6. 【BZOJ2096】[Poi2010]Pilots 双指针+单调队列

    [BZOJ2096][Poi2010]Pilots Description Tz又耍畸形了!!他要当飞行员,他拿到了一个飞行员测试难度序列,他设定了一个难度差的最大值,在序列中他想找到一个最长的子串, ...

  7. When Programmers and Testers Collaborate

    When Programmers and Testers Collaborate Janet Gregory SOMETHING MAGICAL HAPPENS when testers and pr ...

  8. Pentaho BIServer Community Edtion 6.1 使用教程 第四篇 安装和使用Saiku 插件 进行 OLAP

    OLAP(On-Line Analytical Processing,联机分析处理)是一个使分析师.管理者和执行者从原始数据中用来快速.一致.交互访问的一种软件技术,从而真实的反映企业的数据情况.OL ...

  9. long_query_time 设置不生效问题

    由于原来的慢查询日志太大了,有1G多,并且其中包含上一次查询优化前的慢sql,所以想收集最近两天的慢查询语句,故 mysql> show global variables like 'slow% ...

  10. ZOJ - 3861 Valid Pattern Lock 【全排列】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3861 思路 先生成全排列,然后判断哪些情况不符合的,剔除就好了 ...