Vases and Flowers

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 347    Accepted Submission(s): 108

Problem Description
Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to N-1. When she receive some flowers, she will try to put them in the vases, one flower in one vase. She randomly choose the vase A and try to put a flower in the vase. If the there is no flower in the vase, she will put a flower in it, otherwise she skip this vase. And then she will try put in the vase A+1, A+2, ..., N-1, until there is no flower left or she has tried the vase N-1. The left flowers will be discarded. Of course, sometimes she will clean the vases. Because there are too many vases, she randomly choose to clean the vases numbered from A to B(A <= B). The flowers in the cleaned vases will be discarded.
 
Input
The first line contains an integer T, indicating the number of test cases.

For each test case, the first line contains two integers N(1 < N < 50001) and M(1 < M < 50001). N is the number of vases, and M is the operations of Alice. Each of the next M lines contains three integers. The first integer of one line is K(1 or 2). If K is 1, then two integers A and F follow. It means Alice receive F flowers and try to put a flower in the vase A first. If K is 2, then two integers A and B follow. It means the owner would like to clean the vases numbered from A to B(A <= B).
 
Output
For each operation of which K is 1, output the position of the vase in which Alice put the first flower and last one, separated by a blank. If she can not put any one, then output 'Can not put any one.'. For each operation of which K is 2, output the number of discarded flowers.
 


Output one blank line after each test case.

http://acm.hdu.edu.cn/showproblem.php?pid=4614

思路:很基本的线段树问题,我们可以用线段树维护每一个区间的剩余空间数量(也就是可以插画的地方),对于第一个询问,我们计算区间[a,n-1]所剩余的空间数,假设为num,若为0,则输出 “Can not put any one.”,否则若小于F,则将F设为num,接下来可以二分区间[a,mid]来确定插入的第一只花和最后一只花的位置l,r,然后将区间[l,r]全赋值为1即可,对于询问2,则计算出区间[a,b]所剩余的空间num,然后 b-a+1-num即为答案。最后将区间[a,b]赋值为1.以上,下面是代码.

#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
#define maxn 50010
#define mid ((t[p].l+t[p].r)>>1)
#define ls (p<<1)
#define rs (ls|1)
using namespace std;
struct tree
{
int l,r;
int sum;
int lazy;
}t[maxn<<2];
void pushup(int p)
{
t[p].sum=t[ls].sum+t[rs].sum;
}
void pushdown(int p)
{
if(t[p].lazy==0)
{
t[ls].lazy=0;
t[rs].lazy=0;
t[ls].sum=0;
t[rs].sum=0;
t[p].lazy=-1;
}
else if(t[p].lazy==1)
{
t[ls].lazy=1;
t[rs].lazy=1;
t[ls].sum=t[ls].r-t[ls].l+1;
t[rs].sum=t[rs].r-t[rs].l+1;
t[p].lazy=-1;
}
}
void build(int p,int l,int r)
{
t[p].l=l,t[p].r=r,t[p].sum=1,t[p].lazy=-1;
if(l==r)
return;
build(ls,l,mid);
build(rs,mid+1,r);
pushup(p);
}
void change(int p,int l,int r,int val)
{
if(t[p].l==l&&t[p].r==r)
{
t[p].lazy=val;
if(val)
{
t[p].sum=r-l+1;
}
else
t[p].sum=0;
return;
}
pushdown(p);
if(l>mid)
change(rs,l,r,val);
else if(r<=mid)
change(ls,l,r,val);
else
{
change(ls,l,mid,val);
change(rs,mid+1,r,val);
}
pushup(p);
}
int query(int p,int l,int r)
{
if(t[p].l==l&&t[p].r==r)
{
return t[p].sum;
}
pushdown(p);
if(l>mid)
return query(rs,l,r);
else if(r<=mid)
return query(ls,l,r);
else
return query(ls,l,mid)+query(rs,mid+1,r);
}
int main()
{
//freopen("dd.txt","r",stdin);
int ncase;
scanf("%d",&ncase);
while(ncase--)
{
int n,m,i;
scanf("%d%d",&n,&m);
build(1,0,n-1);
int k,a,b;
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&k,&a,&b);
if(k==1)
{
int ttmp=query(1,a,n-1);
if(ttmp==0)
{
printf("Can not put any one.\n");
}
else
{
if(ttmp<b)
b=ttmp;
int mi=a,ma=n-1,Mid;
int l,r;
while(mi<=ma)
{
Mid=(mi+ma)>>1;
if(query(1,a,Mid)>0)
{
l=Mid;
ma=Mid-1;
}
else
mi=Mid+1;
}
mi=a,ma=n-1;
while(mi<=ma)
{
Mid=(mi+ma)>>1;
if(query(1,a,Mid)>=b)
{
r=Mid;
ma=Mid-1;
}
else
mi=Mid+1;
}
change(1,l,r,0);
printf("%d %d\n",l,r);
}
}
else
{
printf("%d\n",b-a+1-query(1,a,b));
change(1,a,b,1);
} }
printf("\n");
}
return 0;
}

