hdu4614(二分法+线段树)

来源:互联网 发布:可以做引流网站的源码 编辑:程序博客网 时间:2024/04/29 08:17

Vases and Flowers

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


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
210 51 3 52 4 51 1 82 3 61 8 810 61 2 52 3 41 0 82 2 51 4 41 2 3
 

Sample Output
[pre]3 721 94Can not put any one.2 620 944 52 3[/pre]
 
 
这是个比较裸的线段树,但比赛时没有想到二分查找法+线段树,一直希望设一个左右第一个为1的两个变量,结果搞来搞去没搞出来
这两天一直没看解题报告,终于自己写出来了,枚举思想万岁,二分查找加快枚举速度。
 
#include<iostream>#include<cstdio>using namespace std;#define MAX 50000struct node {int left,right,num,tag;}tree[MAX*3];/*int ml,mr;int Min(int a,int b){return a<b?a:b;}int Max(int a,int b){return a>b?a:b;}*/void build( int id, int left, int right ){    tree[id].left = left;    tree[id].right = right;tree[id].num=right-left+1;tree[id].tag=0;    if( left == right )    {return ;    }    else    {        int mid = ( left + right )/2;                build( id * 2, left, mid );        build( id * 2 + 1, mid + 1, right );}}int qurry(int id,int l,int r){if(tree[id].tag!=0){if(tree[id].tag==1)return r-l+1;else return 0;}if(tree[id].left==l&&tree[id].right==r){return tree[id].num;}int mid=(tree[id].left+tree[id].right)/2;if(r<=mid)return qurry(id*2,l,r);else if(l>mid)return qurry(id*2+1,l,r);else{return qurry(id*2,l,mid)+qurry(id*2+1,mid+1,r);}}void updata(int id,int l,int r,int v){    if(tree[id].left==l&&tree[id].right==r)    {tree[id].tag=v;if(v==-1)tree[id].num=0;else tree[id].num=r-l+1;return ;    }if(tree[id].tag!=0){tree[id*2].tag=tree[id].tag;tree[id*2+1].tag=tree[id].tag;if(tree[id].tag==-1){tree[id*2].num=0;    tree[id*2+1].num=0;}else if(tree[id].tag==1){tree[id*2].num=(tree[id*2].right-tree[id*2].left+1);        tree[id*2+1].num=(tree[id*2+1].right-tree[id*2+1].left+1);;}tree[id].tag=0;}tree[id].tag=0;int mid=(tree[id].left+tree[id].right)/2;    if(r<=mid) updata(id * 2, l,r,v);else if(l>mid)updata(id * 2+1, l,r,v);    else {updata(id*2,l,mid,v);updata(id*2+1,mid+1,r,v);}tree[id].num=tree[id*2].num+tree[id*2+1].num;}/*int getup(int id,int st,int sum){if(tree[id].tag!=0){tree[id*2].tag=tree[id].tag;tree[id*2+1].tag=tree[id].tag;tree[id].tag=0;}if(tree[id].num==sum){//cout<<"mr="<<mr<<"  tree[id].left="<<tree[id].left<<endl;mr=Min(mr,tree[id].left);return 1;}int mid=(tree[id].left+tree[id].right)/2;if(st<=mid){if(tree[id*2+1].num>=sum){//cout<<"  tree[id*2+1].num="<<tree[id*2+1].num<<"到左"<<"  sum="<<sum<<endl;return getup(id * 2+1, mid+1,sum);}else {//cout<<"到右"<<endl;return getup(id*2,st,sum-tree[id*2+1].num);}}else{return getup(id * 2+1, st,sum);}}*/int main(){int n,i,cas,a,b,k,m,tmp,ansl,ansr;cin>>cas;while(cas--){scanf("%d%d",&n,&m);build(1,1,n);for(i=0;i<m;i++){scanf("%d%d%d",&k,&a,&b);if(1==k){a++;int high,low,mid;tmp=qurry(1,a,n);if(tmp==0)printf("Can not put any one.\n");else{//二分法求下界high=n,low=a;ansl=a;while(low<=high){mid=(high+low)/2;tmp=qurry(1,a,mid);if(tmp>0)ansl=mid,high=mid-1;else low=mid+1;}//二分法求上界high=n,low=a;ansr=n;tmp=qurry(1,a,n);if(tmp<b)b=tmp;while(low<=high){mid=(high+low)/2;tmp=qurry(1,a,mid);if(tmp>=b)ansr=mid,high=mid-1;else low=mid+1;}updata(1,ansl,ansr,-1);printf("%d %d\n",ansl-1,ansr-1);}}else {a++,b++;tmp=qurry(1,a,b);//[a,b]之间有多少不用修改updata(1,a,b,1);printf("%d\n",b-a+1-tmp);}}printf("\n");}return 0;}

原创粉丝点击