hdu 4614 线段树
思路:当k为1的时候,用二分法查询包含有f个空瓶的上界r,然后更新会方便很多,直接更新区间(a,r)了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define lson(x) (x<<1)
#define rson(x) ((x<<1)+1)
#define mid ((tree[po].l+tree[po].r)>>1)
#define inf 1<<30
#define Maxm 1000000
#define Maxn 60000
using namespace std;
struct Tree{
int l,r,left,up;
}tree[Maxm];
int flag=,ans,fir;
void buildTree(int l,int r,int po)
{
tree[po].l=l,tree[po].r=r;
tree[po].left=r-l+;
tree[po].up=;
if(l==r)
return ;
buildTree(l,mid,lson(po));
buildTree(mid+,r,rson(po));
}
void down(int po)
{ if(!tree[po].left)
{
tree[po].up=;
tree[lson(po)].left=;
tree[rson(po)].left=;
tree[rson(po)].up=;
tree[lson(po)].up=;
return ;
}
if(!tree[po].up) return ;
tree[lson(po)].left=mid-tree[po].l+;
tree[rson(po)].left=tree[po].r-mid;
tree[rson(po)].up=;
tree[lson(po)].up=;
tree[po].up=;
}
void update(int l,int r,int po)
{
if(l<=tree[po].l&&r>=tree[po].r)
{
ans+=tree[po].r-tree[po].l+-tree[po].left;
tree[po].left=tree[po].r-tree[po].l+;
tree[po].up=;
return ;
}
down(po);
if(r<=mid)
update(l,r,lson(po));
else
if(l>mid)
update(l,r,rson(po));
else{
update(l,r,lson(po));
update(l,r,rson(po));
}
tree[po].left=tree[lson(po)].left+tree[rson(po)].left;
}
int findTree(int l,int r,int po)
{
if(l<=tree[po].l&&r>=tree[po].r)
{
return tree[po].left;
}
down(po);
int temp=;
if(r<=mid)
temp=findTree(l,r,lson(po));
else
if(l>mid)
temp=findTree(l,r,rson(po));
else{
temp=findTree(l,r,lson(po));
temp+=findTree(l,r,rson(po));
}
tree[po].left=tree[lson(po)].left+tree[rson(po)].left;
return temp;
}
void Insert(int l,int r,int po)
{
if(l<=tree[po].l&&r>=tree[po].r)
{
if(tree[po].left==tree[po].r-tree[po].l+&&!flag)
flag=,fir=tree[po].l;
if(flag)
{
tree[po].left=;
return ;
}
}
if(!tree[po].left)
return ;
down(po);
if(r<=mid)
Insert(l,r,lson(po));
else
if(l>mid)
Insert(l,r,rson(po));
else{
Insert(l,r,lson(po));
Insert(l,r,rson(po));
}
tree[po].left=tree[lson(po)].left+tree[rson(po)].left;
}
int main()
{
int n,m,k,a,b,i,j,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
buildTree(,n-,);
for(i=;i<=m;i++)
{
scanf("%d%d%d",&k,&a,&b);
if(k==)
{
flag=;
int l,r,Mid,temp;
temp=findTree(a,n-,);
if(temp<b)
b=temp;
l=a,r=n-;
while(r>l)
{
Mid=(l+r)>>;
temp=findTree(a,Mid,);
if(temp>=b)
r=Mid;
else
l=Mid+;
}
Insert(a,r,);
if(!flag)
printf("Can not put any one.\n");
else
printf("%d %d\n",fir,r);
}
else
{
ans=;
update(a,b,);
printf("%d\n",ans);
}
}
printf("\n");
}
return ;
}
hdu 4614 线段树的更多相关文章
- L - Vases and Flowers HDU - 4614 线段树+二分
题意 给出一排空花瓶 有两种操作 1是 从A花瓶开始放F朵花 如果当前瓶有花就跳过前往下一个 直到花用完或者 瓶子到了最后一个为止 输出 成功放花的第一个和最后一个 如果没有输出 can not. ...
- HDU 4614 线段树+二分查找
Vases and Flowers 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4614 Problem Description Alice is s ...
- hdu 5877 线段树(2016 ACM/ICPC Asia Regional Dalian Online)
Weak Pair Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- hdu 3974 线段树 将树弄到区间上
Assign the task Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 3436 线段树 一顿操作
Queue-jumpers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- hdu 3397 线段树双标记
Sequence operation Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- hdu 4578 线段树(标记处理)
Transformation Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 65535/65536 K (Java/Others) ...
- hdu 4533 线段树(问题转化+)
威威猫系列故事——晒被子 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Tot ...
- hdu 2871 线段树(各种操作)
Memory Control Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
随机推荐
- JDBC学习笔记(1)——JDBC概述
JDBC JDBC API是一个Java API,可以访问任何类型表列数据,特别是存储在关系数据库中的数据.JDBC代表Java数据库连接. JDBC库中所包含的API任务通常与数据库使用: 连接到数 ...
- How to include JavaScript file in JSF
In JSF 2.0, you can use <h:outputScript /> tag to render a HTML "script" element, an ...
- codeforces 337D 树形DP Book of Evil
原题直通车:codeforces 337D Book of Evil 题意:一棵n个结点的树上可能存在一个Evil,Evil危险范围为d,即当某个点与它的距离x<=d时,那么x是危险的. 现已知 ...
- POJ 3922A Simple Stone Game
题目链接 A Sample Stone Game 题目大意:给定n,k,表示最初时有n个石头,两个人玩取石子游戏,第一个人第一次可以取1~n-1个石头,后面每个人最多可以拿走前面一个人拿走的个数的K倍 ...
- Arduino Due, Maple and Teensy3.0 的 W5200性能测试
开源平台中以太网连接方案里W5100是众所周知的,W5200正在此领域越来越受欢迎.这个测试结果是在Arduino Due(Atmel CortexM3-84Mhz), Maple(ST Cortex ...
- 使用 EPPlus,NPOI,操作EXCEL
NPOI, 读取xls文件(Excel2003及之前的版本) (NPOI.dll+Ionic.Zip.dll) http://npoi.codeplex.com/ EPPlus ...
- 应用XML作为数据库的快速开发框架
背景 我经常应用C#开发一些小的桌面程序,这些桌面程序往往有以下几个特点: 程序比较小,开发周期很短. 程序的数据量不大,多数情况下不超过1万行记录. 对程序的性能要求不高. 程序并发很少或者基本没有 ...
- Could not allocate CursorWindow size due to error -12 错误解决方法
04-29 11:13:54.284 13584-13584/com.uniubi.smartfrontdesk E/art: Throwing OutOfMemoryError "pthr ...
- [原]生产环境下的nginx.conf配置文件(多虚拟主机)
[原]生产环境下的nginx.conf配置文件(多虚拟主机) 2013-12-27阅读110 评论0 我的生产环境下的nginx.conf配置文件,做了虚拟主机设置的,大家可以根据需求更改,下载即可在 ...
- [Angular 2] Understanding @Injectable
In order to resolve a dependency, Angular’s DI uses type annotations. To make sure these types are p ...