POJ3067:Japan(线段树)
Description
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 number of the city on the East coast and second one is the number of the city of the West coast.
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(线段树)的更多相关文章
- K - Japan(线段树)
Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Jap ...
- 【线段树区间合并】HDU1540-Tunnel Warfare
一.题目 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...
- POJ 2892 Tunnel Warfare(线段树单点更新区间合并)
Tunnel Warfare Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 7876 Accepted: 3259 D ...
- hdu 1540 Tunnel Warfare (区间线段树(模板))
http://acm.hdu.edu.cn/showproblem.php?pid=1540 Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) ...
- poj 2892 Tunnel Warfare(线段树)
Tunnel Warfare Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 7499 Accepted: 3096 D ...
- hdu1540之线段树单点更新+区间合并
Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- hdu 1540 Tunnel Warfare(线段树区间统计)
Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- poj 2892---Tunnel Warfare(线段树单点更新、区间合并)
题目链接 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...
- hdu1540 Tunnel Warfare 线段树/树状数组
During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast a ...
随机推荐
- python爬虫基础06-常见加密算法
Python与常见加密方式 前言 数据加密与解密通常是为了保证数据在传输过程中的安全性,自古以来就一直存在,古代主要应用在战争领域,战争中会有很多情报信息要传递,这些重要的信息都会经过加密,在发送到对 ...
- Python之路-基础数据类型之字典 集合
字典的定义-dict 字典(dict)是python中唯⼀的⼀个映射类型.他是以{ }括起来的键值对组成,字典是无序的,key是不可修改的.dic = {1:'好',2:'美',3:'啊'} 字典的操 ...
- cdev_add
初始化 cdev 后,需要把它添加到系统中去.为此可以调用 cdev_add()函数.传入cdev 结构的指针,起始设备编号,以及设备编号范围. 函数首先将分配的设备号与设备数目保存进cdev结构体中 ...
- cs229_part3
接下来就是最最最重要的一个有监督学习算法了. 支持向量机 问题背景 样本集表示: \[(x,y)\in D, x\in R^n, y\in \{-1,+1\}\] 回到之前的逻辑回归模型中: 逻辑回归 ...
- unittest单元测试(测试报告生成)
自动化测试执行完成之后,我们需要生成测试报告来查看测试结果,使用HTMLTestRunner模块可以直接生产Html格式的报告. 下载地址: http://tungwaiyip.info/softwa ...
- ubuntu14.04LTS root登录出现错误
输入root帐号和密码,出现以下错误: Error found when loading /root/.profile stdin:is not a tty 解决方法: 在终端中用命令:gedit / ...
- Tinkoff Challenge - Elimination Round 开始补题
A. Oleg and shares time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- SPOJ CIRU The area of the union of circles ——Simpson积分
[题目分析] 圆的面积并. 直接Simpson积分,(但是有计算几何的解法,留着flag). simpson积分,如果圆出现了不连续的情况,是很容易出事情的.(脑补一下) 但是没有什么办法,本来就是一 ...
- BZOJ3295 动态逆序对(树状数组套线段树)
[Cqoi2011]动态逆序对 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 6058 Solved: 2117[Submit][Status][D ...
- 转:C++ Vector用法深入剖析
http://developer.51cto.com/art/201002/183645.htm C++编程语言中有一种叫做Vector的应用方法,它的作用在实际编程中是非常重要的.在这里我们将会为大 ...