Untitled

mail@pastecode.io avatar
unknown
plain_text
22 days ago
4.1 kB
2
Indexable
Never
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
template<class T> using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long
#define db double
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define speed ios_base::sync_with_stdio(false);cin.tie(0), cout.tie(0);
/*
                    " وَأَن لَّيْسَ لِلْإِنسَانِ إِلَّا مَا سَعَى ﴿39﴾ وَأَنَّ سَعْيَهُ سَوْفَ يُرَى ﴿40﴾ ثُمَّ يُجْزَاهُ الْجَزَاء الْأَوْفَى "
*/
const ll N = 1e6 + 5, inf = 1e18, mod = 1e9 + 7;
ll dx[] = {0, 0, -1, 1, -1, 1, 1, -1};
ll dy[] = {1, -1, 0, 0, 1, 1, -1, -1};
char di[] = {'R', 'L', 'U', 'D'};
ll n, q;

struct SegTree {
    vector<ll> tree;
    vector<ll> lazy;
    ll treesize;

    ll merge(ll left, ll right) {
        return max(left, right);
    }

    void build(ll oldn) {
        if (__builtin_popcount(oldn) == 1)
            treesize = oldn;
        else treesize = 1 << (__lg(oldn) + 1);
        tree.resize(treesize << 1);
        lazy.resize(treesize << 1, 0);
    }

    void propagate(ll node, ll sl, ll sr) {
        if (lazy[node] != 0) {
            tree[node] += lazy[node];
            if (sl != sr) {
                lazy[node << 1] += lazy[node];
                lazy[node << 1 | 1] += lazy[node];
            }
        }
        lazy[node] = 0;
    }

    ll query(ll x, ll ql, ll qr, ll node, ll sl, ll sr) {
        propagate(node, sl, sr);
        ll mid = (sl + sr) / 2;
        ll left = (node << 1);
        ll right = (node << 1 | 1);
        if (ql > sr || sl > qr or tree[node] < x)
            return -1;
        if (ql <= sl and sr <= qr) {
            if (sl == sr)
                return tree[node] >= x ? sl : -1;
            propagate(left, sl, mid);
            propagate(right, mid + 1, sr);
            if (tree[left] >= x)
                return query(x, ql, qr, left, sl, mid);
            if (tree[right] >= x)
                return query(x, ql, qr, right, mid + 1, sr);
            return -1;
        }
        ll l = query(x, ql, qr, left, sl, mid);
        if (l != -1)
            return l;
        ll r = query(x, ql, qr, right, mid + 1, sr);
        if (r != -1)
            return r;
        return -1;
    }

    ll query(ll x, ll ql, ll qr) {
        return query(x, ql, qr, 1, 0, treesize - 1);
    }

    void update(ll ql, ll qr, ll v, ll node, ll sl, ll sr) {
        propagate(node, sl, sr);
        if (ql <= sl && sr <= qr) {
            lazy[node] = v;
            propagate(node, sl, sr);
            return;
        }
        if (ql > sr || sl > qr)
            return;
        ll mid = (sl + sr) / 2;
        ll left = (node << 1);
        ll right = (node << 1 | 1);
        update(ql, qr, v, left, sl, mid);
        update(ql, qr, v, right, mid + 1, sr);
        tree[node] = merge(tree[left], tree[right]);
    }

    void update(ll ql, ll qr, ll v) {
        update(ql, qr, v, 1, 0, treesize - 1);
    }
};

void solve() {

    cin >> n >> q;
    SegTree st;
    st.build(n);
    while (q--) {
        ll type;
        cin >> type;
        if (type == 1) {
            ll l, r, v;
            cin >> l >> r >> v;
            st.update(l, r - 1, v);
        } else {
            ll x, idx;
            cin >> x >> idx;
            cout << st.query(x, idx, n - 1) << "\n";
        }
    }
}

void file() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}

int main() {
    speed
    file();
    int testcases = 1;
    //    cin >> testcases;
    while (testcases--) {
        solve();
    }
}
Leave a Comment