2013 多校联合2 D Vases and Flowers (hdu 4614)的更多相关文章

  1. 2013 多校联合 F Magic Ball Game (hdu 4605)

    http://acm.hdu.edu.cn/showproblem.php?pid=4605 Magic Ball Game Time Limit: 10000/5000 MS (Java/Other ...

  2. L - Vases and Flowers - hdu 4614(区间操作)

    题意:有两种操作,第一种从A开始插花,如果有花就跳到下一个,然后输出最后一个花瓶的编号,如果花瓶不够把多余的花丢掉.操作2把区间清空 分析:很明显的线段树操作,就是插花的时候麻烦一下,需要先找出来他剩 ...

  3. L - Vases and Flowers HDU - 4614 线段树+二分

    题意 给出一排空花瓶 有两种操作  1是 从A花瓶开始放F朵花 如果当前瓶有花就跳过前往下一个 直到花用完或者 瓶子到了最后一个为止 输出 成功放花的第一个和最后一个  如果没有输出 can not. ...

  4. 2013多校联合2 I Warm up 2(hdu 4619)

    Warm up 2 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total ...

  5. 2013 多校联合 2 A Balls Rearrangement (hdu 4611)

    Balls Rearrangement Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Othe ...

  6. 2013多校联合3 G The Unsolvable Problem(hdu 4627)

    2013-07-30 20:35 388人阅读 评论(0) 收藏 举报 http://acm.hdu.edu.cn/showproblem.php?pid=4627 The Unsolvable Pr ...

  7. HDU 4614 Vases and Flowers (2013多校2 1004 线段树)

    Vases and Flowers Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others ...

  8. 2016暑假多校联合---Rikka with Sequence (线段树)

    2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...

  9. 2016暑假多校联合---Windows 10

    2016暑假多校联合---Windows 10(HDU:5802) Problem Description Long long ago, there was an old monk living on ...

随机推荐

  1. 【Hybrid App】一个产品经理眼中的PhoneGap Vs. AppCan

    首先在写这篇文章前,必须先申明一下,本人是技术出身,对HTML技术及手机客户端都有过编程经验,只是出于工作岗位的变动,便没有再具体代码工作,以下文章涉及的中间件的基本代码实现及前期的API使用,都是自 ...

  2. 创建组合索引SQL从1个多小时到1S的案例

    select aa.acct_org, aa.loan_acct_no, aa.FUNCTIONARY, aa.cust_no, sum(dwm.pkg_tools.currcdtran(bb.INT ...

  3. BZOJ1782: [Usaco2010 Feb]slowdown 慢慢游

    1782: [Usaco2010 Feb]slowdown 慢慢游 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 541  Solved: 326[Sub ...

  4. 【git】git常用命令简介

    使用Git也好长时间了,但一直没系统的学习过,现在总结以下工作中用到的,记录以下,以后忘记了可以来看看. 因为操作系统是Windows,所以将简单介绍一下通过git bash命令行的使用: 本文将不介 ...

  5. (转载)PHP使用header函数设置HTTP头的示例方法表头

    (转载)http://justcoding.iteye.com/blog/601117/ 代码: //定义编码 header( 'Content-Type:text/html;charset=utf- ...

  6. COALESCE函数

    --SQL学习笔记一 --函数coalesce --功能返回参数中第一个非NULL值 --语法 COALESCE ( expression [ ,n ] ) --创建测试表 IF OBJECT_ID( ...

  7. RMQ——[USACO Jan07] 均衡队形题解

    题目:[USACO Jan07] 均衡队形 描述: 题目描述 农夫约翰的 N (1 ≤ N ≤ 50,000) 头奶牛,每天挤奶时总会按同样的顺序站好.一日,农夫约翰决定为奶牛们举行一个“终极飞盘”比 ...

  8. Spring JDBC 随笔

    Spring 框架,借助 JdbcTemplate 类来简化 java 访问 database. 完成一个增查改删(CRUD)的基本功能,借助下面 5 个角色来共同来完成. 1. object cla ...

  9. NSOutputStream\NSInputStream

    NSOutputStream-保存网络资源到本地 _filePath = [[NetworkManager sharedInstance] pathForTemporaryFileWithPrefix ...

  10. 基于Equinox构建OSGi项目

    几种OSGi框架 Several independently implemented OSGi frameworks exist today, including four that are avai ...