Cod sursa(job #1693397)

Utilizator stoianmihailStoian Mihail stoianmihail Data 22 aprilie 2016 23:44:52
Problema A+B Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.87 kb
#include <bits/stdc++.h>

using std::vector;

struct tree {
private:
  int result;
  int N, treeSize;
  vector <int> val;

public:
  tree() {

  }

  tree(int N) {
    this -> N = N;
    if (N & (N - 1)) {
      this -> treeSize = N;
    } else {
      this -> treeSize = 1;
      while (this -> treeSize <= N) {
        this -> treeSize <<= 1;
      }
    }
    this -> treeSize <<= 1;
    this -> val.resize(this -> treeSize);
  }

private:
  int MAX(int X, int Y) {
    return X > Y ? X : Y;
  }

  void refresh(int u, int left, int right) {
    this -> val[u] = this -> MAX(this -> val[2 * u], this -> val[2 * u + 1]);
  }

  void find(int u, int left, int right, int pos, int x) {
    int mid = (left + right) >> 1;

    if (left == right) {
      this -> val[u] = x;
      return;
    }

    if (pos <= mid) {
      find(2 * u, left, mid, pos, x);
    } else {
      find(2 * u + 1, mid + 1, right, pos, x);
    }
    this -> refresh(u, left, right);
  }

  void search(int u, int left, int right, int a, int b) {
    int mid = (left + right) >> 1;

    if (a <= left && right <= b) {
      result = this -> MAX(result, this -> val[u]);
      return;
    }

    if (a <= mid) {
      search(2 * u, left, mid, a, b);
    }
    if (b > mid) {
      search(2 * u + 1, mid + 1, right, a, b);
    }
  }

public:
  int size() {
    return this -> N;
  }

  /** 1 <= pos <= N. **/
  void update(int pos, int x) {
    this -> find(1, 1, this -> N, pos, x);
  }

  /** 1 <= a <= b <= N. **/
  int query(int a, int b) {
    this -> result = 0;
    this -> search(1, 1, this -> N, a, b);
    return this -> result;
  }
};

int main(void) {
  FILE *f = fopen("mess.in", "r");

  tree T(2);

  T.update(3, 3);
  T.update(1, 4);
  fprintf(stderr, "%d\n", T.query(3, 5));
  fprintf(stderr, "%d\n", T.query(1, 5));
  fprintf(stderr, "%d\n", T.query(4, 5));

  /// Multumim Doamne!
  puts("Doamne ajuta!");
  return 0;
}