답안 #869598

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
869598 2023-11-05T01:59:38 Z NeroZein Hedgehog Daniyar and Algorithms (IZhO19_sortbooks) C++17
0 / 100
951 ms 50880 KB
#include "bits/stdc++.h"
using namespace std;

#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif

const int N = 1e6 + 6;

int ans[N]; 
int seg[N * 2];

void upd(int nd, int l, int r, int p, int v) {
  if (l == r) {
    seg[nd] = max(seg[nd], v);
    return;
  }
  int mid = (l + r) >> 1;
  int rs = nd + ((mid - l + 1) << 1);
  if (p <= mid) {
    upd(nd + 1, l, mid, p, v);
  } else {
    upd(rs, mid + 1, r, p, v);
  }
  seg[nd] = max(seg[nd + 1], seg[rs]); 
}

int qry(int nd, int l, int r, int s, int e) {
  if (l >= s && r <= e) {
    return seg[nd]; 
  }
  int mid = (l + r) >> 1;
  int rs = nd + ((mid - l + 1) << 1);
  if (mid >= e) {
    return qry(nd + 1, l, mid, s, e); 
  } else {
    if (mid < s) {
      return qry(rs, mid + 1, r, s, e);
    } else {
      return max(qry(nd + 1, l, mid, s, e), qry(rs, mid + 1, r, s, e)); 
    }
  }
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, q;
  cin >> n >> q;
  vector<int> a(n); 
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  stack<int> stk;
  vector<array<int, 4>> ranges; 
  for (int i = 0; i < q; ++i) {
    int l, r, v;
    cin >> l >> r >> v; 
    --l, --r; 
    ranges.push_back({l, r, -v, i});
  }
  for (int i = n - 1; i >= 0; --i) {
    while (!stk.empty() && a[stk.top()] < a[i]) {
      ranges.push_back({i, stk.top(), -(a[i] + a[stk.top()]), -1});//ranges with higher value come before
      stk.pop();
    }
    stk.push(i); 
  }
  sort(ranges.begin(), ranges.end()); 
  reverse(ranges.begin(), ranges.end()); 
  for (auto [l, r, v, i] : ranges) {
    v = -v;
    if (i == -1) {
      upd(0, 0, n - 1, r, v); 
    } else {
      int mx = qry(0, 0, n - 1, l, r);
      ans[i] = (mx <= v); 
    }
  }
  for (int i = 0; i < q; ++i) {
    cout << ans[i] << '\n'; 
  }
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Incorrect 1 ms 2396 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Incorrect 1 ms 2396 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 951 ms 50880 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 76 ms 7328 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Incorrect 1 ms 2396 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Incorrect 1 ms 2396 KB Output isn't correct
4 Halted 0 ms 0 KB -