Submission #431415

#TimeUsernameProblemLanguageResultExecution timeMemory
431415MilosMilutinovicHedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++14
100 / 100
2779 ms96580 KiB
#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;
const int M = 4 * N;

int a[N], st[M];
vector<tuple<int, int, int>> qq[N];

void modify(int node, int l, int r, int pos, int val) {
  if (l == r) {
    st[node] = max(st[node], val);
    return;
  }
  int mid = l + r >> 1;
  if (pos <= mid) modify(node * 2, l, mid, pos, val);
  else modify(node * 2 + 1, mid + 1, r, pos, val);
  st[node] = max(st[node * 2], st[node * 2 + 1]);
}

int get(int node, int l, int r, int ll, int rr) {
  if (l > r || l > rr || r < ll) return 0;
  if (ll <= l && r <= rr) return st[node];
  int mid = l + r >> 1;
  return max(get(node * 2, l, mid, ll, rr), get(node * 2 + 1, mid + 1, r, ll, rr));
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < q; i++) {
    int l, r, v;
    cin >> l >> r >> v;
    --l, --r;
    qq[r].push_back({l, v, i});
  }
  stack<int> s;
  vector<int> ans(q);
  for (int i = 0; i < n; i++) {
    while (!s.empty() && a[s.top()] <= a[i]) {
      s.pop();
    }
    if (!s.empty()) {
      modify(1, 0, n - 1, s.top(), a[s.top()] + a[i]);
    }
    for (auto& p : qq[i]) {
      if (get(1, 0, n - 1, get<0>(p), i) <= get<1>(p)) {
        ans[get<2>(p)] = 1;
      }
    }
    s.push(i);
  }
  for (int i = 0; i < q; i++) {
    cout << ans[i] << '\n';
  }
  return 0;
}

Compilation message (stderr)

sortbooks.cpp: In function 'void modify(int, int, int, int, int)':
sortbooks.cpp:16:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   16 |   int mid = l + r >> 1;
      |             ~~^~~
sortbooks.cpp: In function 'int get(int, int, int, int, int)':
sortbooks.cpp:25:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   25 |   int mid = l + r >> 1;
      |             ~~^~~
#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...