题目大意:有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. (转)MyEclipse设置注释格式

    原文:http://xinghaifeng2006.iteye.com/blog/1243565 MyEclipse设置注释格式(转载)          博客分类: Java基础知识   Windo ...

  2. Osmocom-BB多信道修改代码相关

    修改bb\src\target\firmware\layer1\prim_rx_nb.c 文件 这个nb表示normalburst,指的是ccch的数据,用专业的术语,应该是,一个ccch的burst ...

  3. eclipse中导入一个android工程有The import android cannot be resolved错误怎么办

    解决方法: 右键工程→Bulid Path→Configure Build Path... 选择Android,如图,在Project Build Target里面勾选相应的SDK即可 右键工程,pr ...

  4. ELF Spec

    ELF Spec Generic System V Application Binary Interface,ELF-64 Object File Format AMD64 System V ABI, ...

  5. BZOJ 2501 Soda Machine

    BIT+离散化. #include<iostream> #include<cstdio> #include<cstring> #include<algorit ...

  6. ERP权限系统(七)

    添加链接权限的字段: //权限管理 n.Target = "MainFrame"; //折叠 TreeView1.Nodes.Add(n); n.Expanded = false;

  7. React Native 组件之TextInput

    React Native 组件之TextInput类似于iOS中的UITextView或者UITextField,是作为一个文字输入的组件,下面的TextInput的用法和相关属性. /** * Sa ...

  8. swift初识

    介绍: Swift是苹果2014年推出的全新编程语言,它继承了C语言.ObjC的特性,且克服了C语言的兼容性问题.Swift发展过程不仅保留了Objc很多语言特性,他也借鉴了多种现代化语言的特点,在其 ...

  9. IOS 作业项目(3) 霓虹灯效果

    先上效果图 #import "CHViewController.h"@interface CHViewController (){    int i;    int j;}@pro ...

  10. HDU1272-小希的迷宫(并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 #include<cstdio> #include<cstring> u ...