Cod sursa(job #3292958)

Utilizator EnnBruhEne Andrei EnnBruh Data 9 aprilie 2025 20:35:16
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.14 kb
#include <bits/stdc++.h>

#pragma GCC optimize ("O3")
#pragma GCC optimize ("inline,unroll-loops,fast-math")
#pragma GCC target ("avx2,bmi,bmi2,lzcnt,popcnt")

using namespace std;

class inParser {
private:
        FILE *fin; char *buff; int id;
        char readCh( ) {
                ++id;
                if (id == 4096) { id = 0; fread(buff, sizeof(char), 4096, fin); }
                return buff[id];
        }
public:
        inParser (const char *name) {
                fin = fopen(name, "r");
                buff = new char[4096]( );
                id = 4095;
        }

        inParser& operator >> (int& num) {
                char ch;
                while (!isdigit(ch = readCh( )));
                num = ch - '0';
                while (isdigit(ch = readCh( )))
                        num = num * 10 + ch - '0';
                return *this;
        }
};

const string fileName = "arbint";
inParser in ((fileName + ".in").c_str( ));
ofstream out (fileName + ".out");

const int maxsze = 1e5 + 2;
const int inf = 0x3f3f3f3f;
const int modulo = 1e9 + 7;

int n, m;
int tree[4 * maxsze];

int maxTree(int l, int r) {
        l += n; r += n;

        int ans = -inf;
        while (l <= r) {
                if ((l & 1) == 1) ans = max(ans, tree[l++]);
                if ((r & 1) == 0) ans = max(ans, tree[r--]);
                l >>= 1; r >>= 1;
        }
        return ans;
}

void updateTree(int pos, int elem) {
        pos += n; tree[pos] = elem;
        pos >>= 1;
        while (pos >= 1) {
                tree[pos] = max(tree[(pos << 1)], tree[(pos << 1) + 1]);
                pos >>= 1;
        }
}


int main( ) {
        in >> n >> m;

        int elem;
        for (int i = 1; i <= n; ++i) {
                in >> elem;
                updateTree(i, elem);
        }

        int op, a, b;
        for (int i = 1; i <= m; ++i) {
                in >> op >> a >> b;
                switch ( op ) {
                        case 0: cout << maxTree(a, b) << '\n'; break;
                        case 1: updateTree(a, b); break;
                }
        }


        return 0;
}