Vases and Flowers

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

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.
 
Sample Input
2
10 5
1 3 5
2 4 5
1 1 8
2 3 6
1 8 8
 
10 6
1 2 5
2 3 4
1 0 8
2 2 5
1 4 4
1 2 3
 
Sample Output
3 7
2
1 9
4
Can not put any one.
 
2 6
2
0 9
4
4 5
2 3

题目链接:HDU 4614

题意就是给一个编号为0~N-1的花瓶,给一系列操作:1、从起始点S开始插入F朵花,插入时连续地从左到右扫描过去若花瓶有空且花没用完就一定要插到花瓶里,最后要么花用完了要么没位置可以放置了;2、清空编号从L到R花瓶。

这题第二问明显可以用线段树做,而且由于前缀和的缘故,花的前缀和数量在编号序列上具有单调性,因此第一问可以用二分做,但是二分开头简单,二分结尾就比较恶心,调试了很久,仔细考虑可以发现会有这样一个容易错误的地方:若从头到尾空位比花少,则终点为第一个达到最大空位的位置,而不是一直到结尾,因此要仅当当前位置的前缀空位和等于最大空位数的时候才记录终点T。如果二分结尾不正确估计第二组样例会对不上

代码:

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 50010;
struct seg
{
int l, mid, r;
int sum, add;
inline int len()
{
return r - l + 1;
}
};
seg T[N << 2]; inline void pushup(int k)
{
T[k].sum = T[LC(k)].sum + T[RC(k)].sum;
}
inline void pushdown(int k)
{
if (!T[k].add)
return ;
T[LC(k)].add = T[k].add;
T[RC(k)].add = T[k].add;
T[LC(k)].sum = (T[k].add == 1) * T[LC(k)].len();
T[RC(k)].sum = (T[k].add == 1) * T[RC(k)].len();
T[k].add = 0;
}
void build(int k, int l, int r)
{
T[k].l = l;
T[k].r = r;
T[k].mid = MID(l, r);
T[k].sum = T[k].add = 0;
if (l == r)
return ;
else
{
build(LC(k), l, T[k].mid);
build(RC(k), T[k].mid + 1, r);
}
}
void update(int k, int l, int r, int flag)
{
if (l <= T[k].l && T[k].r <= r)
{
T[k].add = flag;
T[k].sum = (flag == 1 ? T[k].len() : 0);
}
else
{
pushdown(k);
if (l <= T[k].mid)
update(LC(k), l, r, flag);
if (r > T[k].mid)
update(RC(k), l, r, flag);
pushup(k);
}
}
int query(int k, int l, int r)
{
if (l <= T[k].l && T[k].r <= r)
return T[k].sum;
else
{
pushdown(k);
if (r <= T[k].mid)
return query(LC(k), l, r);
else if (l > T[k].mid)
return query(RC(k), l, r);
else
return query(LC(k), l, T[k].mid) + query(RC(k), T[k].mid + 1, r);
}
}
int main(void)
{
int tcase, n, m, ops, l, r;
scanf("%d", &tcase);
while (tcase--)
{
scanf("%d%d", &n, &m);
build(1, 0, n - 1);
while (m--)
{
scanf("%d%d%d", &ops, &l, &r);
if (ops == 2)
{
int discard = query(1, l, r);
update(1, l, r, -1);
printf("%d\n", discard);
}
else if (ops == 1)
{
int S = -1; //二分找到第一个为空的点
int L = l, need = r, R = n - 1;
while (L <= R)
{
int mid = (L + R) >> 1;
if (query(1, l, mid) < mid - l + 1)
{
S = mid;
R = mid - 1;
}
else
L = mid + 1;
}
if (S == -1)//连开头位置都不存在,肯定位置全满了
puts("Can not put any one.");
else
{
int maxmvacnt = (n - 1 - S + 1) - query(1, S, n - 1);//用实际上最大的空位数而不是花朵数去二分
need = min<int>(need, maxmvacnt);
int T = -1;
int L = S, R = n - 1;
while (L <= R)
{
int mid = (L + R) >> 1;
int vacant = mid - S + 1 - query(1, S, mid);
if (vacant < need)
L = mid + 1;
else if (vacant == need) //此处与普通二分有一点不同
{
T = mid;
R = mid - 1;
}
else
R = mid - 1;
}
printf("%d %d\n", S, T);
update(1, S, T, 1);
}
}
}
putchar('\n');
}
return 0;
}

