hihoCoder:#1079(线段树+离散化)
题目大意:给n个区间,有的区间可能覆盖掉其他区间,问没有完全被其他区间覆盖的区间有几个?区间依次给出,如果有两个区间完全一样,则视为后面的覆盖前面的。
题目分析:区间可能很长,所以要将其离散化。但离散化之后区间就变成了连续的,不再是离散的。也就是叶子由左右端点为u、u变成了左右端点为u-1、u,左右儿子有(l,mid)和(mid+1,r)变成了(l,mid)和(mid,r)。所以离散化之后要以长度为1的区间为叶子节点建立线段树,而不是以1、2、3...为叶子节点建线段树。
代码如下:
# include<iostream>
# include<cstdio>
# include<set>
# include<map>
# include<cstring>
# include<algorithm>
using namespace std; const int N=200000; int tr[N*4+5];
int n,m;
int lazy[N*4+5];
int x[N+5],y[N+5];
set<int>s;
map<int,int>mp;
int num[N+5]; void pushDown(int rt,int l,int r)
{
if(lazy[rt]==-1) return ;
lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];
tr[rt<<1]=tr[rt<<1|1]=tr[rt];
lazy[rt]=-1;
} void update(int rt,int l,int r,int L,int R,int val)
{
if(L<=l&&r<=R){
tr[rt]=val;
lazy[rt]=val;
}else{
pushDown(rt,l,r);
int mid=l+(r-l)/2;
if(L<=mid) update(rt<<1,l,mid,L,R,val);
if(R>mid) update(rt<<1|1,mid+1,r,L,R,val);
}
} void query(int rt,int l,int r)
{
if(l==r){
if(tr[rt]!=-1) s.insert(tr[rt]);
return ;
}
if(lazy[rt]!=-1){
s.insert(tr[rt]);
return ;
}
int mid=l+(r-l)/2;
query(rt<<1,l,mid);
query(rt<<1|1,mid+1,r);
} int f(int l,int r,int x)
{
while(l<r){
int mid=l+(r-l)/2;
if(num[mid]<x)
l=mid+1;
else
r=mid;
}
return l;
} int main()
{
while(~scanf("%d%d",&n,&m))
{
num[0]=0;
mp.clear();
for(int i=0;i<n;++i){
scanf("%d%d",x+i,y+i);
if(mp[x[i]]==0) num[++num[0]]=x[i],++mp[x[i]];
if(mp[y[i]]==0) num[++num[0]]=y[i],++mp[y[i]];
}
sort(num+1,num+num[0]+1);
memset(tr,-1,sizeof(tr));
memset(lazy,-1,sizeof(lazy));
for(int i=0;i<n;++i){
int a=f(1,num[0]+1,x[i]);
int b=f(1,num[0]+1,y[i]);
update(1,1,num[0]-1,a,b-1,i);
}
s.clear();
query(1,1,num[0]-1);
printf("%d\n",s.size());
}
return 0;
}
hihoCoder:#1079(线段树+离散化)的更多相关文章
- hihoCoder#1079(线段树+坐标离散化)
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho在回国之后,重新过起了朝7晚5的学生生活,当然了,他们还是在一直学习着各种算法~ 这天小Hi和小Ho所在的学 ...
- POJ 2528 Mayor's posters(线段树+离散化)
Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...
- poj 2528 Mayor's posters(线段树+离散化)
/* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...
- [poj2528] Mayor's posters (线段树+离散化)
线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...
- [UESTC1059]秋实大哥与小朋友(线段树, 离散化)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1059 普通线段树+离散化,关键是……离散化后建树和查询都要按照基本法!!!RE了不知道多少次………………我真 ...
- poj 2528 Mayor's posters 线段树+离散化技巧
poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...
- BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针
BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针 Description 在数轴上有 n个闭区间 [l1,r1],[l2,r2],...,[ln,rn].现在要从中选出 m 个区间, ...
- D - Mayor's posters(线段树+离散化)
题目: The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campai ...
- 主席树||可持久化线段树+离散化 || 莫队+分块 ||BZOJ 3585: mex || Luogu P4137 Rmq Problem / mex
题面:Rmq Problem / mex 题解: 先离散化,然后插一堆空白,大体就是如果(对于以a.data<b.data排序后的A)A[i-1].data+1!=A[i].data,则插一个空 ...
- HDU5124:lines(线段树+离散化)或(离散化思想)
http://acm.hdu.edu.cn/showproblem.php?pid=5124 Problem Description John has several lines. The lines ...
随机推荐
- 经典线程同步 关键段CS
上一篇<秒杀多线程第四篇 一个经典的多线程同步问题>提出了一个经典的多线程同步互斥问题,本篇将用关键段CRITICAL_SECTION来尝试解决这个问题. 本文首先介绍下如何使用关键段,然 ...
- checkbox的全选、反选(计算价格)
package com.baidu.jisuan; import java.util.ArrayList;import java.util.List; import com.baidu.adapter ...
- 黑客界大拿tombkeeper文章:怎么学好技术成为技术大拿(题目我自拟的)
这两天论坛上又有人开始抱怨世风日下,大家都现实了,都不开放了,不交流了.对这种“月经贴”,我基本上已经习惯了,不过因为吃了粉皮炖鸡,心情比较好,于是就说了两句. 三四年前,当时我对人性的看法还不像现在 ...
- MapReduce数据流(二)
输入块(InputSplit):一个输入块描述了构成MapReduce程序中单个map任务的一个单元.把一个MapReduce程序应用到一个数据集上,即是指一个作业,会由几个(也可能几百个)任务组成. ...
- 简单的php Mysql类(查询 删除 更新)
php Mysql类一般都包括了几乎我们常用的数据库操作方法,这里只提供了查询 删除 更新三种操作,算不是很全只是一个简单的数据库查询类了. 代码如下 复制代码 class mysql { ...
- sql server手工注入
sql server手工注入 测试网站testasp.vulnweb.com 1. http://testasp.vulnweb.com/showforum.asp?id=0 http://testa ...
- phpunit+selenium环境搭建
这个环境搭建遇到了挺多麻烦,最终还是没能自己解决,幸好有同事“青蛙”的帮忙解决了这个问题!在这里把本人亲测步骤给大家列一下,希望给大家提供方便! 安装pear: Go-pear.phar下载地址:ht ...
- 不能使用weak修饰进行声明的类
These classes include NSTextView, NSFont and NSColorSpace; for the full list, see Transitioning to A ...
- AmazeUI HTML元素
按钮Button am-btn-xx(default.primary.secondary.success.warning.danger.link) am-radius 圆角按钮 am-round 椭圆 ...
- 怎样将某一类型标识为适合绑定到 System.Web.UI.WebControls.ObjectDataSource 对象的对象
1.页面的代码如下: body> <form id="form1" runat="server"> <div> </div& ...