【线段树】个人练习-Leetcode-3161. Block Placement Queries

题目链接:https://leetcode.cn/problems/block-placement-queries/description/

题目大意:在x轴上,有两种操作:

  • 在某个点上插入一个障碍物
  • 查询,在[0, x]区间内,能否放下长度为sz的物品

插入和查询操作交替进行,返回每次查询的结果。

思路:一开始时看见题目写的"anywhere"以为是【区间内每处都能放下物品】,寻思着那不是整个区间都没有障碍物才行吗,好像判断有点简单了。后来才发现只要有一处能放下就行了。

那其实就是查询[0, x]区间内,被障碍物隔开的各个区间中,能存储的最大长度。这种涉及区间查询的,应该用线段树来解决。然而长久不写已经忘了线段树怎么写了...看了很多题解,还是灵神的比较清晰。

更新i处的值为val

cpp 复制代码
	void update(int o, int l, int r, int i, int val) {
        if (l == r) {
            tree[o] = val;
            return;
        }
        int mid = (l + r) / 2;
        if (i <= mid)
            update(o * 2, l, mid, i, val);
        else
            update(o * 2 + 1, mid+1, r, i, val);
        tree[o] = max(tree[o * 2], tree[o*2 + 1]);
    }

返回[0, x]中最大的区间长度

cpp 复制代码
	int query(int o, int l, int r, int x) {
        if (r <= x)
            return tree[o];
        int mid = (l + r) / 2;
        if (x <= mid) 
            return query(o * 2, l, mid, x);
        return max(tree[o * 2], query(o * 2 + 1, mid + 1, r, x)); 
    }

对于[0, x]来说,要么最大长度在[0, pre]prex左边的最大的障碍物位置)之间,要么在[pre, x]之间(因为每个query中x其实也相当于一个暂时的障碍)。

完整代码

cpp 复制代码
class Solution {
public:
    vector<int> tree;

    void update(int o, int l, int r, int i, int val) {
        if (l == r) {
            tree[o] = val;
            return;
        }
        int mid = (l + r) / 2;
        if (i <= mid)
            update(o * 2, l, mid, i, val);
        else
            update(o * 2 + 1, mid+1, r, i, val);
        tree[o] = max(tree[o * 2], tree[o*2 + 1]);
    }


    int query(int o, int l, int r, int x) {
        if (r <= x)
            return tree[o];
        int mid = (l + r) / 2;
        if (x <= mid) 
            return query(o * 2, l, mid, x);
        return max(tree[o * 2], query(o * 2 + 1, mid + 1, r, x)); 
    }


    vector<bool> getResults(vector<vector<int>>& queries) {
        int maxn = 0;
        for (auto q : queries)
            maxn = max(maxn, q[1]);
        maxn++;
        
        set<int> st{0, maxn};
        tree.resize(2 << (32 - __builtin_clz(maxn)));
        vector<bool> res;
        
        for (auto q : queries) {
            int x = q[1];
            auto it = st.lower_bound(x);
            int pre = *std::prev(it);
            if (q[0] == 1) {
                int nxt = *it;
                st.insert(x);
                update(1, 0, maxn, x, x - pre);
                update(1, 0, maxn, nxt, nxt - x);
            }
            else {
                int maxlen = max(query(1, 0, maxn, pre), x - pre);
                res.push_back(maxlen >= q[2]);
            }
        }
        return res;
    }
};
相关推荐
春日见7 分钟前
如何避免代码冲突,拉取分支
linux·人工智能·算法·机器学习·自动驾驶
副露のmagic9 分钟前
更弱智的算法学习 day59
算法
u0109272711 小时前
C++中的RAII技术深入
开发语言·c++·算法
2401_832131952 小时前
模板错误消息优化
开发语言·c++·算法
金枪不摆鳍2 小时前
算法--二叉搜索树
数据结构·c++·算法
近津薪荼2 小时前
优选算法——双指针6(单调性)
c++·学习·算法
helloworldandy3 小时前
高性能图像处理库
开发语言·c++·算法
2401_836563183 小时前
C++中的枚举类高级用法
开发语言·c++·算法
bantinghy3 小时前
Nginx基础加权轮询负载均衡算法
服务器·算法·nginx·负载均衡
chao1898443 小时前
矢量拟合算法在网络参数有理式拟合中的应用
开发语言·算法