Cod sursa(job #1244194)

Utilizator danny794Dan Danaila danny794 Data 16 octombrie 2014 21:24:53
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.44 kb
#include <iostream>
#include <cstdio>
#include <cmath>

#define NMAX ((1 << 18) | 1)

using namespace std;

int N, M, count;
int tree[NMAX];

void read()
{
  freopen("arbint.in", "r", stdin);
  freopen("arbint.out", "w", stdout);
  scanf("%d%d", &N, &M);
  count = 1 << ((int) (log(N) / log(2)));
  if(count < N)
    count <<= 1;
  for(int i = 0; i < N; i++)
    scanf("%d", &tree[count + i]);
}

void process()
{
  int k = count;
  while(k)
  {
    int c = k >> 1;
    for(int i = 0; i < k; i+=2)
      tree[c+(i>>1)] = tree[k+i] > tree[k+i+1] ? tree[k+i] : tree[k+i+1];
    k>>=1;
  }
}

int maxi(int a, int b, int c, int d, int pos)
{
  if(a == c && b == d)
    return tree[pos];
  int m = (c + d) >> 1;
  if(b <= m)
    return maxi(a, b, c, m, pos << 1);
  else if(a > m)
    return maxi(a, b, m + 1, d, (pos << 1) + 1);

  int x = maxi(a, m, c, m, pos << 1);
  int y = maxi(m + 1, b, m + 1, d, (pos << 1) + 1);
  return x > y ? x : y;
}

void update(int pos, int val)
{
  pos = count + pos - 1;
  tree[pos] = val;
  pos >>= 1;
  while(pos)
  {
    if(tree[(pos << 1) + 1] < tree[pos << 1])
      tree[pos] = tree[pos << 1];
    else
      tree[pos] = tree[(pos << 1) + 1];
    pos >>= 1;
  }
}

void solve()
{
  while(M)
  {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    switch(a)
    {
      case 0: printf("%d\n", maxi(b, c, 1, count, 1)); break;
      case 1: update(b, c); break;
    }
    M--;
  }
}

int main() {
  read();
  process();
  solve();
	return 0;
}