Cod sursa(job #2423670)

Utilizator MatteoalexandruMatteo Verzotti Matteoalexandru Data 21 mai 2019 20:28:22
Problema Arbori de intervale Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.81 kb
// C++
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <deque>
#include <ctime>
#include <map>
#include <chrono>
#include <cmath>
#define INF 0x3f3f3f3f
#define MAX(a,b) a>b ? a:b
#define MIN(a,b) a<b ? a:b

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long long rand_seed() {
    long long a = rng();
    return a;
}

const int N = 1e5;
int v[N+5],aint[N+5];

void calc(int nod) {
    aint[nod] = MAX(aint[2*nod],aint[2*nod+1]);
}

void build(int nod,int st,int dr) {
    if (st == dr)
        aint[nod] = v[st];
    else {
        int mid = (st + dr) >> 1;
        build(2 * nod, st, mid);
        build(2 * nod + 1, mid + 1, dr);
        calc(nod);
    }
}

void update(int nod,int st,int dr,int x,int y) {
    if(st == dr)
        aint[nod] = y;
    else {
        int mid = (st + dr) >> 1;
        if(x <= mid)
            update(2 * nod, st, mid, x, y);
        else update(2 * nod + 1, mid+1, dr, x, y);
    }
    calc(nod);
}

int query(int nod,int st,int dr,int x,int y) {
    if (x <= st && dr <= y)
        return aint[nod];
    else {
        int ans = -INF;
        int mid = (st + dr) / 2;
        if (x <= mid)   ans = max(ans, query(2*nod, st, mid, x, y));
        if (y >= mid + 1)    ans = max(ans, query(2 * nod + 1, mid + 1, dr, x, y));
        return ans;
    }
}

int main() {
    freopen("arbint.in","r",stdin);
    freopen("arbint.out","w",stdout);
    int n,i,st,dr,k,x,y,t;
    scanf("%d%d",&n,&k);
    for(i=1; i<=n; i++) scanf("%d",&v[i]);
    build(1,1,n);
    for(i=1; i<=k; i++) {
        scanf("%d%d%d",&t,&x,&y);
        if(t == 0) printf("%d\n",query(1,1,n,x,y));
        else update(1,1,n,x,y);
    }
    return 0;
}