[swustoj 764] 校门外的树 Plus Plus
校门外的树 Plus Plus(0764)
问题描述
西南某科技大学的校门外长度为 L 的公路上有一排树,每两棵相邻的树之间的间隔都是 1 米。我们可以把马路看成一个数轴,马路的一端在数轴 1 的位置,另一端在 L 的位置;数轴上的每个整数点,即 1,2,……,L,都种有一棵树。
现在要将这排树的某一段涂成某种颜色,给定 N 组区间[ J,K ],表示从 J 到 K 的树都要涂上一种颜色(每次涂的颜色不一样,而且每次涂色会覆盖掉原来的颜色),区间之间可能有重叠的部分, L 的长度为 1000,0000。现在你的任务是计算涂色工作结束后,这一排树里总共有多少种颜色的树?(没涂过色的不算)
输入
第一行为一个整数N (1<=N<=10000)。 接下来的N行每行是两个整数数J,K(1<=J<=K<=1000,0000),表示涂色的起止区间。
多组测试数据。
输出
一个整数,表示有多少种颜色的树。
样例输入
2
1 10
5 5
1
1 1
样例输出
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 40005 struct tree
{
int l,r;
}p[N<<]; int tot;
int ans;
int x[N];
int lazy[N<<];
bool flag[N<<]; void pushdown(int rt)
{
if(lazy[rt]!=-)
{
lazy[rt<<]=lazy[rt<<|]=lazy[rt];
lazy[rt]=-;
}
}
void build(int l,int r,int rt)
{
lazy[rt]=-;
if(l==r) return;
int m=(l+r)>>;
build(l,m,rt<<);
build(m+,r,rt<<|);
}
void update(int l,int r,int rt,int L,int R,int c)
{
if(l==L && R==r)
{
lazy[rt]=c;
return;
}
pushdown(rt);
int m=(l+r)>>;
if(R<=m) update(l,m,rt<<,L,R,c);
else if(L>m) update(m+,r,rt<<|,L,R,c);
else
{
update(l,m,rt<<,L,m,c);
update(m+,r,rt<<|,m+,R,c);
}
}
void query(int l,int r,int rt)
{
if(lazy[rt]!=-)
{
if(!flag[lazy[rt]])
{
ans++;
flag[lazy[rt]]=;
}
return;
}
if(l>=r) return;
pushdown(rt);
int m=(l+r)>>;
query(l,m,rt<<);
query(m+,r,rt<<|);
}
int main()
{
int n,i;
while(scanf("%d",&n)!=EOF)
{
ans=;
tot=;
memset(flag,,sizeof(flag));
for(i=;i<n;i++)
{
scanf("%d%d",&p[i].l,&p[i].r);
x[tot++]=p[i].l;
x[tot++]=p[i].r;
} sort(x,x+tot);
tot=unique(x,x+tot)-x;
for(i=tot-;i>;i--)
{
if(x[i]-x[i-]>) x[tot++]=x[i-]+;
}
sort(x,x+tot);
build(,tot-,);
for(i=;i<n;i++)
{
int l=lower_bound(x,x+tot,p[i].l)-x;
int r=lower_bound(x,x+tot,p[i].r)-x;
update(,tot-,,l,r,i);
}
query(,tot-,);
cout<<ans<<endl;
}
return ;
}
原来搓代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 20005 struct tree
{
int l,r;
}p[N<<];
struct node
{
int val,id,pos;
}h[N<<]; int ans;
int color[N<<];
bool flag[N<<];
int change[N<<]; bool cmp(node a,node b)
{
return a.val<b.val;
}
void pushup(int rt)
{
color[rt]=;
}
void pushdown(int rt)
{
if(change[rt]!=-)
{
change[rt<<]=change[rt<<|]=change[rt];
color[rt<<]=color[rt<<|]=change[rt];
change[rt]=-;
}
}
void build(int l,int r,int rt)
{
color[rt]=;
change[rt]=-;//初始化
if(l==r) return;
int m=(l+r)>>;
build(l,m,rt<<);
build(m+,r,rt<<|);
}
void update(int l,int r,int rt,int L,int R,int c)
{
if(l==L && R==r)
{
color[rt]=c;
change[rt]=c;
return;
}
pushdown(rt);
int m=(l+r)>>;
if(R<=m)
update(l,m,rt<<,L,R,c);
else if(L>m)
update(m+,r,rt<<|,L,R,c);
else
{
update(l,m,rt<<,L,m,c);
update(m+,r,rt<<|,m+,R,c);
}
pushup(rt);
}
void query(int l,int r,int rt)
{
int m=(l+r)>>;
if(color[rt])
{
if(!flag[color[rt]])
{
ans++;
flag[color[rt]]=;
}
return;
}
if(l==r) return; //忘了加这一句、一直RE
query(l,m,rt<<);
query(m+,r,rt<<|);
}
int main()
{
int n,i;
while(scanf("%d",&n)!=EOF)
{
ans=;
memset(flag,,sizeof(flag));
for(i=;i<n;i++)
{
scanf("%d%d",&p[i].l,&p[i].r);
h[i*].val=p[i].l;
h[i*].id=i;
h[i*].pos=;
h[i*+].val=p[i].r;
h[i*+].id=i;
h[i*+].pos=;
}
/* 离散化 */
sort(h,h+n*,cmp);
int last=-,total=;
for(i=;i<n*;i++)
{
if(h[i].val!=last)
{
total++;
if(i && h[i].val-h[i-].val>) total++; //注意这里、= =、手动插点
last=h[i].val;
}
if(h[i].val!=total)
{
if(h[i].pos) p[h[i].id].r=total;
else p[h[i].id].l=total;
}
}
build(,total,);
for(i=;i<n;i++)
{
update(,total,,p[i].l,p[i].r,i+);
}
query(,total,);
cout<<ans<<endl;
}
return ;
}
[swustoj 764] 校门外的树 Plus Plus的更多相关文章
- P1047 校门外的树
P1047 校门外的树 题目描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置:数轴上的每个整数点,即0 ...
- Vijos1448校门外的树 题解
Vijos1448校门外的树 题解 描述: 校门外有很多树,有苹果树,香蕉树,有会扔石头的,有可以吃掉补充体力的…… 如今学校决定在某个时刻在某一段种上一种树,保证任一时刻不会出现两段相同种类的树,现 ...
- OpenJudge计算概论-校门外的树
/*======================================================================== 校门外的树 总时间限制: 1000ms 内存限制: ...
- 校门外的树 - Grids2808
校门外的树 问题描述: 某校大门外长度为 L 的马路上有一排树,每两棵相邻的树之间的间隔都是1 米.我们 可以把马路看成一个数轴,马路的一端在数轴0 的位置,另一端在L 的位置:数轴上的每 个整数点, ...
- 校门外的树 OpenJudge 1.6.06
06:校门外的树 总时间限制: 1000ms 内存限制: 65536kB 描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的一端在数轴0 ...
- 【解题报告】VijosP1448校门外的树(困难版)
原题: 校门外有很多树,有苹果树,香蕉树,有会扔石头的,有可以吃掉补充体力的--如今学校决定在某个时刻在某一段种上一种树,保证任一时刻不会出现两段相同种类的树,现有两个操作:K=1,K=1,读入l.r ...
- Vijos P1103 校门外的树【线段树,模拟】
校门外的树 描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置:数轴上的每个整数点,即0,1,2,……, ...
- Vijos P1448 校门外的树【多解,线段树,树状数组,括号序列法+暴力优化】
校门外的树 描述 校门外有很多树,有苹果树,香蕉树,有会扔石头的,有可以吃掉补充体力的…… 如今学校决定在某个时刻在某一段种上一种树,保证任一时刻不会出现两段相同种类的树,现有两个操作: K=1,K= ...
- C语言 · 校门外的树
算法提高 校门外的树 时间限制:1.0s 内存限制:256.0MB 问题描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的 ...
随机推荐
- NOIP(提高组)DAY1国王游戏
问题描述 恰逢H国国庆,国王邀请n位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这n位大臣排成一排,国王站在队伍的最前面.排好队 ...
- java中的IO流
Java中的IO流 在之前的时候我已经接触过C#中的IO流,也就是说集中数据固化的方式之一,那么我们今天来说一下java中的IO流. 首先,我们学习IO流就是要对文件或目录进行一系列的操作,那么怎样操 ...
- Convert.ToInt32()与int.Parse()的区别 (转载)
Convert.ToInt32()与int.Parse()的区别(1)这两个方法的最大不同是它们对null值的处理:Convert.ToInt32(null)会返回0而不会产生任何异常,但int.Pa ...
- Apache的多路处理模块MPM:Prefork Worker Event
如何确认当前apache使用哪种模式 通过/etc/init.d/httpd中的来确认系统apache的运行脚本路径 apachectl=/usr/sbin/apachectl httpd=${HTT ...
- vb delphi7、2010 csharp vb.net空白测试程序
工作中难免在网上看到一段不错的代码,希望能够拿来测试一次,为了避免每次测试都要新建一个空白测试程序,索性预先建立好,要用的时候复制一遍,然后打开直接粘贴需要测试的代码进行测试.
- 【BZOJ】1044: [HAOI2008]木棍分割 二分+区间DP
链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1044 Description 有n根木棍, 第i根木棍的长度为Li,n根木棍依次连结了一起, ...
- codeforces 630K - Indivisibility
K. Indivisibility 题意:给一个n(1 <= n <= 10^18)的区间,问区间中有多少个数不能被2~10这些数整除: 整除只需要看素数即可,只有2,3,5,7四个素数: ...
- hadoop基本命令1
(大讲台——国内首个it在线混合式自适应学习平台,轻量级的高薪就业和技能提升解决方案) 1.列出所有Hadoop Shell支持的命令$ bin/hadoop fs -help2.显示关于某个命令的详 ...
- 制作按钮(Button)
按钮的核心作用 1.按钮能接收单击并触发响应事件. 2.按钮被单击时能同时触发多个响应事件. 3.按钮可以有普通.悬停.单击.禁用等多个状态的不同表现. 4.广泛的说,按钮的核心在于接收事件,任何可以 ...
- ThinkPHP框架安全性能分析
http://www.freebuf.com/articles/web/59713.html 点击劫持cookie 点击劫持所有链接