Cod sursa(job #1999696)

Utilizator Stefan_RaduStefan Radu Stefan_Radu Data 11 iulie 2017 20:59:57
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.62 kb
#include <fstream>
#include <vector>

using namespace std;

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

class Segment_tree {
public:

  Segment_tree(int n): nod(n * 4 + 1, 0) {
    this -> n = n;
  }

  void update(int poz, int val, int st, int dr, int no_nod = 1) {
    if(st == dr) {
      nod[no_nod] = val;
      return;
    }
    else {
      if(poz <= (st + dr >> 1)) {
        update(poz, val, st, (st + dr) / 2, (no_nod << 1));
      }
      else {
        update(poz, val, (st + dr) / 2 + 1, dr, (no_nod << 1 | 1));
      }
      
      nod[no_nod] = max(nod[no_nod << 1], nod[no_nod << 1 | 1]);
    }
  }

  void query(int left_s, int right_s, int st, int dr, int &sol, int no_nod = 1) {
    if(left_s <= st and dr <= right_s) {
      sol = max(nod[no_nod], sol);
    }
    else {
      if(left_s <= (st + dr) / 2)
        query(left_s, right_s, st, (st + dr) / 2, sol, (no_nod << 1));
      if(right_s >= (st + dr) / 2 + 1)
        query(left_s, right_s, (st + dr) / 2 + 1, dr, sol, (no_nod << 1 | 1));
    }
  }

private:
  int n;
  vector< int > nod;
};

int main() {
  int n, m;
  cin >> n >> m;
  Segment_tree *tree = new Segment_tree(n);
  for(int i = 1; i <= n; i ++) {
    int val;
    cin >> val;
    tree -> update(i, val, 1, n, 1);
  }
  for(int i = 1; i <= m; i ++) {
    int type;
    cin >> type;
    if(type) {
      int poz;
      int val;
      cin >> poz >> val;
      tree -> update(poz, val, 1, n);
    }
    else {
      int left_s;
      int right_s;
      cin >> left_s >> right_s;
      int sol = 0;
      tree -> query(left_s, right_s, 1, n, sol);
      cout << sol << '\n';
    }
  }
  delete tree;
  return 0;
}