线段树-------L - Vases and Flowers(简单线段树 + 二分)

本文介绍了一个基于二分查找的花瓶管理算法,该算法用于处理Alice收到花朵后的存放及清理操作。通过维护一棵线段树,算法实现了花朵的高效放置与统计。当Alice收到花朵时,算法会找到首个可用花瓶的位置;当进行清理时,则更新线段树以反映花瓶状态。文章详细展示了算法流程,并提供了完整的C++实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

就是简单地在k == 1的时候二分的找始终点。。。。由于二分写的有点问题,所以贡献N次wa

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
const int N = 50005;

typedef struct Node{
    int val;
    int addval;
}Node;
Node tree[N << 2];

void pushdown(int root,int m)
{
    if(tree[root].addval != -1){
        tree[root * 2 + 1].addval = tree[root * 2 + 2].addval = tree[root].addval;
        tree[root * 2 + 1].val = (m - (m >> 1)) * tree[root].addval;
        tree[root * 2 + 2].val = (m >> 1) * tree[root].addval;
        tree[root].addval = -1;
    }
}

void update(int root,int l,int r,int ql,int qr,int val)
{
    if(r < ql || l > qr){
        return ;
    }
    if(l >= ql && r <= qr){
        tree[root].val = (r - l + 1) * val;
        tree[root].addval = val;
        return ;
    }
    pushdown(root,r - l + 1);
    int mid = (l + r) / 2;
    if(ql <= mid){
        update(root * 2 + 1,l,mid,ql,qr,val);
    }
    if(qr > mid){
        update(root * 2 + 2,mid + 1,r,ql,qr,val);
    }
    tree[root].val = tree[root * 2 + 1].val + tree[root * 2 + 2].val;
}

int query(int root,int l,int r,int ql,int qr)
{
    if(r < ql || l > qr){
        return 0;
    }
    if(l >= ql && r <= qr){
        return tree[root].val;
    }
    pushdown(root,r - l + 1);
    int mid = (l + r) / 2;
    int sum = 0;
    if(ql <= mid){
        sum += query(root * 2 + 1,l,mid,ql,qr);
    }
    if(qr > mid){
        sum += query(root * 2 + 2,mid + 1,r,ql,qr);
    }
    return sum;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        for(int i = 1;i < (N << 2);++i){
            tree[i].val = 0;
            tree[i].addval = -1;
        }
        int n,m;
        scanf("%d %d",&n,&m);
        for(int i = 0;i < m;++i){
            int k,a,b;
            scanf("%d %d %d",&k,&a,&b);
            if(k == 2){
                int sum = query(1,1,n,a + 1,b + 1);
                update(1,1,n,a + 1,b + 1,0);
                printf("%d\n",sum);
            }else{
                int sum = query(1,1,n,a + 1,n);
                if(sum == (n - a)){
                    printf("Can not put any one.\n");
                }else{
                    int cnt = n - a - sum;
                    if(cnt >= b){
                        cnt = b;
                    }
                    //cout << cnt << endl;
                    int p = -1,q = -1;
                    int l = a + 1,r = n;
                    while(l <= r){
                        int mid = (l + r) / 2;
                        int ptr = query(1,1,n,a + 1,mid);
                        if(ptr == (mid - a - 1)){
                            p = mid;
                            r = mid - 1;
                        }else if(ptr > (mid - a - 1)){
                            l = mid + 1;
                        }else{
                            r = mid - 1;
                        }
                    }
                    printf("%d ",p - 1);
                    l = a + 1;r = n;
                    while(l <= r){
                        int mid = (l + r) / 2;
                        int ptr = query(1,1,n,a + 1,mid);
                        if(ptr + cnt == mid - a){
                            q = mid;
                            r = mid - 1;
                        }else if(ptr + cnt > mid - a){
                            l = mid + 1;
                        }else{
                            r = mid - 1;
                        }
                    }
                    printf("%d\n",q - 1);
                    update(1,1,n,p,q,1);
                }
            }
//            for(int j = 1;j <= n;++j){
//                printf("%d ",query(1,1,n,1,j));
//            }
//            printf("\n");
        }
        printf("\n");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值