hdu 4614 Vases and Flowers(线段树)

Vases and Flowers

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


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
 
 
用一个lzay标记区间为空或者区间为满。。
然后对于操作1的话,,就不断更新 x , x + v - 1 使它们为满 , 直到 x + v - 1 为 n - 1
操作 2 的话就是一个区间求和,,还有清空
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstring>
#include <map>
#include <queue>
using namespace std;
typedef long long LL ;
typedef pair<int,int> pii ;
#define X first
#define Y second
#define root 1,n,1
#define lr rt<<1
#define rr rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int N = 200010;
const int mod = 10007;
int n , m , ansl , ansr , v ;
int lazy[N<<2] , date[N<<2];

void build( int l , int r , int rt ) {
    date[rt] = lazy[rt] = 0 ;
    if( l == r ) return ;
    int mid = (l+r)>>1;
    build(lson),build(rson);
}

void Up( int rt ) {
    date[rt] = date[lr] + date[rr];
}

void Down( int l , int r , int rt ) {
    if( lazy[rt] == 1 ) {
        date[lr] = 0 ; date[rr] = 0 ;
        lazy[lr] = 1 ; lazy[rr] = 1 ;
    }
    else if( lazy[rt] == -1 ) {
        int mid = (l+r)>>1;
        date[lr] = mid-l+1 , date[rr] = r-mid;
        lazy[lr] = -1 , lazy[rr]= -1 ;
    }
    lazy[rt] = 0 ;
}

void update( int l , int r , int rt , int L , int R ) {
    if( date[rt] == r - l + 1 ) return ;
    if( L == l && r == R && date[rt] == 0 ) {
        ansl = min( ansl , l ); ansr = max( ansr , r );
        v -= ( r - l + 1 ) ;
        date[rt] = r - l + 1 ;
        lazy[rt] = -1 ;
        return ;
    }
    Down( l , r , rt ) ;
    int mid = (l+r) >> 1;
    if( R <= mid ) update(lson,L,R);
    else if( L > mid ) update(rson,L,R);
    else update(lson,L,mid) , update(rson,mid+1,R);
    Up(rt);
}

int clean( int l , int r , int rt , int L , int R ) {
    int res = 0 ;
    if( l == L && R == r ) {
        int tmp = date[rt] ;
        date[rt] = 0 ; lazy[rt] = 1 ;
        return tmp ;
    }
    Down(l,r,rt) ;
    int mid = (l+r)>>1;
    if( R <= mid ) res = clean(lson,L,R) ;
    else if( L > mid ) res = clean(rson,L,R);
    else res = clean(lson,L,mid) + clean(rson,mid+1,R) ;
    Up(rt);
    return res ;
}

int main()
{
    #ifdef LOCAL
       freopen("in.txt","r",stdin);
       //freopen("out.txt","w",stdout);
    #endif // LOCAL
    int _ , cas = 1 ;
    scanf("%d",&_);
    while( _-- ) {
        scanf("%d%d",&n,&m);
        build( root );
        while( m-- ) {
            int op , x , y ;
            scanf("%d%d%d",&op,&x,&y);
            if( op == 1 ) {
                x++;
                ansl = n - 1 , ansr = 0 ; v = y ;
                while( true ) {
                    y = min( n , x + v - 1 ) ;
                    int tmpv = v ;
                    update( root , x , y );
                    if( v <= 0 || y == n ) break ;
                    x += tmpv ;
                }
                if( ansl > ansr ) puts("Can not put any one.");
                else printf("%d %d\n",ansl-1,ansr-1);
            }
            else {
                x++ , y++ ;
                printf("%d\n",clean(root,x,y));
            }
        }
        puts("");
    }
}
View Code

 

posted @ 2015-02-06 14:58  hl_mark  阅读(173)  评论(0编辑  收藏  举报