HDU 4614 Vases and Flowers(线段树+二分)的更多相关文章

  1. HDU 4614 Vases and Flowers(线段树+二分)

    题目链接 比赛的时候一直想用树状数组,但是树状数组区间更新之后,功能有局限性.线段树中的lz标记很强大,这个题的题意也挺纠结的. k = 1时,从a开始,插b个花,输出第一个插的位置,最后一个的位置, ...

  2. hdu 4614 Vases and Flowers 线段树

    题目链接 一共n个盒子, 两种操作, 第一种是给出两个数x, y, 从第x个盒子开始放y朵花, 一个盒子只能放一朵, 如果某个盒子已经有了, 那么就跳过这个盒子放下面的盒子. 直到花放完了或者到了最后 ...

  3. hdu4614 Vases and Flowers 线段树+二分

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4614 题意: 给你N个花瓶,编号是0  到 N - 1 ,初始状态花瓶是空的,每个花瓶最多插一朵花. ...

  4. HDU 4614 Vases and Flowers(二分+线段树区间查询修改)

    描述Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to ...

  5. HDU 4614 Vases and Flowers 【线段树】+【二分】

    <题目链接> 题目大意: 有n个花瓶,每个花瓶中只能放一朵花.两种操作,一种是从A开始放F朵花,如果有的花瓶中已经有花则跳过这个花瓶,往下一个花瓶放:第二种是将区间[A,B]之间花瓶中的花 ...

  6. 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 ...

  7. HDU 4614 Vases and Flowers(线段树+记录区间始末点或乱搞)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4614 题目大意:有n个空花瓶,有两种操作: 操作①:给出两个数字A,B,表示从第A个花瓶开始插花,插B ...

  8. HDU 4614 Vases and Flowers (2013多校第二场线段树)

    题意摘自:http://blog.csdn.net/kdqzzxxcc/article/details/9474169 ORZZ 题意:给你N个花瓶,编号是0 到 N - 1 ,初始状态花瓶是空的,每 ...

  9. hdu 4614 Vases and Flowers(线段树:成段更新)

    线段树裸题.自己写复杂了,准确说是没想清楚就敲了. 先是建点为已插花之和,其实和未插花是一个道理,可是开始是小绕,后来滚雪球了,跪了. 重新建图,分解询问1为:找出真正插画的开始点和终止点,做成段更新 ...

随机推荐

  1. 【洛谷2577】[ZJOI2005] 午餐(较水DP)

    点此看题面 大致题意: 有\(N\)个学生去食堂打饭,每个学生有两个属性:打饭时间\(a_i\)和吃饭时间\(b_i\).现要求将这些学生分成两队分别打饭,求最早何时所有人吃完饭. 贪心 首先,依据贪 ...

  2. 【转】iOS 上常用的两个功能:点击屏幕和return退出隐藏键盘和解决虚拟键盘挡住UITextField的方法

    iOS上面对键盘的处理很不人性化,所以这些功能都需要自己来实现, 首先是点击return和屏幕隐藏键盘 这个首先引用双子座的博客 http://my.oschina.net/plumsoft/blog ...

  3. Windows环境下在Oracle VM VirtualBOX下克隆虚拟机镜像(克隆和导入)

    Windows环境下在Oracle VM VirtualBOX下克隆虚拟机镜像: 注:直接复制一个.vdi 虚拟硬盘再挂上去就可以,但Virtualbox居然提示UUID重复,无法使用. 则,可以通过 ...

  4. 解决ssh登录慢,等待时间长的问题

    有时候在ssh远程登录到其他主机上时发现登录时间太长,经过亲自测试,发现主要有两个问题会导致ssh登录慢: 1.使用了dns反查,这样的话当ssh某个IP时,系统会试图通过DNS反查相对应的域名,如果 ...

  5. ubuntu install oracle jdk

    .Download the required tarball from here .unzip this tarball using "tar -zxvf tarball_name .cre ...

  6. vue使用animate.css类库实现动画

    首先安装animate.css类库 cnpm install animate.css –save 然后在vue的script文件中引用 import $ from '../assets/js/jque ...

  7. Vmware 不能上网

    Vmware 安装 WIN7 不能上网,如何解决? 情况一: 虚拟机右下角出现红色叉号,检查物理的服务是否开启“VMware NAT Service” 1 .开启方法:WIN + R -> 输入 ...

  8. bat 服务启动脚本

    当电脑上有多个数据库(特别是Oracle,占用内存大,所以我都是设置为手动启动的,或者想在电脑上运行一下其他UI类软件或玩些游戏的时候也需要暂时关掉,奈何我这渣机(V_V))需要启动或停止的时候,就用 ...

  9. 精致的系统监控工具-netdata

    今天在网上瞎逛,偶然发现一款监控工具:netdata,感到一惊,监控工具竟然可以这么漂亮! 简单了解一下,这款工具还算比较新,监控系统运行状态的功能非常强大,除了监控cpu,网卡,磁盘,内存,进程等等 ...

  10. k8s的secret基本概念及案例

    secret相对于configMap,功能上是相似的但是secret是以其他编码方式去记录配置信息的,但是也可以被解读,只不过有技术门槛,不是那么容易就被解读.使用base64可以解码:echo ** ...