Cod sursa(job #2564290)

Utilizator NotTheBatmanBruce Wayne NotTheBatman Data 1 martie 2020 19:54:46
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.84 kb
#include <fstream>
#include <iostream>
#define lson(x) (2 * x)
#define rson(x) (2 * x + 1)
using namespace std;

const int N = 100005;

int a[N], n, segmtree[2 * N];

inline void Recompute (int father)
{
    segmtree[father] = max(segmtree[lson(father)], segmtree[rson(father)]);
}

void Build_Tree (int father, int left, int right)
{
    if (left == right)
        segmtree[father] = a[left];
    else
    {
        int mid = (left + right) / 2;
        Build_Tree(lson(father), left, mid);
        Build_Tree(rson(father), mid + 1, right);
        Recompute(father);
    }
}

void Update_Element (int father, int left, int right, int pos, int val)
{
    if (left == right)
        segmtree[father] = val;
    else
    {
        int mid = (left + right) / 2;
        if (pos <= mid)
            Update_Element(lson(father), left, mid, pos, val);
        else Update_Element(rson(father), mid + 1, right, pos, val);
        Recompute(father);
    }
}

int Query (int father, int left, int right, int x, int y) /// [x, y] = query segment
{
    if (x <= left && right <= y)
        return segmtree[father];
    int ans = -2e9, mid;
    mid = (left + right) / 2;
    if (x <= mid)
        ans = max(ans, Query(lson(father), left, mid, x, y));
    if (y > mid)
        ans = max(ans, Query(rson(father), mid + 1, right, x, y));
    return ans;
}

void Read_Solve ()
{
    ifstream fin ("arbint.in");
    int q;
    fin >> n >> q;
    for (int i = 1;  i <= n; i++)
        fin >> a[i];
    Build_Tree(1, 1, n);
    ofstream fout ("arbint.out");
    while (q--)
    {
        int op, x, y;
        fin >> op >> x >> y;
        if (op) Update_Element(1, 1, n, x, y);
        else fout << Query(1, 1, n, x, y) << "\n";
    }
    fin.close();
    fout.close();
}


int main()
{
    Read_Solve();
    return 0;
}