結果

問題 No.878 Range High-Element Query
ユーザー koi_kotyakoi_kotya
提出日時 2019-09-08 08:41:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 281 ms / 2,000 ms
コード長 4,445 bytes
コンパイル時間 1,765 ms
コンパイル使用メモリ 174,792 KB
実行使用メモリ 18,100 KB
最終ジャッジ日時 2023-09-09 22:22:15
合計ジャッジ時間 4,825 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 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 2 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 4 ms
4,380 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 270 ms
16,876 KB
testcase_12 AC 182 ms
17,204 KB
testcase_13 AC 212 ms
11,320 KB
testcase_14 AC 160 ms
10,768 KB
testcase_15 AC 185 ms
17,092 KB
testcase_16 AC 260 ms
18,080 KB
testcase_17 AC 281 ms
18,100 KB
testcase_18 AC 279 ms
18,056 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)

struct SparseTable {
private:
    vector<vector<int>> st;
    vector<int> lookup;
public:
    SparseTable(vector<int>& v) {
        int b = 0,n = v.size();
        while ((1<<b) <= n) ++b;
        st.assign(b,vector<int>(1<<b));
        for (int i = 0;i < n;++i) {
            st[0][i] = v[i];
        }
        for (int i = 1;i < b;++i) {
            for (int j = 0;j+(1<<i) <= (1<<b);++j) {
                st[i][j] = max(st[i-1][j],st[i-1][j+(1<<(i-1))]);
            }
        }
        lookup.resize(n+1);
        for (int i = 2;i < n+1;++i) {
            lookup[i] = lookup[i>>1]+1;
        }
    }
    int getmax(int l,int r) {
        int b = lookup[r-l];
        return max(st[b][l],st[b][r-(1<<b)]);
    }
};

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]);
    SparseTable st(a);
    for (int i = 0;i < n;++i) {
        int ng = i,ok = n+1;
        while (ok-ng > 1) {
            int mid = (ok+ng)/2;
            int v = st.getmax(i,mid);
            (v > a[i] ? ok : ng) = mid;
        }
        if (ok <= n) b[i].push_back(ng);
    }
    
    for (int j = 0;j < 20;++j) {
        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);
    }*/
}

/*
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 = 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