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

求交叉的点数
满足交叉的条件是si<sj&&ei>ej || si>sj&&ei<ej
排好序后就是求终点逆序数了。能够用线段树实现
#include<stdio.h>
#include<string.h>
#include <algorithm>
using namespace std; #define N 1010
#define M 1000010
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
int T,n,m,k;
int sum[N<<2],a[N];
__int64 ans; struct node
{
int x;
int y;
} s[M]; int cmp(node a,node b)
{
if(a.y!=b.y)
return a.y>b.y;
return a.x<b.x;
} void Pushup(int rt)
{
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
} void Update(int rt,int l,int r,int x)
{
if(l == r)
{
sum[rt]++;
a[l]++;
return ;
}
int mid = (l + r) >> 1;
if(x <= mid)
Update(lson,x);
else
Update(rson,x);
Pushup(rt);
} int Query(int rt,int l,int r,int L,int R)
{
if(L <= l && R >= r)
{
return sum[rt];
}
int mid= (l + r) >> 1;
int res= 0;
if(L <= mid) res += Query(lson,L,R);
if(R > mid ) res += Query(rson,L,R);
return res;
} int main()
{
int i,j,res,cas = 1;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&k);
memset(sum,0,sizeof(sum));
memset(a,0,sizeof(a));
ans = 0;
res = 0;
for(i = 1; i <= k; ++i)
scanf("%d %d",&s[i].x,&s[i].y);
sort(s+1,s+1+k,cmp);
for(i = 1; i <= k; ++i)
{
int tmp = s[i].y;
if(i>1 && s[i].y == s[i-1].y)
res++;
else
res = 0;
ans += Query(1,1,n,1,s[i].x)-a[s[i].x]-res;
Update(1,1,n,s[i].x);
}
printf("Test case %d: %I64d\n",cas++,ans);
}
return 0;
}

POJ3067:Japan(线段树)的更多相关文章

  1. K - Japan(线段树)

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

  2. 【线段树区间合并】HDU1540-Tunnel Warfare

    一.题目 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...

  3. POJ 2892 Tunnel Warfare(线段树单点更新区间合并)

    Tunnel Warfare Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7876   Accepted: 3259 D ...

  4. hdu 1540 Tunnel Warfare (区间线段树(模板))

    http://acm.hdu.edu.cn/showproblem.php?pid=1540 Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) ...

  5. poj 2892 Tunnel Warfare(线段树)

    Tunnel Warfare Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7499   Accepted: 3096 D ...

  6. hdu1540之线段树单点更新+区间合并

    Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  7. hdu 1540 Tunnel Warfare(线段树区间统计)

    Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  8. poj 2892---Tunnel Warfare(线段树单点更新、区间合并)

    题目链接 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...

  9. hdu1540 Tunnel Warfare 线段树/树状数组

    During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast a ...

随机推荐

  1. Web框架之Django_02基本操作(Django项目启动配置、数据库连接、orm、增删改查)

    摘要: Django项目简单现实过程 pycharm连接数据库 Django之orm简单操作增删改查 一.新建Django项目.配置.设置: 新建Django项目:(为了熟悉Django操作,暂时全部 ...

  2. Python的第二堂课(1)

    一.编程语言的分类 机器语言:直接使用二进制命令去编写程序. 优点:执行效率高 缺点:开发效率低 汇编语言:用英文标签代替二进制命令去编写程序 优点:开发效率高于机器语言 缺点:执行效率低于机器语言 ...

  3. 【练习】reserving.kr 之Direct3D FPS

    算法函数如图,关键点在标志处,加密字符串如下图 于是写如下脚本: flag_c='436B666B62756C694C455C455F5A461C07252529701734390116494C201 ...

  4. js request学习

    SP的内置对象在JSP页面中无须声明就可以直接使用,其内置对象常用的有Request,response,session,application,out,config,pageCOntext reque ...

  5. Mysql 使用命令及 sql 语句示例

    Mysql 是数据库开发使用的主要平台之一.sql 的学习掌握与使用是数据库开发的基础,此处展示详细sql 语句的写法,及各种功能下的 sql 语句. 在此处有 sql 语句使用示例:在这里 此处插入 ...

  6. Java-对复合类型数据进行排序

    Array.sort(arr)可以进行简单的排序,如果需要复杂的排序可以实现Comparable package com.tj; import java.util.Arrays; public cla ...

  7. 【LeetCode】Same Tree(相同的树)

    这道题是LeetCode里的第100道题. 这是题目: 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1 ...

  8. Web开发细节搜集

    App_Data 百度百科:  App_Data文件夹应该包含应用程序的本地数据存储.它通常以文件(诸如Microsoft Access或Microsoft SQL Server Express数据库 ...

  9. 开源中国git关联xcode操作步骤

    1.网上代码托管有好多我选了开源中国的git 2.开源中国链接:http://git.oschina.net 3.在git上创建一个新的项目 4.打开终端全局设置名字和邮箱 tanqihongdeiM ...

  10. Morris Traversal 方法遍历二叉树(非递归、不用栈,O(1)空间)

    http://www.cnblogs.com/AnnieKim/archive/2013/06/15/MorrisTraversal.html