Vases and Flowers

题目链接

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

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

题意

有一个初始全为零的序列,支持两种操作:

1.1 x y 从x开始往后找y个为0的位置并赋值为1,找不满没关系。输出未赋值时第一个为零位置和最后一个为零的位置,如果没有一个为零的位置输出 Can not put any one.

2.2 x y 输出x到y的和,并将x到y赋值成0

题解

主要操作就是区间覆盖和区间求和,至于操作一,我们可以二分查找,左区间就是x,有区间r二分,sum[x,r]随r单调不减,我们只要求最左边的sum[x,r]=1和sum[x,r]=y的位置即可,还有些细节可以仔细想想。

代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 0x7f7f7f7f
#define N 100050
int n,m;
template<typename T>void read(T&x)
{
ll k=0; char c=getchar();
x=0;
while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
if (c==EOF)exit(0);
while(isdigit(c))x=x*10+c-'0',c=getchar();
x=k?-x:x;
}
void read_char(char &c)
{while(!isalpha(c=getchar())&&c!=EOF);}
struct Node{int l,r,lazy,sum;};
struct segmentTree
{
Node tr[N<<2];
void push_up(int x);
void push_down(int x);
void bt(int x,int l,int r);
void update(int x,int l,int r,int tt);
int query(int x,int l,int r);
int ef(int k,int x,int l,int r);
}seg;
void segmentTree::push_up(int x)
{
int len=tr[x].r-tr[x].l+1;
if (len>1)tr[x].sum=tr[x<<1].sum+tr[x<<1|1].sum;
if (tr[x].lazy!=-1)tr[x].sum=tr[x].lazy*len;
}
void segmentTree::push_down(int x)
{
if (tr[x].lazy==-1)return;
tr[x<<1|1].lazy=tr[x<<1].lazy=tr[x].lazy;
push_up(x<<1);
push_up(x<<1|1);
tr[x].lazy=-1;
}
void segmentTree::bt(int x,int l,int r)
{
tr[x]=Node{l,r,0,0};
if (l==r)return;
int mid=(l+r)>>1;
bt(x<<1,l,mid);
bt(x<<1|1,mid+1,r);
}
void segmentTree::update(int x,int l,int r,int tt)
{
if (l<=tr[x].l&&tr[x].r<=r)
{
tr[x].lazy=tt;
push_up(x);
return;
}
int mid=(tr[x].l+tr[x].r)>>1;
push_down(x);
if (l<=mid)update(x<<1,l,r,tt);
if (mid<r)update(x<<1|1,l,r,tt);
push_up(x);
}
int segmentTree::query(int x,int l,int r)
{
if (l<=tr[x].l&&tr[x].r<=r)return tr[x].sum;
int mid=(tr[x].l+tr[x].r)>>1,ans=0;
push_down(x);
if (l<=mid)ans+=query(x<<1,l,r);
if (mid<r)ans+=query(x<<1|1,l,r);
return ans;
}
int segmentTree::ef(int k,int x,int l,int r)
{
if (l==r)return l;
int mid=(l+r)>>1;
int tp=mid-x+1-query(1,x,mid);
if (tp<k)return ef(k,x,mid+1,r);
if (tp>=k)return ef(k,x,l,mid);
}
void work()
{
read(n); read(m);
seg.bt(1,0,n-1);
for(int i=1;i<=m;i++)
{
int id,x,y;
read(id); read(x); read(y);
if (id==1)
{
int k=seg.query(1,x,n-1);
if (k==n-x){printf("Can not put any one.\n");continue;}
int ds=seg.ef(1,x,x,n-1);
int dw=seg.ef(min(n-x-k,y),x,x,n-1);
seg.update(1,ds,dw,1);
printf("%d %d\n",ds,dw);
}
if (id==2)
{
printf("%d\n",seg.query(1,x,y));
seg.update(1,x,y,0);
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
int T;
read(T);
while(T--)work(),printf("\n");
}

最新文章

  1. 一、常见PHP网站安全漏洞
  2. VS的安装
  3. Visual Studio 插件的开发(转)
  4. Windows消息机制详解
  5. Python socket超时
  6. Spring aop——前置增强和后置增强 使用注解Aspect和非侵入式配置
  7. JQuery快速入门
  8. TCP/IP详解--TCP首部选项中时间戳选项
  9. Grunt上手指南&lt;转&gt;
  10. ORACLE-RAC-11G-R2_INSTALL
  11. Unity3d Shortcuts
  12. DEM渲染洼地淹没图(转)
  13. (转)CSS行高——line-height
  14. HDU 1061 Rightmost Digit解决问题的方法
  15. Java常用API解析——序列化API
  16. Xcode9新特性介绍-中文篇
  17. 纯 CSS 实现波浪效果!
  18. Android App 压力测试方法(Monkey)
  19. 硬盘安装Kali
  20. react_app 项目开发 (8)_角色管理_用户管理----权限管理 ---- shouldComponentUpdate

热门文章

  1. ios高版本中select的option选项内容不显示问题
  2. mysql 使用service mysqld start 提示未识别服务 进入/etc/rc.d/init.d 下面未发现有mysqld解决方法
  3. centos7远程服务器中redis的安装与java连接
  4. tensorflow查看ckpt各节点名称
  5. 使用浏览器连接Linux服务器
  6. kvm管理工具Webvirtmgr安装
  7. jpa 总结
  8. 阶段5 3.微服务项目【学成在线】_day02 CMS前端开发_08-vuejs研究-vuejs基础-v-if和v-for指令
  9. 详解Pytorch中的网络构造,模型save和load,.pth权重文件解析
  10. SQL 里ESCAPE的用法