Cod sursa(job #1419210)

Utilizator andreiagAndrei Galusca andreiag Data 15 aprilie 2015 01:27:37
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 kb
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;
const int Nmax = 400555;

int d = 1;
int A[Nmax];

int max(int x, int y) { return x > y ? x : y; }

int query(int a, int b, int l, int r, int ind) {
  if (a <= l && r <= b)
    return A[ind];
  int mid = (l+r)/2;
  int x = a <= mid ? query(a, b, l, mid, 2*ind) : 0;
  int y = b > mid ? query(a, b, mid+1, r, 2*ind+1) : 0;
  return max(x, y);
}

void update(int pos, int val) {
  A[pos+d-1] = val;
  int x = pos+d-1;
  while (x > 1) {
    x /= 2;
    A[x] = max(A[2*x], A[2*x+1]);
  }
}

int main()
{
  ifstream f ("arbint.in");
  ofstream g ("arbint.out");
  
  int N, M, x, a, b;
  f >> N >> M;

  while (d < N) {
    d *= 2;
  }
  
  for (int i = 0; i < N; i++) {
    f >> A[d+i];
  }

  for (int i = d-1; i >= 1; i--) {
    A[i] = max(A[2*i], A[2*i+1]);
  }
  
  for (int m = 0; m < M; m++) {
    f >> x >> a >> b;
    if (x == 0)
      g << query(a, b, 1, d, 1) << '\n';
    else if (x == 1)
      update(a, b);
  }

  return 0;
}