题目链接
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
[pre]3 7
2
1 9
4
Can not put any one.
2 6
2
0 9
4
4 5
2 3
[/pre]
Author
SYSU
Source
2013 Multi-University Training Contest 2
题意:给定操作1,代表在x位置起,要插y朵花,不过插的时候要插在值为0的位置上,插到最后位置(n)的时候必须停止,如果一朵都插不了输出Can not put any one.
,同时输出插的时候的左右端点。
操作2代表将输出【x,y】区间的数并将其清零
思路:用线段树来维护区间操作,至于左右端点的话用二分就可以了,注意细节。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5e4+5 ;
struct cxk{
int l,r,lazy,sum;
}tree[maxn<<2];
void pushup(int x)
{
tree[x].sum=tree[x<<1].sum+tree[x<<1|1].sum;
}
void pushdown(int x)
{
if(tree[x].lazy!=-1)
{
tree[x<<1].sum=(tree[x<<1].r-tree[x<<1].l+1)*tree[x].lazy;
tree[x<<1|1].sum=(tree[x<<1|1].r-tree[x<<1|1].l+1)*tree[x].lazy;
tree[x<<1].lazy=tree[x].lazy;
tree[x<<1|1].lazy=tree[x].lazy;
tree[x].lazy=-1;
}
}
void build(int x,int l,int r)
{
tree[x].l=l;tree[x].r=r;
if(l==r){
tree[x].sum=0;tree[x].lazy=-1;return ;
}
int mid=(l+r)>>1;
build(x<<1,l,mid);
build(x<<1|1,mid+1,r);
pushup(x);
}
void update(int x,int l,int r,int v)
{
if(l<=tree[x].l&&tree[x].r<=r)
{
tree[x].sum=(tree[x].r-tree[x].l+1)*v;
tree[x].lazy=v;
return ;
}
pushdown(x);
int mid=(tree[x].l+tree[x].r)>>1;
if(l<=mid) update(x<<1,l,r,v);
if(mid<r) update(x<<1|1,l,r,v);
pushup(x);
}
int query(int x,int l,int r)
{
if(l<=tree[x].l&&tree[x].r<=r) return tree[x].sum;
pushdown(x);
int mid=(tree[x].l+tree[x].r)>>1,ans=0;
if(l<=mid) ans+=query(x<<1,l,r);
if(mid<r) ans+=query(x<<1|1,l,r);
pushup(x);
return ans;
}
int check(int left,int right,int x)
{
int l=left,r=right;
while(l<=r)
{
int mid=(l+r)>>1;
if(mid-left+1-query(1,left,mid)>=x) r=mid-1;
else l=mid+1;
}
return l;
}
int main()
{
int T,x,y,n,m,op;
scanf("%d",&T);
while(T--)
{
memset(tree,0,sizeof(tree));
scanf("%d %d",&n,&m);
build(1,1,n);
while(m--)
{
scanf("%d %d %d",&op,&x,&y);
x++;
if(op==1)
{
int num=n-x+1-query(1,x,n);
if(num==0)
{
printf("Can not put any one.\n");continue;
}
int l=check(x,n,1),r=check(x,n,min(y,num));
printf("%d %d\n",l-1,r-1);
update(1,l,r,1);
}
else if(op==2){
y++,printf("%d\n",query(1,x,y)),update(1,x,y,0);
}
}
puts("");
}
}
本文介绍了一种使用线段树数据结构解决特定问题的方法。该问题涉及到一系列操作,包括在一组花瓶中放置花朵和清理花瓶,通过线段树能够高效地处理这些操作并返回所需结果。
&spm=1001.2101.3001.5002&articleId=105621756&d=1&t=3&u=b78ccf41a86a4c2a8ae7439a138cbe2f)
2085

被折叠的 条评论
为什么被折叠?



