树套树

 

【2488. 树套树-简单版】

 

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
using namespace std;
const int N = 5e4 + 10, M = N * 4, INF = 1e9;
int n, m;
struct Tree{
    int l, r;
    multiset<int> s;
}tr[M];
int w[N];

void build(int u, int l, int r)
{
    tr[u] = {l, r};
    tr[u].s.insert(-INF), tr[u].s.insert(INF);
    for(int i = l; i <= r; i ++) tr[u].s.insert(w[i]);
    if(l == r) return;
    int mid = l + r >> 1;
    build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
}

void change(int u, int p, int x)
{
    tr[u].s.erase(tr[u].s.find(w[p]));
    tr[u].s.insert(x);
    if(tr[u].l == tr[u].r) return;
    int mid = tr[u].l + tr[u].r >> 1;
    if(p <= mid) change(u << 1, p, x);
    else change(u << 1 | 1, p, x);
}

int query(int u, int a, int b, int x)
{
    if(tr[u].l >= a && tr[u].r <= b)
    {
        auto it = tr[u].s.lower_bound(x);
        -- it;
        return *it;
    }
    int mid = tr[u].l + tr[u].r >> 1, res = -INF;
    if(a <= mid) res = max(res, query(u << 1, a, b, x));
    if(b > mid) res = max(res, query(u << 1 | 1, a, b, x));
    return res;
}

int main()
{
    cin.tie(0);
    cin >> n >> m;
    for(int i = 1; i <= n; i ++) cin >> w[i];
    build(1, 1, n);
    
    while(m --)
    {
        int op, a, b, x;
        cin >> op;
        if(op == 1)
        {
            cin >> a >> x;
            change(1, a, x);
            w[a] = x;
        }
        else
        {
            cin >> a >> b >> x;
            cout << query(1, a, b, x) << endl;
        }
    }
}

 

posted @ 2021-04-04 21:20  龙雪可可  阅读(63)  评论(0编辑  收藏  举报
****************************************** 页脚Html代码 ******************************************