Cod sursa(job #2187275)

Utilizator mateicosCostescu Matei mateicos Data 26 martie 2018 12:48:07
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.41 kb
#include <cstdio>
#include <algorithm>

int ai[400005], v[100005], sol;

using namespace std;

void build(int nod, int st, int dr){
  if(st == dr){
    ai[nod] = v[st]; 
  }
  else{
    int med = (st + dr) / 2;
    build(2 * nod, st, med);
    build(2 * nod + 1, med + 1, dr);
    ai[nod] = max(ai[2 * nod], ai[2 * nod + 1]);
  }
}

void update(int nod, int st, int dr, int pozV, int x){
  if(st == dr){
    ai[nod] = x;
  }
  else{
    int med = (st + dr) / 2;
		if(pozV <= med){
      update(2 * nod, st, med, pozV, x);
    }
    if(pozV >= med + 1){
      update(2 * nod + 1, med + 1, dr, pozV, x);
    }
    ai[nod] = max(ai[2 * nod], ai[2 * nod + 1]);
  }
}
void querry(int nod, int  st, int dr, int a, int b){
  if(a <= st && dr <= b){
    sol = max(sol, ai[nod]);
  }
  else{
    int med = (st + dr) / 2;
    if(a <= med)
      querry(2 * nod, st, med, a, b);
		if(b >= med + 1)
		  querry(2 * nod + 1, med + 1, dr, a, b);
  }
}

int main()
{
    freopen("arbint.in", "r", stdin);
    freopen("arbint.out", "w", stdout);
    int n ,i ,m, p, a, b;
    scanf("%d%d", &n, &m);
    for(i = 1;i <= n;i++)
	    scanf("%d", &v[i]);
		build(1, 1, n);
		for(i = 1;i <= m;i++){
	    scanf("%d%d%d", &p, &a, &b);
	    if(p == 0){
	    	sol = 0;
	      querry(1, 1, n, a, b);
        printf("%d\n", sol);
	    }
	    else{
	      update(1, 1, n, a, b);
	    }
	  }
    return 0;
}