Submission #345400

#TimeUsernameProblemLanguageResultExecution timeMemory
345400PetyHedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++14
Compilation error
0 ms0 KiB
#include <iostream>
#include<vector>
#pragma GCC optimize("O1")


using namespace std;

const int N = 1e6;
int n, m, w[N + 2], cnt;
struct nodA {
  int inv;
  vector<int>v;
} aint[4 * N + 2];
int ans = 0, mx = 0;


void build (int nod, int st, int dr) {
  if (st == dr) {
    aint[nod].inv = 0;
    aint[nod].v.push_back(w[st]);
    return;
  }
  int mid = (st + dr) / 2;
  build(2 * nod, st, mid);
  build(2 * nod + 1, mid + 1, dr);
  aint[nod].inv = max(aint[2 * nod].inv, aint[2 * nod + 1].inv);
  auto it = lower_bound(aint[2 * nod + 1].v.begin(), aint[2 * nod + 1].v.end(), aint[2 * nod].v.back());
  if (it != aint[2 * nod + 1].v.begin()) {
    it--;
    aint[nod].inv = max(aint[nod].inv, aint[2 * nod].v.back() + *it);
  }
  for (int i = st; i <= dr; i++)
    aint[nod].v.push_back(w[i]);
  cnt += aint[nod].v.size();
  sort(aint[nod].v.begin(), aint[nod].v.end());
  /*int i = 0, j = 0;
  while (i < aint[2 * nod].v.size() && j < aint[2 * nod + 1].v.size()) {
    if (aint[2 * nod].v[i] == aint[2 * nod + 1].v[j]) {
      int x = aint[2 * nod].v[i];
      aint[nod].v.push_back(x);
      aint[nod].v.push_back(x);
      i++;
      j++;
    }
    if (aint[2 * nod].v[i] < aint[2 * nod + 1].v[j]) {
      int x = aint[2 * nod].v[i];
      aint[nod].v.push_back(x);
      i++;
    }
    if (aint[2 * nod].v[i] > aint[2 * nod + 1].v[j]) {
      int x = aint[2 * nod + 1].v[j];
      aint[nod].v.push_back(x);
      j++;
    }
  }
  while (i < aint[2 * nod].v.size())
    aint[nod].v.push_back(aint[2 * nod].v[i++]);
  while (j < aint[2 * nod + 1].v.size())
    aint[nod].v.push_back(aint[2 * nod + 1].v[j++]);*/
}

void query (int nod, int st, int dr, int a, int b) {
  if (a <= st && dr <= b) {
    if (mx == -1) {
      ans = aint[nod].inv;
      mx = max(mx, aint[nod].v.back());
    }
    else {
      ans = max(ans, aint[nod].inv);
      auto it = lower_bound(aint[nod].v.begin(), aint[nod].v.end(), mx);
      if (it != aint[nod].v.begin()) {
        it--;
        ans = max(ans, mx + *it);
      }
      mx = max(mx, aint[nod].v.back());
    }
    return;
  }
  int mid = (st + dr) / 2;
  if (a <= mid)
    query(2 * nod, st, mid, a, b);
  if (b > mid)
    query(2 * nod + 1, mid + 1, dr, a, b);
}

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(0); cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    cin >> w[i];
  build(1, 1, n);
  for (int i = 1; i <= m; i++) {
    int l, r, k;
    cin >> l >> r >> k;
    ans = mx = -1;
    query(1, 1, n, l, r);
    cout << (ans <= k) << "\n";
  }
  return 0;
}

Compilation message (stderr)

sortbooks.cpp: In function 'void build(int, int, int)':
sortbooks.cpp:35:3: error: 'sort' was not declared in this scope; did you mean 'qsort'?
   35 |   sort(aint[nod].v.begin(), aint[nod].v.end());
      |   ^~~~
      |   qsort