結果

問題 No.905 Sorted?
ユーザー tnakao0123tnakao0123
提出日時 2019-10-12 03:42:59
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 71 ms / 2,000 ms
コード長 2,441 bytes
コンパイル時間 620 ms
コンパイル使用メモリ 87,232 KB
実行使用メモリ 6,528 KB
最終ジャッジ日時 2024-05-04 19:57:19
合計ジャッジ時間 2,539 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,632 KB
testcase_01 AC 3 ms
5,632 KB
testcase_02 AC 2 ms
5,632 KB
testcase_03 AC 2 ms
5,632 KB
testcase_04 AC 3 ms
5,632 KB
testcase_05 AC 3 ms
5,504 KB
testcase_06 AC 3 ms
5,760 KB
testcase_07 AC 4 ms
5,760 KB
testcase_08 AC 63 ms
5,888 KB
testcase_09 AC 40 ms
5,888 KB
testcase_10 AC 61 ms
6,528 KB
testcase_11 AC 52 ms
6,272 KB
testcase_12 AC 57 ms
6,016 KB
testcase_13 AC 59 ms
6,528 KB
testcase_14 AC 71 ms
6,400 KB
testcase_15 AC 49 ms
6,400 KB
testcase_16 AC 64 ms
6,528 KB
testcase_17 AC 63 ms
6,400 KB
testcase_18 AC 49 ms
6,400 KB
testcase_19 AC 2 ms
5,504 KB
testcase_20 AC 3 ms
5,504 KB
testcase_21 AC 2 ms
5,504 KB
testcase_22 AC 2 ms
5,632 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:94:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   94 |   scanf("%d", &n);
      |   ~~~~~^~~~~~~~~~
main.cpp:95:36: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   95 |   for (int i = 0; i < n; i++) scanf("%lld", as + i);
      |                               ~~~~~^~~~~~~~~~~~~~~~
main.cpp:104:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  104 |   scanf("%d", &qn);
      |   ~~~~~^~~~~~~~~~~
main.cpp:108:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  108 |     scanf("%d%d", &l, &r);
      |     ~~~~~^~~~~~~~~~~~~~~~

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 905.cc:  No.905 Sorted? - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_E2 = 1 << 18; // = 262144
const int INF = 1 << 30;

/* typedef */

typedef long long ll;
typedef pair<int,int> pii;

template <typename T, const int MAX_E2>
struct SegTreeMinMax {
  int n, e2;
  T mins[MAX_E2], maxs[MAX_E2], mindef, maxdef;
  SegTreeMinMax() {}

  void init(int _n, T _mindef, T _maxdef) {
    n = _n; mindef = _mindef; maxdef = _maxdef;
    for (e2 = 1; e2 < n; e2 <<= 1);
    fill(mins, mins + MAX_E2, mindef);
    fill(maxs, maxs + MAX_E2, maxdef);
  }

  T &get_min(int i) { return mins[e2 - 1 + i]; }
  T &get_max(int i) { return maxs[e2 - 1 + i]; }
  void set(int i, T v) { get_min(i) = get_max(i) = v; }

  void set_all() {
    for (int j = e2 - 2; j >= 0; j--) {
      int j0 = j * 2 + 1, j1 = j0 + 1;
      mins[j] = min<T>(mins[j0], mins[j1]);
      maxs[j] = max<T>(maxs[j0], maxs[j1]);
    }
  }

  typedef pair<T,T> ptt;

  ptt get_range(int r0, int r1, int k, int i0, int i1) {
    if (r1 <= i0 || i1 <= r0) return ptt(mindef, maxdef);
    if (r0 <= i0 && i1 <= r1) return ptt(mins[k], maxs[k]);

    int im = (i0 + i1) / 2;
    ptt v0 = get_range(r0, r1, k * 2 + 1, i0, im);
    ptt v1 = get_range(r0, r1, k * 2 + 2, im, i1);
    return ptt(min<T>(v0.first, v1.first), max<T>(v0.second, v1.second));
  }
  ptt get_range(int r0, int r1) { return get_range(r0, r1, 0, 0, e2); }
};

/* global variables */

ll as[MAX_N];
SegTreeMinMax<int,MAX_E2> st;

/* subroutines */

inline int cmpi(ll a, ll b) {
  if (a < b) return -1;
  if (a > b) return 1;
  return 0;
}

/* main */

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%lld", as + i);

  st.init(n - 1, INF, -INF);

  for (int i = 0; i + 1 < n; i++)
    st.set(i, cmpi(as[i + 1], as[i]));
  st.set_all();

  int qn;
  scanf("%d", &qn);

  while (qn--) {
    int l, r;
    scanf("%d%d", &l, &r);

    if (l == r) puts("1 1");
    else {
      pii v = st.get_range(l, r);
      printf("%d %d\n", (v.first >= 0) ? 1 : 0, (v.second <= 0) ? 1 : 0);
    }
  }
  return 0;
}
0