题目大意:有n个花瓶,每个花瓶中只能放一朵花。两种操作,一种是从A开始放F朵花,如果有的花瓶中已经有花则跳过这个花瓶,往下一个花瓶放;第二种是将区间[A,B]之间花瓶中的花清空。如果是第一种操作,输出这次放的花的左右端点;如果是第二种操作,输出这次总共清理出了多少支花。

题目分析:建立线段树,节点维护在相应的区间中,没有放入花的花瓶数目。有三种操作:一、查询某个区间中第k个没有插入花的花瓶位置;二、更新区间,使区间全部插入花;三、更新区间,使区间中的花瓶全部清空;

代码如下:

# include<iostream>
# include<cstdio>
# include<queue>
# include<vector>
# include<list>
# include<map>
# include<set>
# include<cstdlib>
# include<string>
# include<cstring>
# include<algorithm>
using namespace std;
# define LL long long const int N=50100;
const int INF=1<<30;
const double oo=1e20;
const double eps=1e-20; int lazy[N*4+5];
int tr[N*4+5]; void pushDown(int rt,int l,int r)
{
if(lazy[rt]!=-1){
lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];
if(lazy[rt]==1)
tr[rt<<1]=tr[rt<<1|1]=0;
else{
int mid=l+(r-l)/2;
tr[rt<<1]=mid-l+1;
tr[rt<<1|1]=r-mid;
}
lazy[rt]=-1;
}
} void pushUp(int rt)
{
tr[rt]=tr[rt<<1]+tr[rt<<1|1];
} void build(int rt,int l,int r)
{
tr[rt]=r-l+1;
lazy[rt]=-1;
if(l==r)
return ;
int mid=l+(r-l)/2;
build(rt<<1,l,mid);
build(rt<<1|1,mid+1,r);
} void update(int rt,int l,int r,int L,int R)
{
if(L<=l&&r<=R){
tr[rt]=0;
lazy[rt]=1;
}else{
pushDown(rt,l,r);
int mid=l+(r-l)/2;
if(L<=mid) update(rt<<1,l,mid,L,R);
if(R>mid) update(rt<<1|1,mid+1,r,L,R);
pushUp(rt);
}
} int clear(int rt,int l,int r,int L,int R)
{
if(L<=l&&r<=R){
int temp=tr[rt];
tr[rt]=r-l+1;
lazy[rt]=0;
return tr[rt]-temp;
}else{
pushDown(rt,l,r);
int mid=l+(r-l)/2;
int res=0;
if(L<=mid) res+=clear(rt<<1,l,mid,L,R);
if(R>mid) res+=clear(rt<<1|1,mid+1,r,L,R);
pushUp(rt);
return res;
}
} int query(int rt,int l,int r,int L,int R)
{
if(L>r||R<l) return 0; ///这句话必须加上,否则query可能会一直被调用下去
if(L<=l&&r<=R)
return tr[rt];
pushDown(rt,l,r);
int mid=l+(r-l)/2;
int res=0;
if(L<=mid)
res+=query(rt<<1,l,mid,L,R);
if(R>mid)
res+=query(rt<<1|1,mid+1,r,L,R);
return res;
} int ask(int rt,int l,int r,int L,int R,int num)
{
if(l==r){
return l;
}else{
pushDown(rt,l,r);
int mid=l+(r-l)/2;
int t=query(rt<<1,l,mid,L,R);
if(num<=t) return ask(rt<<1,l,mid,L,R,num);
return ask(rt<<1|1,mid+1,r,L,R,num-t);
}
} int main()
{
int T,n,m;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
build(1,1,n);
int k,a,b;
while(m--)
{
scanf("%d%d%d",&k,&a,&b);
if(k==1){
int t=query(1,0,n-1,a,n-1);
if(t==0)
printf("Can not put any one.\n");
else{
int l=ask(1,0,n-1,a,n-1,1);
int r=ask(1,0,n-1,a,n-1,min(b,t));
printf("%d %d\n",l,r);
update(1,0,n-1,l,r);
}
}else{
int ans=clear(1,0,n-1,a,min(n-1,b));
printf("%d\n",ans);
}
}
printf("\n");
}
return 0;
}

  

