Submission #1131975

#TimeUsernameProblemLanguageResultExecution timeMemory
1131975JelalTkmHedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++20
0 / 100
2687 ms299996 KiB
#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

using namespace std;

#define int long long int

const int N = 3e5 + 10;
const int md = 1e9 + 7;
const int INF = 1e9;

struct segtree {

  vector<pair<vector<int>, pair<int, int>>> tree;
  int size;
 
  void init(int n) {
    size = 1;
    while (size < n) size <<= 1;
    tree.assign(2 * size - 1, {{}, {0, 0}});
  }

  void build(vector<int> &a, int x, int lx, int rx) {
    if (rx - lx == 1) {
      if (lx < (int) a.size()) {
        tree[x].first.push_back(a[lx]);
        tree[x].second.first = a[lx];
      }
    } else {
      int m = (lx + rx) >> 1;
      build(a, 2 * x + 1, lx, m);
      build(a, 2 * x + 2, m, rx);
      tree[x].first = tree[2 * x + 2].first;
      int l = lower_bound(tree[x].first.begin(), tree[x].first.end(), tree[2 * x + 1].second.first) - tree[x].first.begin();
      l--;
      tree[x].second.second = max(tree[2 * x + 1].second.second, tree[2 * x + 2].second.second);
      if (l >= 0)
        tree[x].second.second = max(tree[x].second.second, tree[2 * x + 1].second.first + tree[x].first[l]);
      tree[x].first.insert(tree[x].first.begin(), tree[2 * x + 1].first.begin(), tree[2 * x + 1].first.end());
    }
  }
 
  void build(vector<int> &a) {
    init((int) a.size() + 1);
    build(a, 0, 0, size);
  }

  pair<int, int> get(int l, int r, int x, int lx, int rx) {
    if (l >= rx || lx >= r) {
      return {0, 0};
    }
    if (lx >= l && rx <= r) {
      return {tree[x].second.second, tree[x].second.first};
    } else {
      int m = (lx + rx) >> 1;
      auto s1 = get(l, r, 2 * x + 1, lx, m);
      auto s2 = get(l, r, 2 * x + 2, m, rx);
      int ans = max(s1.first, s2.first);
      int l = lower_bound(tree[2 * x + 2].first.begin(), tree[2 * x + 2].first.end(), s1.second) - tree[2 * x + 2].first.begin();
      l--;
      if (l >= 0)
        ans = max(ans, s1.second + tree[2 * x + 2].first[l]);
      return {ans, max(s1.second, s2.second)};
    }
  }

  int get(int l, int r) {
    auto pr = get(l, r, 0, 0, size);
    return pr.first;
  }

};

int32_t main(int32_t argc, char *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  
  int T = 1;
  // cin >> T;
  while (T--) {
    int n, m;
    cin >> n >> m;
    vector<int> a(n);
    for (int i = 0; i < n; i++)
      cin >> a[i];

    segtree st;
    st.build(a);

    while (m--) {
      int l, r, k;
      cin >> l >> r >> k;
      l--;
      if (st.get(l, r) > k)
        cout << 0 << '\n';
      else cout << 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...