POJ 1823 Hotel 线段树

题目链接

和上一题差不多....第三种操作只需要输出maxx[1]的值就可以。

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
using namespace std;
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
const int maxn = 16005;
int maxx[maxn<<2], pre_max[maxn<<2], suf_max[maxn<<2], cover[maxn<<2];
void pushUp(int rt, int m) {
    maxx[rt] = max(maxx[rt<<1], maxx[rt<<1|1]);
    pre_max[rt] = pre_max[rt<<1];
    suf_max[rt] = suf_max[rt<<1|1];
    if(maxx[rt<<1] == (m-(m>>1)))
        pre_max[rt] = pre_max[rt<<1] + pre_max[rt<<1|1];
    if(maxx[rt<<1|1] == (m>>1))
        suf_max[rt] = suf_max[rt<<1|1]+suf_max[rt<<1];
    maxx[rt] = max(maxx[rt], suf_max[rt<<1]+pre_max[rt<<1|1]);
}
void build(int l, int r, int rt) {
    maxx[rt] = pre_max[rt] = suf_max[rt] = r-l+1;
    cover[rt] = -1;
    if(l == r)
        return ;
    int m = l+r>>1;
    build(lson);
    build(rson);
}
void pushDown(int rt, int m) {
    if(~cover[rt]) {
        cover[rt<<1] = cover[rt<<1|1] = cover[rt];
        maxx[rt<<1] = pre_max[rt<<1] = suf_max[rt<<1] = cover[rt]*(m-(m>>1));
        pre_max[rt<<1|1] = suf_max[rt<<1|1] = maxx[rt<<1|1] = cover[rt]*(m>>1);
        cover[rt] = -1;
    }
}
void update(int L, int R, int l, int r, int rt, int val) {
    if(L<=l&&R>=r) {
        cover[rt] = val;
        maxx[rt] = suf_max[rt] = pre_max[rt] = val*(r-l+1);
        return ;
    }
    pushDown(rt, r-l+1);
    int m = l+r>>1;
    if(L<=m)
        update(L, R, lson, val);
    if(R>m)
        update(L, R, rson, val);
    pushUp(rt, r-l+1);
}
int main()
{
    int n, m, sign, x, y;
    while(cin>>n>>m) {
        build(1, n, 1);
        while(m--) {
            scanf("%d", &sign);
            if(sign == 1) {
                scanf("%d%d", &x, &y);
                update(x, y+x-1, 1, n, 1, 0);
            } else if(sign == 2){
                scanf("%d%d", &x, &y);
                update(x, y+x-1, 1, n, 1, 1);
            } else {
                printf("%d\n", maxx[1]);
            }
        }
    }
}
时间: 2024-12-22 08:10:32

POJ 1823 Hotel 线段树的相关文章

poj 3667 Hotel - 线段树

The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a vacation on the sunny shores of Lake Superior. Bessie, ever the competent travel agent, has named the Bullmoose Hotel on famed Cumberland Street as their v

POJ 3667 Hotel(线段树区间合并)

Description The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a vacation on the sunny shores of Lake Superior. Bessie, ever the competent travel agent, has named the Bullmoose Hotel on famed Cumberland Stree

POJ 3667 Hotel 线段树 区间合并

题意: 1 输入a:询问是不是有连续长度为a的空房间,有的话住进最左边 2 输入a b:将[a,a+b-1]的房间清空 思路:记录区间中最长的空房间 线段树操作: update:区间替换 query:询问满足条件的最左端点 #include <cstdio> #include <iostream> #include <algorithm> #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1

POJ 3667 Hotel ( 线段树区间合并 )

题目链接~~> 做题感悟:这题是接触线段树区间合并的第一题,做的很纠结. 解题思路: 注意线段树上节点代表的信息 : 每个节点需要维护 lc , rc , mc ,add ,见下图: add 为懒惰标记.假设 i 代表父亲节点编号,左儿子为  i * 2  ,右儿子为 i * 2  + 1 ,那么我们可以得到 : T [ i ] .lc 首先加上左儿子的左边的空格数,然后需要判断一下,如果左儿子的左节点占满了整个左区间时需要再加上右儿子的左边的空格数.同理 T [ i ] .rc 也可以这样得到

POJ 2481 Cows (线段树)

Cows 题目:http://poj.org/problem?id=2481 题意:有N头牛,每只牛有一个值[S,E],如果对于牛i和牛j来说,它们的值满足下面的条件则证明牛i比牛j强壮:Si <=Sjand Ej <= Ei and Ei - Si > Ej - Sj.现在已知每一头牛的测验值,要求输出每头牛有几头牛比其强壮. 思路:将牛按照S从小到大排序,S相同按照E从大到小排序,这就保证了排在后面的牛一定不比前面的牛强壮.再按照E值(离散化后)建立一颗线段树(这里最值只有1e5,所

POJ 2299 离散化线段树

点击打开链接 Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 40827   Accepted: 14752 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by

POJ训练计划2299_Ultra-QuickSort(线段树/单点更新)

解题报告 题意: 求逆序数. 思路: 线段树离散化处理. #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #define LL long long using namespace std; LL sum[2001000],num[501000],_hash[501000]; void push_up(int rt) { sum[rt]=sum[rt*2

POJ 3667(线段树区间合并)

http://poj.org/problem?id=3667 题意:两个操作 : 1 选出靠左的长度为a的区间. 2 把从 a到a+b的区间清空. 线段树区间合并+lazy // by caonima // hehe #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <cmath> using namespace std; co

POJ&#183;1151 Atlantis&#183;线段树求矩形面积并

题目在这:http://poj.org/problem?id=1151 Atlantis Time Limit: 1000MS   Memory Limit: 10000K Description There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the is