Cod sursa(job #2707180)

Utilizator Rares09Rares I Rares09 Data 16 februarie 2021 17:15:12
Problema Arbori de intervale Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.82 kb
#include <fstream>

using namespace std;

ifstream cin ("arbint.in");
ofstream cout ("arbint.out");

int n, m, tree[400005], lazy[400005];

int setValue(int pos, int tl, int tr, int l, int r, int val)
{
    tree[pos] += lazy[pos];

    if(tl != tr)
    {
        lazy[2 * pos] += lazy[pos];
        lazy[2 * pos + 1] += lazy[pos];
    }

    lazy[pos] = 0;

    if(r < tl || l > tr)
        return tree[pos];
    else if(l <= tl && tr <= r)
    {
        int diff = val - tree[pos];
        tree[pos] = val;

        if(tl != tr)
        {
            lazy[2 * pos] += diff;
            lazy[2 * pos + 1] += diff;
        }

        return tree[pos];
    }

    int mid = (tl + tr) / 2;
    tree[pos] = setValue(pos * 2, tl, mid, l, r, val);
    tree[pos] = max(tree[pos], setValue(pos * 2 + 1, mid + 1, tr, l, r, val));
    return tree[pos];
}

int query(int pos, int tl, int tr, int l, int r)
{
    tree[pos] += lazy[pos];

    if(tl != tr)
    {
        lazy[2 * pos] += lazy[pos];
        lazy[2 * pos + 1] += lazy[pos];
    }

    lazy[pos] = 0;

    if(r < tl || l > tr)
        return -1;
    else if(l <= tl && tr <= r)
    {
        return tree[pos];
    }

    int mid = (tl + tr) / 2, mx = -1;
    mx = query(pos * 2, tl, mid, l, r);
    mx = max(mx, query(pos * 2 + 1, mid + 1, tr, l, r));
    return mx;
}

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

    //for(int i = 0; i <= 4 * n; ++i)
    //    tree[i] = 1e9;

    for(int i = 1; i <= n; ++i)
    {
        int x;
        cin >> x;
        setValue(1, 1, n, i, i, x);
    }

    for(int i = 1; i <= m; ++i)
    {
        int a, b, c;
        cin >> a >> b >> c;

        if(a == 0)
            cout << query(1, 1, n, b, c) << '\n';
        else
            setValue(1, 1, n, b, b, c);
    }

    return 0;
}