Cod sursa(job #2999705)

Utilizator justin.stoicaJustin Stoica justin.stoica Data 11 martie 2023 12:34:50
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.67 kb

// calificatClar.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>

using namespace std;

const int NMAX = 100010;

int aint[4 * NMAX + 5], lazy[4 * NMAX];

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

void update(int node, int l, int r, int poz, int val) {
    if (l == r) {
        aint[node] = val;
        return;
    }
    int mid = (l + r) >> 1;
    int stanga = node << 1;
    int dreapta = stanga | 1;
    if (poz <= mid) {
        update(stanga, l, mid, poz, val);
    }
    else{
       update(dreapta, mid + 1, r, poz, val); 
    }
    aint[node] = max(aint[stanga], aint[dreapta]);
    return;
}

int querry(int node, int l, int r, int lq, int rq) {
    if (lq <= l && rq >= r) {
        return aint[node];
    }
    int mid = (l + r) >> 1;
    int stanga = node << 1;
    int dreapta = stanga | 1;
    int x = 0, y = 0;
    if (mid >= lq) {
        x = querry(stanga, l, mid, lq, rq);
    }
    if (mid + 1 <= rq) {
        y = querry(dreapta, mid + 1, r, lq, rq);
    }
    return max(x, y);
}

void updateCuLazy(int node, int l, int r, int lq, int rq, int val) {
    int mid = (l + r) >> 1;
    int stanga = node << 1;
    int dreapta = stanga | 1;
    if (lazy[node] != 0) {
        aint[node] += (r - l + 1) * lazy[node];
        lazy[stanga] += lazy[node];
        lazy[dreapta] += lazy[node];
        lazy[node] = 0;
    }
    if (lq <= l && rq >= r) {
        aint[node] += (r - l + 1) * val;
        if (l != r) {
            lazy[stanga] += val;
            lazy[dreapta] += val;
        }
        return;
    }
    if (lq <= mid) {
        updateCuLazy(stanga, l, mid, lq, rq, val);
    }
    if(rq >= mid + 1) {
        updateCuLazy(dreapta, mid + 1, r, lq, rq, val);
    }
    aint[node] = aint[stanga] + aint[dreapta];
    return;
}

int querryCuLazy(int node, int l, int r, int lq, int rq) {
    int mid = (l + r) >> 1;
    int stanga = node << 1;
    int dreapta = stanga | 1;
    if (lazy[node] != 0) {
        aint[node] += (r - l + 1) * lazy[node];
        lazy[stanga] += lazy[node];
        lazy[dreapta] += lazy[node];
        lazy[node] = 0;
    }
    if (lq <= l && rq >= r) {
        return aint[node];
    }
    int x = 0, y = 0;
    if (lq <= mid) {
        x = querryCuLazy(stanga, l, mid, lq, rq);
    }
    if (rq >= mid + 1) {
        y = querryCuLazy(dreapta, mid + 1, r, lq, rq);
    }
    return x + y;
} 

int main()
{
    int n, m;
    cin >> n >> m;
    for (int i = 1, x; i <= n; i++) {
        cin >> x;
        updateCuLazy(1, 1, n, i, i, x);
    }
    while (m--) {
        int x, y, z, k;
        cin >> x >> y >> z;
        if (x == 0) {
            cout << querryCuLazy(1, 1, n, y, z) << endl;
        }
        else if (x == 1) {
            cin >> k;
            updateCuLazy(1, 1, n, y, z, k);
            for (int i = 1; i <= n; i++) {
                cout << querry(1, 1, n, i, i) << " ";
            }
            cout << endl;
        }
    }
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file