Cod sursa(job #2555618)

Utilizator memecoinMeme Coin memecoin Data 24 februarie 2020 10:28:02
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.17 kb
#include <fstream>
#include <iomanip>
#include <string>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <map>
#include <string.h>
#include <queue>
#include <stack>

#define INF 0x3f3f3f3f

using namespace std;

#ifdef DEBUG
string name = "data";
#else
string name = "arbint";
#endif

ifstream fin(name + ".in");
ofstream fout(name + ".out");

#define MAXN 100005 * 10

int aint[MAXN];
int n, q;

struct Interval {
    int a;
    int b;
    
    bool equals(Interval &other) {
        return a == other.a && b == other.b;
    }

    bool contains(Interval &other) {
        return other.a >= a && other.b <= b;
    }
    
    bool isDisjoint(Interval &other) {
        return b < other.a || a > other.b;
    }
    
    bool isUnitLength() {
        return a == b;
    }
    
    pair<Interval, Interval> splitAtMidPoint() {
        int mid = (a + b) / 2;
        return {{a, mid}, {mid + 1, b}};
    }
};

int update(Interval q, Interval window, int node, int x) {
    if (q.isDisjoint(window)) {
        return aint[node];
    }
    
    if (window.isUnitLength()) {
        aint[node] = x;
        return x;
    }
    
    auto split = window.splitAtMidPoint();
    
    auto left = update(q, split.first, node * 2, x);
    auto right = update(q, split.second, node * 2 + 1, x);
    
    aint[node] = max(left, right);
    
    return aint[node];
}

void update(int i, int x) {
    update({i, i}, {1,n}, 1, x);
}

int query(Interval q, Interval window, int node) {
    if (q.isDisjoint(window)) {
        return 0;
    }
    
    if (q.contains(window)) {
        return aint[node];
    }
    
    auto split = window.splitAtMidPoint();
    
    return max(query(q, split.first, node * 2), query(q, split.second, node * 2 + 1));
}

int query(int a, int b) {
    return query({a,b}, {1,n}, 1);
}

int main() {
    
    fin >> n >> q;
    
    for (int i = 1; i <= n; ++i) {
        int x;
        fin >> x;
        update(i, x);
    }
        
    for (int i = 0; i < q; ++i) {
        int t, x, y;
        fin >> t >> x >> y;
        if (t == 0) {
            fout << query(x, y) << "\n";
        } else {
            update(x, y);
        }
    }
    
    return 0;
}