結果

問題 No.905 Sorted?
ユーザー face4face4
提出日時 2019-10-12 16:11:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 300 ms / 2,000 ms
コード長 1,702 bytes
コンパイル時間 984 ms
コンパイル使用メモリ 82,776 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-18 19:04:45
合計ジャッジ時間 6,540 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 4 ms
4,380 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 8 ms
4,376 KB
testcase_08 AC 276 ms
4,380 KB
testcase_09 AC 172 ms
4,376 KB
testcase_10 AC 257 ms
4,376 KB
testcase_11 AC 228 ms
4,376 KB
testcase_12 AC 265 ms
4,380 KB
testcase_13 AC 263 ms
4,380 KB
testcase_14 AC 299 ms
4,376 KB
testcase_15 AC 276 ms
4,376 KB
testcase_16 AC 291 ms
4,380 KB
testcase_17 AC 300 ms
4,376 KB
testcase_18 AC 222 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<functional>
#include<climits>
using namespace std;

template<typename T>
struct Seg{
private:
    vector<T> node;
    int n;
    function<T(T,T)> f;
    T def;

public:
    Seg(int siz, T d, function<T(T,T)> f) : def(d), f(f) {
        n = 1;
        while(n < siz)  n *= 2;
        node.resize(2*n-1, def);
    }
    Seg(vector<T> v, T d, function<T(T,T)> f) : def(d), f(f){
        n = 1;
        while(n < v.size())  n *= 2;
        node.resize(2*n-1);
        for(int i = 0; i < v.size(); i++)   node[n-1+i] = v[i];
        for(int i = n-2; i >= 0; i--)   node[i] = f(node[2*i+1], node[2*i+2]);
    }

    void update(int x, T val){
        x += n-1;
        node[x] = val; /* ! */
        while(x){
            x = (x-1)/2;
            node[x] = f(node[2*x+1], node[2*x+2]);
        }
    }

    T query(int a, int b, int k=0, int l=0, int r=-1){
        if(r < 0)   r = n;
        if(b <= l || r <= a)    return def;
        if(a <= l && r <= b)    return node[k];
        T lx = query(a, b, 2*k+1, l, (l+r)/2);
        T rx = query(a, b, 2*k+2, (l+r)/2, r);
        return f(lx, rx);
    }
};

int main(){
    int n;
    cin >> n;
    long long a[n];
    for(int i = 0; i < n; i++)  cin >> a[i];
    vector<bool> asc(n, 0), dec(n, 0);
    for(int i = 1; i < n; i++){
        asc[i] = a[i-1] <= a[i];
        dec[i] = a[i-1] >= a[i];
    }

    function<bool(bool,bool)> f = [](bool a, bool b){return a&b;};
    Seg<bool> ge(asc, true, f);
    Seg<bool> le(dec, true, f);

    int q;
    cin >> q;
    while(q-- > 0){
        int l, r;
        cin >> l >> r;
        cout << ge.query(l+1,r+1) << " " << le.query(l+1,r+1) << endl;
    }
    return 0;
}
0