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标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用 ...
随机推荐
- swift基础--字符串
(1)遍历 (2)长度 (3)拼接 (4)插值 (5)大小写 (6)trim (7)split ……等等 var a = "你好" var b = String() a.isEmp ...
- 在 Windows 8 或 8.1 上安装 .NET Framework 3.5 安装错误:0x800f0906、0x800F081F
昨天给一天新装Windows 8.1的PC装.NET Framework 3.5 发现联网速度很慢,并且在长久等待过后直接报错了:0x800f0906 经过Bing,发现了解决方案: 如果根据需要安装 ...
- 1197: [HNOI2006]花仙子的魔法 - BZOJ
Description Input 包含两个整数,并用一个空格隔开,第一个整数表示实施魔法的次数m,第二个整数表示空间的维数n.其中,1≤m≤100,1≤n≤15. Output 仅包含一个整数,表示 ...
- Workspace in use or cannot be created, choose a different one.--错误解决办法
eclipse 使用一段时间后,有时会因为一些故障自己就莫名奇妙的关闭了,再打开时有时没有问题,有时有会提示错误 Workspace Unavailable: 原因:出现这种情况一般是workspac ...
- myeclipse报错:Could not create the view: An unexpected exception was thrown.
打开server窗口,发现显示:Could not create the view: An unexpected exception was thrown. 此处解决方法: 关闭myeclipse 删 ...
- TIANKENG’s restaurant
Problem B:http://codeforces.com/contest/616/problem/B B. Dinner with Emma 题意:一对夫妻要去餐厅吃晚饭,Emma 想去最豪华( ...
- DLL远程注入与卸载
以下提供两个函数,分别用于向其它进程注入和卸载指定DLL模块.支持Unicode编码. #include <windows.h>#include <tchar.h>#inclu ...
- 【NGUI】屏幕自适应(不用UIStretch,用UIRoot)---------------good
原地址:http://blog.csdn.net/lzhq1982/article/details/18814023 这篇文章是转载的,之前用UIStretch做屏幕自适应,但一直有两个硬伤让我难受, ...
- DIY Ruby CPU 分析——Part III
[编者按]作者 Emil Soman,Rubyist,除此之外竟然同时也是艺术家,吉他手,Garden City RubyConf 组织者.本文是 DIY Ruby CPU Profiling 的第二 ...
- SOCI、LiteSQL、POCO数据库访问类库对比
最近在做视频的开发,其中视频的设备接入管理服务器.流媒体管理服务器.中心服务器都涉及到了数据库的操作,同时需要兼容大多数版本的数据库,包括mysql.sqlite.oracle.公司原来使用的是ado ...