HDU 4614-Vases and Flowers(线段树区间更新)
题意:
n个花瓶(0-n-1) 现有两个操作,
操作1 给a,f 从a位置开始向后连续插f个花(一个花瓶插一个)若当前花瓶有花则向后找,直到n-1位置如果还有多余的花则丢掉求查完花的第一和最后一个位置。
操作2 L,R 清空[l,r]花瓶内的花,并输出花的数量。
分析:
本题关键是用二分求插花的首末的位置,其他是基本的区间更新。
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<1|1
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
#define N 50010
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
int vempty[N*],setv[N*],n,q,p,pos;
void pushup(int rt){
vempty[rt]=vempty[rt<<]+vempty[rt<<|];
}
void pushdown(int rt,int len){
if(setv[rt]!=-){
setv[rt<<]=setv[rt<<|]=setv[rt];
vempty[rt<<]=(len-len/)*setv[rt];
vempty[rt<<|]=len/*setv[rt];
setv[rt]=-;
}
}
void build(int l,int r,int rt){
setv[rt]=-;
vempty[rt]=r-l+;
if(l==r)return ;
int m=(l+r)>>;
build(lson);
build(rson);
}
void update_add(int L,int R,int l,int r,int rt){
if(L<=l&&R>=r){
vempty[rt]=setv[rt]=;
return;
}
pushdown(rt,r-l+);
int m=(l+r)>>;
if(L<=m)update_add(L,R,lson);
if(R>m)update_add(L,R,rson);
pushup(rt);
}
void update_cle(int L,int R,int l,int r,int rt){
if(L<=l&&R>=r){
setv[rt]=;
vempty[rt]=(r-l+);
return;
}
pushdown(rt,r-l+);
int m=(l+r)>>;
if(L<=m)update_cle(L,R,lson);
if(R>m)update_cle(L,R,rson);
pushup(rt);
}
int query(int L,int R,int l,int r,int rt){
if(L<=l&&R>=r){
return vempty[rt];
}
pushdown(rt,r-l+);
int num=;
int m=(l+r)>>;
if(L<=m)num+=query(L,R,lson);
if(R>m)num+=query(L,R,rson);
pushup(rt);
return num;
}
int main()
{
int t,k,a,b;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&q);
build(,n,);
while(q--){
scanf("%d%d%d",&k,&a,&b);
if(k==){
int ans=query(a+,n,,n,);
if(ans==){
printf("Can not put any one.\n");
continue;
}
//二分首位置
int num=,l=a+,r=n,ll,rr;
while(l<=r){
int m=(l+r)>>;
int tmp=query(a+,m,,n,);
if(tmp<num)l=m+;
else
r=m-;
}
ll=l;
//二分末位置
num=ans<b?ans:b,l=a+,r=n;
while(l<=r){
int m=(l+r)>>;
int tmp=query(a+,m,,n,);
if(tmp<num)l=m+;
else
r=m-;
}
rr=l;
printf("%d %d\n",ll-,rr-);
update_add(ll,rr,,n,);
}
else{
int tmp=b-a+-query(a+,b+,,n,);
printf("%d\n",tmp);
update_cle(a+,b+,,n,);
}
}
printf("\n");
}
return ;
}
HDU 4614-Vases and Flowers(线段树区间更新)的更多相关文章
- HDU 4614 Vases and Flowers(线段树+二分)
题目链接 比赛的时候一直想用树状数组,但是树状数组区间更新之后,功能有局限性.线段树中的lz标记很强大,这个题的题意也挺纠结的. k = 1时,从a开始,插b个花,输出第一个插的位置,最后一个的位置, ...
- HDU-4614 Vases and Flowers 线段树区间更新
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4614 线段树保存区间是否被覆盖以及区间的和即可,在询问的时候在线段树上二分查找就可以了...代码写得比 ...
- hdu 4614 Vases and Flowers 线段树
题目链接 一共n个盒子, 两种操作, 第一种是给出两个数x, y, 从第x个盒子开始放y朵花, 一个盒子只能放一朵, 如果某个盒子已经有了, 那么就跳过这个盒子放下面的盒子. 直到花放完了或者到了最后 ...
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- HDU 1556 Color the ball(线段树区间更新)
Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...
- (简单) HDU 1698 Just a Hook , 线段树+区间更新。
Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...
- 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 ...
- hdu - 1689 Just a Hook (线段树区间更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1698 n个数初始每个数的价值为1,接下来有m个更新,每次x,y,z 把x,y区间的数的价值更新为z(1<= ...
- 2014多校第四场1006 || HDU 4902 Nice boat (线段树 区间更新)
题目链接 题意 : 给你n个初值,然后进行两种操作,第一种操作是将(L,R)这一区间上所有的数变成x,第二种操作是将(L,R)这一区间上所有大于x的数a[i]变成gcd(x,a[i]).输出最后n个数 ...
- HDU 1698 Just a Hook 线段树区间更新、
来谈谈自己对延迟标记(lazy标记)的理解吧. lazy标记的主要作用是尽可能的降低时间复杂度. 这样说吧. 如果你不用lazy标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用 ...
随机推荐
- django开发框架-view & template
django框架的主要模型是MVT,Model模型,View视图,Template模板,基于基本的HttpRequest方式. django支持的数据库有四种:PostgreSQL,MySQL, Or ...
- Android水平(横向)翻页列表,类似水平GridVIew
Android水平(横向)翻页列表,类似于水平方向的GridView,行列自定义,但要翻页切换,考虑加载性能,当Item数据很多时加载和翻页要流畅,翻页时要有动画效果,效果图如下: 实现方式: 1:翻 ...
- owa Your request can't be completed right now. Please try again later.
Your request can't be completed right now. Please try again later.
- Jetty 与 Tomcat 比较,及性能分析
主流java的web容器,主要是Tomcat, jboss, jetty, resin.由于以前我们主要用的是jboss4.0.5,但jbosse用的servlet容器是tomcat5.5,所以只进行 ...
- python学习笔记3(字符串)
Python字符串: 在Python中的字符串被确定为一组连续的字符在引号之间, Python允许在任何对单引号或双引号. 串的子集,可以使用切片操作符可采用([]和[:]),索引从0开始的字符串的开 ...
- [搜片神器]BT种子下载超时很多的问题分析
继续接着第一篇写:使用C#实现DHT磁力搜索的BT种子后端管理程序+数据库设计(开源)[搜片神器] 谢谢园子朋友的支持,已经找到个VPS进行测试,国外的服务器: h31bt.org 大家可以给提点意 ...
- js获取节点
demo1: <!-- <div id="test" v="1">你好</div> --> // console.log(t ...
- BZOJ 1589: [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果
Description 每年万圣节,威斯康星的奶牛们都要打扮一番,出门在农场的N(1≤N≤100000)个牛棚里转悠,来采集糖果.她们每走到一个未曾经过的牛棚,就会采集这个棚里的1颗糖果. 农场不大, ...
- PAT-乙级-1013. 数素数 (20)
1013. 数素数 (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 令Pi表示第i个素数.现任给两个正整 ...
- Android ExpandableListView 带有Checkbox的简单应用
expandablelistview2_groups.xml <?xml version="1.0" encoding="utf-8"?> < ...