Cod sursa(job #2961697)

Utilizator flibiaVisanu Cristian flibia Data 6 ianuarie 2023 21:15:16
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 4.76 kb
#include <bits/stdc++.h>

using namespace std;
 
#ifdef LOCAL
#include "debug.h"
#else
#define dbg(...) 69
#define nl(...) 42
#endif

#define ll long long
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll random(ll st, ll dr) {
    assert(st <= dr);
    return st + rng() % (dr - st + 1);
}

const int MOD = 1e9 + 7;
const int N = 2e5 + 10;

struct Data {
    int val;
};

// combines data of two nodes
// careful
Data combine(Data &a, Data &b) {
    return {max(a.val, b.val)};
}

struct Lazy {
    int flag; // 1 lazy on, 0 lazy off
    int val;
};

struct Node {
    Data data;
    Lazy lazy;
    int L;
    int R;  
    Node *left;
    Node *right;
    Node(Data data, Lazy lazy, int L, int R): data(data), lazy(lazy), L(L), R(R), left(nullptr), right(nullptr) {}

    bool isLeaf() {
        return L == R;
    }

    int mid() {
        return L + (R - L) / 2;
    }

    // will merge lazy with data and clear lazy
    // careful !!!
    void applyLazy() {
        data.val = lazy.val;
    }

    // will applyLazy, then push lazy to left and right
    // careful !!!
    void pushLazy() {
        if (lazy.flag) {
            applyLazy();
            if (isLeaf()) {
                lazy.flag = 0;
                lazy.val = 0;
                return;
            }

            if (left == nullptr) {
                left = new Node({0}, {0, 0}, L, mid());
            }
            if (right == nullptr) {
                right = new Node({0}, {0, 0}, mid() + 1, R);
            }

            // careful !!!
            left->lazy = {1, lazy.val};
            right->lazy = {1, lazy.val};
        }

        lazy.flag = 0;
        lazy.val = 0;
    }

    // will update data by combining left and right
    void update() {
        Data lv = {0}; // careful
        Data rv = {0}; // careful
        if (left) {
            lv = left->data;
        }
        if (right) {
            rv = right->data;
        }
        
        data = combine(lv, rv);
    }
};

struct LazySegTreeDynamic {
    Node *root;

    LazySegTreeDynamic(int l, int r) {
        root = new Node({0}, {0, 0}, l, r);
    }

    void update(Node *node, int l, int r, int val) {
        if (node == nullptr) {
            return;
        }

        // cout << "[(" << node->L << "," << node->R << ") (";
        // cout << l << "," << r << "," << val << ")]" << endl;

        node->pushLazy();

        if (l > r || r < node->L || l > node->R) {
            return;
        }

        if (l <= node->L && node->R <= r) {
            // create new lazy
            node->lazy = {1, val};
            node->pushLazy();
            return;
        }

        // create left or right if needed
        if (l <= node->mid() && node->left == nullptr) {
            // careful
            node->left = new Node({0}, {0, 0}, node->L, node->mid());
        }
        if (r > node->mid() && node->right == nullptr) {
            // careful
            node->right = new Node({0}, {0, 0}, node->mid() + 1, node->R);
        }
        
        update(node->left, l, r, val);
        update(node->right, l, r, val);

        node->update();
    }

    void update(int l, int r, int val) {
        update(root, l, r, val);
    }

    Data query(Node *node, int l, int r) {
        if (node == nullptr) {
            // careful
            return {0};
        }

        node->pushLazy();

        if (l > r || r < node->L || l > node->R) {
            // careful
            return {0};
        }

        if (l <= node->L && node->R <= r) {
            return node->data;
        }

        Data lv = query(node->left, l, r);
        Data rv = query(node->right, l, r);

        return combine(lv, rv);
    }

    int query(int l, int r) {
        return query(root, l, r).val;
    }
};

void solve(int test, istream &cin, ostream &cout) {
    int n, m;
    cin >> n >> m;
    LazySegTreeDynamic sgt(1, n);
    for (int i = 1; i <= n; i++) {
        int x;
        cin >> x;
        sgt.update(i, i, x);
    }

    while (m--) {
        int c, l, r;
        cin >> c >> l >> r;
        if (c) {
            sgt.update(l, l, r);
        } else {
            cout << sgt.query(l, r) << '\n';
        }
    }
}

int main() {
    ifstream cin("arbint.in");
    ofstream cout("arbint.out");
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    auto start = std::chrono::high_resolution_clock::now();

    bool multiTest = false;

    int t;    
    if (multiTest) {
        cin >> t;
    } else {
        t = 1;
    }

    for (int test = 1; test <= t; test++) {
        solve(test, cin, cout);
    }

    auto stop = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);
    // cerr << fixed << setprecision(6) << "Running time: " << (double) duration.count() / 1e6 << " seconds" << '\n';

    return 0;
}