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 ...
随机推荐
- RabbitMQ搭建单机及集群
1,基本环境配置 hosts 文件 免密登录 2,访问官网 https://www.rabbitmq.com/download.html 3, 4,安装依赖 yum -y install make g ...
- 【Android】drawable VS mipmap
Android Studio 创建工程后默认的资源文件夹如下图所示: 一直有些疑惑的是 mipmap 和 drawable 文件夹有什么区别,以及是否还需要创建 drawable-xhdpi, dra ...
- C# 10分钟完成百度图片提取文字(文字识别)——入门篇
现在图片文字识别已经很成熟了,比如qq长按图片,点击图片识别就可以识别图片的文字,将不认识的.文字数量大的.或者不能赋值的值进行二次可复制功能. 我们现在就基于百度Ai开放平台进行个人文字识别,dem ...
- HashMap、Hash Table、ConcurrentHashMap
这个这个...本王最近由于开始找实习工作了,所以就在牛客网上刷一些公司的面试题,大多都是一些java,前端HTML,js,jquery,以及一些好久没有碰的算法题,说实话,有点难受,其实在我不知道的很 ...
- 转载 vue-awesome-swiper - 基于vue实现h5滑动翻页效果
说到h5的翻页,很定第一时间想到的是swiper.但是我当时想到的却是,vue里边怎么用swiper?! 中国有句古话叫:天塌下来有个高的顶着. 在前端圈里,总有前仆后继的仁人志士相继挥洒着热汗(这里 ...
- 有助于提高"锁"性能的几点建议
有助于提高"锁"性能的几点建议 1.减少锁持有时间 public synchronized void syncMethod() { othercode1(); mutextMeth ...
- Wtm携手LayUI -- .netcore 开源生态我们是认真的!
经过WTM团队和LayUI团队多次深入协商,双方于2019年7月29日在北京中国国际展览中心正式达成战略合作意向, 双方签署了战略合作框架协议,LayUI团队承诺使用WTM框架的任何项目都可以免费使用 ...
- eclipse的下载安装配置
1.在eclipse官网下载与你电脑版本相对应的安装包.链接:https://www.eclipse.org/downloads/eclipse-packages/ 2.下载与eclipse版本相对应 ...
- Hadoop 系列(二)—— 集群资源管理器 YARN
一.hadoop yarn 简介 Apache YARN (Yet Another Resource Negotiator) 是 hadoop 2.0 引入的集群资源管理系统.用户可以将各种服务框架部 ...
- .NETCoreCSharp 中级篇2-3 Linq简介
.NETCoreCSharp 中级篇2-3 本节内容为Linq及其拓展方法.Linq中表达式树的使用 简介 语言集成查询(LINQ)是一系列直接将查询功能集成到C#语言的技术统称.数据查询历来都表示为 ...