Submission #749889

#TimeUsernameProblemLanguageResultExecution timeMemory
749889taherGrowing Trees (BOI11_grow)C++17
100 / 100
107 ms3276 KiB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "C:\GCC\debug.h"
#else
#define debug(...) void(42)
#endif

template<typename T>
class fenwick {
  public:
  int n;
  vector<T> fenw;

  fenwick(int _n) : n(_n) {
    fenw.resize(n);
  }

  void modify(int x, T v) {
    while (x < n) {
      fenw[x] += v;
      x |= (x + 1);
    }
  }

  T get(int x) {
    T v{};
    while (x >= 0) {
      v += fenw[x];
      x = (x & (x + 1)) - 1;
    }
    return v;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.end());

  fenwick<int> fenw(n);

  auto find_first = [&](int h) {
    // find smallest index such that Ai >= h
    int low = 0, high = n - 1;

    while (low <= high) {
      int mid = low + (high - low) / 2;

      if (a[mid] + fenw.get(mid) >= h) {
        high = mid - 1;
      } else {
        low = mid + 1;
      }
    }
    ++high;
    return high;
  };

  auto find_last = [&](int h) {
    // find biggest index such that Ai <= h
    int low = 0, high = n - 1;
    while (low <= high) {
      int mid = low + (high - low) / 2;

      if (a[mid] + fenw.get(mid) <= h) {
        low = mid + 1;
      } else {
        high = mid - 1;
      }
    }
    --low;
    return low;
  };

  auto Add = [&](int h, int c) {
    int i = find_first(h);
    int nxt = i + c;

    if (nxt >= n) {
      fenw.modify(i, +1);
      return ;
    }

    int x = a[nxt] + fenw.get(nxt);
    int y = a[nxt - 1] + fenw.get(nxt - 1);
    assert(y <= x);

    if (y < x) {
      fenw.modify(i, +1);
      fenw.modify(nxt, -1);
      return ;  
    }

    int segDeb = find_first(x);
    fenw.modify(i, +1);
    fenw.modify(segDeb, -1);
    int segEnd = find_last(x);
    int marge = nxt - segDeb;

    fenw.modify(segEnd - marge + 1, +1);
    if (segEnd + 1 < n) {
      fenw.modify(segEnd + 1, -1);
    }
    return ;
  };

  for (int i = 0; i < m; i++) {
    char type;
    cin >> type;

    if (type == 'F') {
      int c, h;
      cin >> c >> h;

      Add(h, c);
    } else {
      int mnH, mxH;
      cin >> mnH >> mxH;
      int x = find_first(mnH);
      int y = find_last(mxH);
      cout << y - x + 1 << '\n';
    }
  }
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...