結果

問題 No.878 Range High-Element Query
ユーザー kyort0nkyort0n
提出日時 2019-09-06 22:11:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 345 ms / 2,000 ms
コード長 2,530 bytes
コンパイル時間 1,965 ms
コンパイル使用メモリ 183,004 KB
実行使用メモリ 19,644 KB
最終ジャッジ日時 2023-09-07 00:19:11
合計ジャッジ時間 5,872 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
10,088 KB
testcase_01 AC 8 ms
10,196 KB
testcase_02 AC 8 ms
10,208 KB
testcase_03 AC 7 ms
10,208 KB
testcase_04 AC 8 ms
10,056 KB
testcase_05 AC 9 ms
10,044 KB
testcase_06 AC 7 ms
10,004 KB
testcase_07 AC 7 ms
10,200 KB
testcase_08 AC 9 ms
10,040 KB
testcase_09 AC 9 ms
10,208 KB
testcase_10 AC 8 ms
10,152 KB
testcase_11 AC 325 ms
18,048 KB
testcase_12 AC 234 ms
17,892 KB
testcase_13 AC 258 ms
16,400 KB
testcase_14 AC 196 ms
15,084 KB
testcase_15 AC 235 ms
17,356 KB
testcase_16 AC 327 ms
19,244 KB
testcase_17 AC 345 ms
19,644 KB
testcase_18 AC 345 ms
19,596 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
//const ll mod = 1000000007;
struct SegmentTree {
private:
    int n;
    vector<ll> node;
 
public:
    SegmentTree() {
        int sz = 100050;
        n = 1; while(n < sz) n *= 2;
        node.resize(2*n-1, 0);
        for(int i=0; i<sz; i++) node[i+n-1] = 0;
        for(int i=n-2; i>=0; i--) node[i] = min(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] = (node[2*x+1] + node[2*x+2]);
        }
    }
    // hannkaikukann 
    ll getsum(int a, int b, int k=0, int l=0, int r=-1) {
        if(r < 0) r = n;
        if(r <= a || b <= l) return 0;
        if(a <= l && r <= b) return node[k];
 
        ll vl = getsum(a, b, 2*k+1, l, (l+r)/2);
        ll vr = getsum(a, b, 2*k+2, (l+r)/2, r);
        return (vl + vr);
    }
};

int ans[105000];
int a[105000];
vector<i_i> v;
vector<int> elements[105000];
vector<i_i> queries[105000];

int main() {
    //cout.precision(10);
    int N, Q;
    cin >> N >> Q;
    for(int i = 1; i <= N; i++) {
        cin >> a[i];
        v.push_back({a[i], i});
    }
    sort(v.begin(), v.end(), greater<i_i>());
    set<int> appeared;
    for(int i = 0; i < v.size(); i++) {
        int index = v[i].second;
        auto itr = appeared.lower_bound(index);
        if(itr == appeared.begin()) {
            elements[0].push_back(index);
        } else {
            itr--;
            elements[*itr + 1].push_back(index);
        }
        appeared.insert(index);
    }
    for(int i = 1; i <= Q; i++) {
        int q, l, r;
        cin >> q >> l >> r;
        queries[l].push_back({i, r});
    }
    SegmentTree seg;
    for(int val = 0; val <= 100000; val++) {
        for(int i = 0; i < elements[val].size(); i++) {
            seg.update(elements[val][i], 1);
        }
        for(int i = 0; i < queries[val].size(); i++) {
            ans[queries[val][i].first] = seg.getsum(val, queries[val][i].second + 1);
        }
    }
    for(int i = 1; i <= Q; i++) {
        cout << ans[i] << endl;
    }
    return 0;
}
0