线段树练习题

poj-3667–hotel
整体来说思路不难想,就是细节比较多,处理复杂,容易出错

题意:给出长为n的空间大小,支持两种操作
1.给定长度len,尽可能填补靠左边的空间,并输出填补的起始位置
2.给定起始位置pos,以及长度len,将以pos为起始长度为len的空间清空

思路:

变量记录:
维护三颗线段树,seg_left[root]表示结点root对应的区间[l,r]上,从左端点l起始的空闲空间长度,seg_right[root]表示结点root对应的区间[l,r]上,从右端点r起始的空闲空间长度,seg_tree[root]表示结点root对应的区间[l,r]上,对应的最大的连续空闲长度。
lazy[root]表示结点root对应的区间[l,r]上,对应的懒标记,这里需要注意的点是,有三种状态,值为2时表示root结点下的子树都为被占状态,值为1时表示root结点下的子树都为空闲状态,两种后来的可以覆盖前面的,值为0表示没有进行标记。
细节处理方面比较多:
1.push_up向上回溯的处理
2.query查询时的处理,先查左子树,再查左子树加上右子树,最后查右子树
3.lazy tag的部分,push_down向下push比较简单,就是将三颗树的值都置为lazy的值就可以。

//#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

#define int long long
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N = 2e5+10;
int n;
int arr[N],seg_tree[N<<2],lazy[N<<2],seg_left[N<<2],seg_right[N<<2];
void push_up(int root,int L,int R)
{
   
   
    seg_tree[root]=seg_right[root<<1]+seg_left[root<<1|1];
    seg_tree[root]=max(seg_tree[root],max(seg_tree[root<<1],seg_tree[root<<1|1]));
    int mid=L+R>>1;
    if(seg_left[root<<1]==mid-L+1)
    seg_left[root]=seg_left[root<<1]+seg_left[root<<1|1];
    else seg_left[root]=seg_left[root<<1];

    if(seg_right[root<<1|1]==R-mid)
    seg_right[root]=seg_right[root<<1]+seg_right[root<<1|1];
    else seg_right[root]=seg_right[root<<1|1];
}
void build(int root,int L,int R)
{
   
   
    if(L==R)
    {
   
   
        seg_tree[root]=1;
        seg_left[root]=1;
        seg_right[root]=1;
        return ;
    }
    int mid=(L+R)>>1;
    build(root<<1,L,mid);
    build(root<<1|1,mid+1,R);
    push_up(root,L,R);
}
void add(int root,int L,int R,int val)
{
   
   
    seg_tree[root]=(val-1)*(R-L+1);
    seg_left[root]=(val-1)*(R-L+1);
    seg_right[root]=(val-1)*(R-L+1);
}
void push_down(int root,int L,int R)
{
   
   
    if(lazy[root])
    {
   
   
        int mid=L+R>>1;
        lazy[root<<1]=lazy[root];
        lazy[root<<1|1]=lazy[
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值