HDU-4614 Vases and Flowers (线段树区间更新)的更多相关文章

  1. HDU 4614 Vases and Flowers(线段树+二分)

    题目链接 比赛的时候一直想用树状数组,但是树状数组区间更新之后,功能有局限性.线段树中的lz标记很强大,这个题的题意也挺纠结的. k = 1时,从a开始,插b个花,输出第一个插的位置,最后一个的位置, ...

  2. HDU-4614 Vases and Flowers 线段树区间更新

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4614 线段树保存区间是否被覆盖以及区间的和即可,在询问的时候在线段树上二分查找就可以了...代码写得比 ...

  3. hdu 4614 Vases and Flowers 线段树

    题目链接 一共n个盒子, 两种操作, 第一种是给出两个数x, y, 从第x个盒子开始放y朵花, 一个盒子只能放一朵, 如果某个盒子已经有了, 那么就跳过这个盒子放下面的盒子. 直到花放完了或者到了最后 ...

  4. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  5. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  6. (简单) HDU 1698 Just a Hook , 线段树+区间更新。

    Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...

  7. HDU 1698 Just a Hook(线段树区间更新查询)

    描述 In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes ...

  8. hdu - 1689 Just a Hook (线段树区间更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=1698 n个数初始每个数的价值为1,接下来有m个更新,每次x,y,z 把x,y区间的数的价值更新为z(1<= ...

  9. 2014多校第四场1006 || HDU 4902 Nice boat (线段树 区间更新)

    题目链接 题意 : 给你n个初值,然后进行两种操作,第一种操作是将(L,R)这一区间上所有的数变成x,第二种操作是将(L,R)这一区间上所有大于x的数a[i]变成gcd(x,a[i]).输出最后n个数 ...

  10. HDU 1698 Just a Hook 线段树区间更新、

    来谈谈自己对延迟标记(lazy标记)的理解吧. lazy标记的主要作用是尽可能的降低时间复杂度. 这样说吧. 如果你不用lazy标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用 ...

随机推荐

  1. K2上海总部技术培训分享笔记

    第一部门 WinDdg 入门指南 1.NGen.exe --> native code 预编译,省去了.NET程序编译器JIT过程,是程序第一次运行也非常快. NGen 参考资料:http:// ...

  2. SharePoint 2016 的新特性概览(二)(What's New for IT Professionals in SharePoint Server 2016)

    博客地址:http://blog.csdn.net/FoxDave SharePoint 2016 的新特性 三. 监测和数据(Insights and Data) 实时数据监测,包括对使用情况.存储 ...

  3. vSphere Client上传镜像

    1. 使用vSphere Client连接到VMware ESXI 2. 打开右侧 [摘要] 选项卡 3. 在 [资源] 中选择存储器中的存储,右键 [浏览数据库存储] 4. 选择工具栏 [创建新文件 ...

  4. 配置navigation bar外观

    /* 配置navigation bar外观开始 */ self.navigationBar.translucent = YES; self.navigationBar.titleTextAttribu ...

  5. JS 基于面向对象的 轮播图1

    ---恢复内容开始--- 1 'use strict' 2 function Tab(id){ 3 if(!id)return; 4 this.oBox = document.getElementBy ...

  6. Ubuntu 14.10 下MySQL无法远程连接问题

    安装好MySQL之后,如果需要远程连接,那么需要做一些配置,否则会出现一些类似的错误,如 mysql root用户ERROR (): mysql 远程登录 ERROR () mysql 远程登录200 ...

  7. 7、SQL基础整理(子查询)

    子查询 (用来进行两个或以上表之间的查询) 1.首先新建一个bumen表和一个haha表,填充数据 2.利用两表进行子查询: --部门人数大于5的部门中最大年龄的人的信息--- select MAX( ...

  8. NOIP2010 关押罪犯 (并查集)

    若x,y有关系 将x与y的补集, y与x的补集建立关系 ; maxm=; ..maxm,..] of longint; f:..maxn*] of longint; i,j,m,n,x,y,z:lon ...

  9. Unity3D ShaderLab 立方体图的菲涅尔反射

    Unity3D ShaderLab 立方体图的菲涅尔反射 菲涅尔反射是反射类型中比较常见的一种类型,当我们的视线正对物体表面,那么反射量会明显增加, 我们几乎可以在任何支持反射类型的物体表面看到这种情 ...

  10. Magento架构师的笔记-----Magento显示当前目录的父分类和子分类的分类名

    在Magento目录的分类页面里,希望在左侧导航获取到父分类和子分类,可以用以下方法:打开app/your_package/your_themes/template/catalog/navigatio ...