HDU4614Vases and Flowers 二分+线段树;
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iterator> using namespace std;
#define lson l , mid , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define debug(x) cerr << #x << " = " << x << "\n"; typedef long long ll;
const int maxn = ;
const int mdd = ;
int n,m;
struct node {
int l,r;
int lazy,sum;
}st[maxn<<]; void build(int l,int r,int rt)
{
st[rt].l = l,st[rt].r = r;
st[rt].sum = ;
st[rt].lazy = -;
int mid = (l+r)>>;
if(l==r)return;
build(lson);
build(rson);
}
inline void pushup(int rt)
{
st[rt].sum = st[rt<<].sum + st[rt<<|].sum;
}
inline void pushdown(int rt)
{ if(st[rt].lazy==)
{
st[rt<<].lazy = st[rt<<|].lazy = st[rt].lazy;
st[rt<<].sum = st[rt<<].r - st[rt<<].l + ;
st[rt<<|].sum = st[rt<<|].r - st[rt<<|].l + ;
}
else if(st[rt].lazy==)
{
st[rt<<].lazy = st[rt<<|].lazy = st[rt].lazy;
st[rt<<].sum = st[rt<<|].sum = ;
} st[rt].lazy = -;
}
int query(int l,int r,int rt,int L,int R)
{
if(l>=L&&r<=R)
{
return st[rt].sum;
}
int mid = (l + r)>>;
pushdown(rt);
int ans = ;
if(mid>=L)ans += query(l,mid,rt<<,L,R);
if(mid<R)ans += query(mid+,r,rt<<|,L,R);
return ans;
}
void update(int l,int r,int rt,int L,int R,int op)
{
if(l>=L&&r<=R)
{
if(op==){
st[rt].lazy = ;
st[rt].sum = r - l + ;
}
else {
st[rt].lazy = ;
st[rt].sum = ;
}
return;
}
int mid = (r+l)>>;
pushdown(rt);
if(mid>=L)update(l,mid,rt<<,L,R,op);
if(mid<R)update(mid+,r,rt<<|,L,R,op);
pushup(rt);
}
int bdfind(int st,int len)//二分查找
{
int le = st;
int ri = n;
int ans = -;
while(le <= ri)
{
int mid = (le + ri)>>;
int lenSum = query(,n,,st,mid); //找有花的瓶子个数
if(lenSum + len == mid - st + )
{
ans = mid; //不要直接return,要找最小的。
ri = mid - ;
}
else if(lenSum + len < mid - st +)
{
ri = mid - ;
}
else le = mid + ;
}
return ans;//找到在【st,mid】间符合len个空瓶;
}
int main(){
// freopen("input","r",stdin);
int t;
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &n, &m);
build(,n,);
while(m--)
{
int op;
int a,b,c;
scanf("%d%d%d", & op, & a,& b);
if(op==)
{
a++;
int st = bdfind(a,);
if(st==-)
puts("Can not put any one.");
else
{
int tmp = query(,n,,st,n);
tmp = (n - st + ) - tmp; //空瓶个数
b = min(tmp,b);
int ed = bdfind(a, b);
printf("%d %d\n",st-,ed-);
update(,n,,st,ed,);
}
}
else
{
a++,b++;
printf("%d\n",query(,n,,a,b));
update(,n,,a,b,);
}
}
puts("");
}
return ;
}
HDU4614Vases and Flowers 二分+线段树;的更多相关文章
- HDU4614 Vases and Flowers 二分+线段树
分析:感觉一看就是二分+线段树,没啥好想的,唯一注意,当开始摆花时,注意和最多能放的比大小 #include<iostream> #include<cmath> #includ ...
- HDU 4614 Vases and Flowers(二分+线段树区间查询修改)
描述Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to ...
- J - Joseph and Tests Gym - 102020J (二分+线段树)
题目链接:https://cn.vjudge.net/contest/283920#problem/J 题目大意:首先给你n个门的高度,然后q次询问,每一次询问包括两种操作,第一种操作是将当前的门的高 ...
- Educational Codeforces Round 61 D 二分 + 线段树
https://codeforces.com/contest/1132/problem/D 二分 + 线段树(弃用结构体型线段树) 题意 有n台电脑,只有一个充电器,每台电脑一开始有a[i]电量,每秒 ...
- 【BZOJ-3110】K大数查询 整体二分 + 线段树
3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 6265 Solved: 2060[Submit][Sta ...
- hdu6070 Dirt Ratio 二分+线段树
/** 题目:hdu6070 Dirt Ratio 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6070 题意:给定n个数,求1.0*x/y最小是多少.x ...
- K-th occurrence HDU - 6704 (后缀数组+二分线段树+主席树)
大意: 给定串s, q个询问(l,r,k), 求子串s[l,r]的第kk次出现位置. 这是一篇很好的题解: https://blog.csdn.net/sdauguanweihong/article/ ...
- hdu4614 Vases and Flowers【线段树】【二分】
Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to N ...
- HDU 4614 Vases and Flowers(线段树+二分)
Vases and Flowers Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others ...
随机推荐
- Python基础总结之第十天开始【认识一下python的另一个数据对象-----字典】(新手可相互督促)
看了大家的评论,还是有意外的收货.感谢每个小伙伴的评论与补充. 众人拾柴火焰高~ 今天的笔记是记录python中的数据对象----字典! 前面有讲到list列表和tuple元组的笔记,他们都是一样可以 ...
- 【iOS】安装 CocoaPods
1. 打开 terminal 2. 移除现有 Ruby 默认源 $ gem sources --remove https://rubygems.org/ 3. 使用新的源 $ gem sources ...
- 【iOS】PLA 3.3.12
发件人 Apple Program License Agreement PLA We found that your app uses the Advertising Identifier but d ...
- 【译】尝试使用Nullable Reference Types
随着.NET Core 3.0 Preview 7的发布,C#8.0已被认为是“功能完整”的.这意味着它们的最大亮点Nullable Reference Types,在行为方面也被锁定在.NET Co ...
- 【转】C++文件读写详解(ofstream,ifstream,fstream)
转:http://blog.csdn.net/kingstar158/article/details/6859379 摘要:具体用法,上面链接中,文章写的很详细,讲解ofstream,ifstream ...
- 微服务与网关技术(SIA-GateWay)
一.背景 软件架构,总是在不断的演进中... 把时间退回到二十年之前,当时企业级领域研发主要推崇的还是C/S模式,PB.Delphi这样的开发软件是企业应用开发的主流.随着时间的推移,基于浏览器的B/ ...
- 直击--vue项目微信小程序页面跳转web-view不刷新-根源
背景 最近项目需要适配小程序,项目是使用了vue开发的网站,其中改造方式是,每个页面都使用小程序创建一个页面通过web-view来显示指定页面的. 在没有使用小程序时,路由跳转时,刷新页面等等,这个是 ...
- 阿里巴巴_java后端面经
自我介绍不多说! 1 多线程有什么用?( 发挥多核CPU的优势 防止阻塞 便于建模 ) 2 怎么检测一个线程是否持有对象监视器( Thread类提供了一个holdsLock(Object obj)方法 ...
- Laya 中缩放的实现
Laya 缩放功能的实现 在 laya 中实现滚轮对选中对象的缩放,涉及到以下两个模块: 事件 容器坐标 1. 事件 在 Laya 中, Event 是事件类型的集合.包含了常见的鼠标事件.键盘事件. ...
- 第一章 .NET基础-C#基础
一.1.1. 基础语法 一.1.1.1. 注释符 一.1.1.1.1. 注释符的作用 l 注释 l 解释 一.1.1.1.2. C#中的3中注释符 l 单行注释 // l 多上注释 /* 要注释的内容 ...