HDOJ 4614 —— 线段树区间更新&区间求和

Vases and Flowers

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


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]
 

Source
 

Recommend
zhuyuanchen520
 
题意是给你n个花瓶,一开始都是空的,你有2个操作:
一:1 L k 代表从L开始向花瓶中插k朵花,每个花瓶一朵,如果当前花瓶有花就插到它的下一个花瓶。输出插花的第一个花瓶和最后一个花瓶。
二: 2 L R 代表倒掉L 到R区间内花瓶中的花,输出倒花的总数。
思路:我们对花瓶建一棵线段树,维护sum,val,flag三个域。
sum表示区间内的空花瓶数,val表示该花瓶是否有花:1为没有,0为有,flag为延时标记。
然后二分去找第一个花瓶和最后一个花瓶即可。
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>

using namespace std;
//#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
//#define mid ((l + r) >> 1)
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 50000 + 500;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x3f3f3f3f;
const int IMIN = 0x80000000;
const double e = 2.718281828;
#define eps 1e-8
#define DEBUG 1
#define mod 1000000007
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pi;
///#pragma comment(linker, "/STACK:102400000,102400000")__int64 a[10050];
int n , m ;
struct node
{
    int l , r , sum , val , flag ;
    int len()
    {
        return r - l + 1;
    }
}a[MAXN << 2];
void build(int l , int r , int rt)
{
    a[rt].l = l , a[rt].r = r;
    a[rt].sum = a[rt].len();
    a[rt].val = 1;
    a[rt].flag = 0;
    if(l == r)return ;
    int mid = (l + r) >> 1;
    build(lson) ,build(rson);
}
void PushUp(int rt)
{
    a[rt].sum = a[rt << 1].sum + a[rt << 1 | 1].sum;
}
void PushDown(int rt)
{
    if(a[rt].flag)
    {
        a[rt << 1].val = a[rt].val;
        a[rt << 1 | 1].val = a[rt].val;
        a[rt << 1].sum = a[rt].val ? a[rt << 1].len() : 0;
        a[rt << 1 | 1].sum = a[rt].val ? a[rt << 1 | 1].len() : 0;
        a[rt << 1].flag = a[rt << 1 | 1].flag = 1;
        a[rt].flag = 0;
    }
}
void update(int delta , int L , int R , int rt)
{
    if(a[rt].l > R || a[rt].r < L)return ;
    if(a[rt].l >= L && a[rt].r <= R)
    {
        a[rt].val = delta;
        a[rt].sum = delta ? a[rt].len() : 0;
        a[rt].flag = 1;
        return ;
    }
    PushDown(rt);
    update(delta , L , R , rt << 1);
    update(delta , L , R , rt << 1 | 1);
    PushUp(rt);
}
int query(int L , int R , int rt)
{
    if(a[rt].l > R || a[rt].r < L)return 0;
    if(a[rt].l >= L && a[rt].r <= R)
    {
        return a[rt].sum;
    }
    PushDown(rt);
    return query(L , R , rt << 1) + query(L , R , rt << 1 | 1);
}
int BSearch(int l , int r , int key)
{
    while(l < r)
    {
        int mid = (l + r) >> 1;
        int sum = query(1 , mid , 1);
        if(sum < key)l = mid + 1;
        else r = mid;
    }
    return l;
}
int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        scanf("%d%d" , &n , &m);
        build(1 , n , 1);
        while(m--)
        {
            int op , l,  r;
            scanf("%d%d%d" , &op  , &l , &r);
            l++;
            if(op == 1)
            {
                int s = query(l , n , 1);
                if(s == 0)
                {
                    puts("Can not put any one.");
                    continue;
                }
                if(s < r)
                {
                    r = s;
                }
                int t = query(1 , l - 1 , 1);
                int FF = BSearch(1 , n , t + 1);
                int LL = BSearch(1 , n , t + r);
                printf("%d %d\n" , FF - 1 , LL - 1);
                update(0 , FF , LL , 1);
            }
            else
            {
                r++;
                int t = query(l, r , 1);
                printf("%d\n" , r - l + 1 - t);
                update(1 , l , r , 1);
            }
        }
        puts("");
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值