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 ...
随机推荐
- tensorboard以时间命名每一个文件夹
tensorboard 有一个良好的命名习惯以时间命名每一个文件夹,例如**20190523_081232** ''' from datetiome import datetime dir = os. ...
- django第六天(模板相关,过滤器和标记)
django第6天 DTL简介 django template language django模板语言 语法: filter{{}} tag{% %} 简单变量的使用 视图函数可以通过两种方式将变量船 ...
- STM32开发笔记之——CMSIS DAP
都说开发stm32都是使用kail iar+jatg/swd的方式,然而arm公司已经开发出了CMSIS DAP的开源下载工具,全称是CoreSight Debug Access Port,网络上有大 ...
- addEvenListener('DOMContentLoaded',function(){})
- Python3 中 configparser 模块解析配置的用法详解
configparser 简介 configparser 是 Pyhton 标准库中用来解析配置文件的模块,并且内置方法和字典非常接近.Python2.x 中名为 ConfigParser,3.x 已 ...
- 在xcode上把你的app多语言国际化(NSLocalizedString)
1.到project->info->localizations 下面的加号,添加你需要的语言 千万不要删除 base 否虽然我不知道有什么用,我是删了整个storyboard没了,很 ...
- Flask+ Angularjs 实例: 创建博客
允许任何用户注册 允许注册的用户登录 允许登录的用户创建博客 允许在首页展示博客 允许登录的用户退 后端 Flask-RESTful - Flask 的 RESTful 扩展 Flask-SQLAlc ...
- php 投票
1.投票主界面(问题界面) <?php$db = new Mysqli("localhost","root","root"," ...
- 【软考2】Java语言的基本知识汇总
导读:现在对于java这一模块,还没有相应的项目经验,只是通过各种类型的资料,对java有一个面上的了解.现在,对此做一个罗列总结,在以后的学习过程中,逐步完善! 一.语言的发展 1.1,机器语言 在 ...
- [luoguP1053] 篝火晚会(贪心 + 乱搞)
传送门 假设第一个位置是1,那么枚举它的左右两边是谁,有两种情况,然后可以递推求出序列. 然后可以贪心,两个序列有多少个不同的数,答案就是多少,具体为啥,yy一下即可 然后就是判断递推求出的序列和目标 ...