結果

問題 No.878 Range High-Element Query
ユーザー koi_kotyakoi_kotya
提出日時 2019-09-07 00:49:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 274 ms / 2,000 ms
コード長 2,675 bytes
コンパイル時間 3,262 ms
コンパイル使用メモリ 173,800 KB
実行使用メモリ 9,816 KB
最終ジャッジ日時 2023-09-07 04:59:02
合計ジャッジ時間 5,544 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 4 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 4 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 259 ms
8,724 KB
testcase_12 AC 168 ms
9,128 KB
testcase_13 AC 210 ms
7,516 KB
testcase_14 AC 160 ms
6,744 KB
testcase_15 AC 176 ms
8,888 KB
testcase_16 AC 254 ms
9,648 KB
testcase_17 AC 273 ms
9,788 KB
testcase_18 AC 274 ms
9,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

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

#define p_ary(ary,a,b,i) do { cout << "["; for (int (i) = (a);(i) < (b);++(i)) cout << ary[(i)] << ((b)-1 == (i) ? "" : ", "); cout << "]\n"; } while(0)
#define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0)

int const INF = INT32_MAX;
struct SegmentTree {
private:
    int n;
    vector<int> node;
public:
    SegmentTree(vector<int> v) {
        int sz = v.size();
        n = 1;
        while (n < sz) n *= 2;
        node.resize(2*n-1,-INF);
        for (int i = 0;i < sz;++i) node[i+n-1] = v[i];
        for (int i = n-2;i >= 0;--i) node[i] = max(node[2*i+1],node[2*i+2]);
    }

    void update(int x,int val) {
        x += (n-1);
        node[x] = val;
        while (x > 0) {
            x = (x-1)/2;
            node[x] = max(node[2*x+1],node[2*x+2]);
        }
    }

    int getmax(int a,int b,int k = 0,int l = 0,int r = -1) {
        if (r < 0) r = n;
        if (r <= a || b <= l) return -INF;
        if (a <= l && r <= b) return node[k];
        int vl = getmax(a,b,2*k+1,l,(l+r)/2);
        int vr = getmax(a,b,2*k+2,(l+r)/2,r);
        return max(vl,vr);
    }

    int find(int a,int b,int x,int k = 0,int l = 0,int r = -1) {
        if (r < 0) r = n;
        if (r <= a || b <= l || node[k] <= x) return -1;
        if (k >= n-1) return k-n+1;
        int vl = find(a,b,x,2*k+1,l,(l+r)/2);
        if (vl != -1) return vl;
        return find(a,b,x,2*k+2,(l+r)/2,r);
    }

    void display() {
        p_ary(node,0,2*n-1,i);
    }
};

int main() {
    int n,q;
    cin >> n >> q;
    vector<int> a(n);
    vector<vector<int>> b(n);
    for (int i = 0;i < n;++i) scanf("%d",&a[i]);
    SegmentTree seg(a);
    for (int i = 0;i < n;++i) {
        int v = seg.find(i+1,n,a[i]);
        if (v > -1) b[i].push_back(v);
    }
    for (int j = 0;j < 20;++j) {
        // for (int i = n-1;i >= 0;--i) {
        for (int i = 0;i < n;++i) {
            if (b[i].size() == j+1 && b[b[i][j]].size() == j+1) b[i].push_back(b[b[i][j]][j]);
        }
    }
    for (int z = 0;z < q;++z) {
        int c,l,r;
        cin >> c >> l >> r;
        l--;
        int ans = 1;
        while (!b[l].empty() && b[l][0] < r) {
            for (int i = b[l].size()-1;i >= 0;--i) if (b[l][i] < r) {
                ans += (1<<i);
                l = b[l][i];
                break;
            }
        }
        cout << ans << "\n";
    }
    /*
    for (int i = 0;i < n;++i) {
        cout << i;
        p_ary(b[i],0,b[i].size(),j);
    }*/
}
0