結果

問題 No.878 Range High-Element Query
ユーザー tsutajtsutaj
提出日時 2019-09-06 22:56:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 424 ms / 2,000 ms
コード長 4,643 bytes
コンパイル時間 1,521 ms
コンパイル使用メモリ 130,680 KB
実行使用メモリ 11,620 KB
最終ジャッジ日時 2023-09-07 01:58:17
合計ジャッジ時間 5,564 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 3 ms
4,384 KB
testcase_05 AC 4 ms
4,380 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,376 KB
testcase_11 AC 383 ms
10,176 KB
testcase_12 AC 300 ms
10,176 KB
testcase_13 AC 303 ms
9,660 KB
testcase_14 AC 232 ms
7,104 KB
testcase_15 AC 295 ms
10,104 KB
testcase_16 AC 400 ms
10,612 KB
testcase_17 AC 423 ms
10,360 KB
testcase_18 AC 424 ms
11,620 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;

// 抽象 SegmentTree (0-indexed・一点更新・区間取得)
template <typename MonoidType>
struct SegmentTree {
    using Function = function< MonoidType(MonoidType, MonoidType) >;

    // node, identity element
    int n;
    vector<MonoidType> node;
    MonoidType E0;

    // update / combine function
    Function upd_f, cmb_f;

    void build(int m, vector<MonoidType> v = vector<MonoidType>()) {
        if(v != vector<MonoidType>()) m = v.size();
        n = 1; while(n < m) n *= 2;

        node = vector<MonoidType>(2*n-1, E0);
        if(v != vector<MonoidType>()) {
            for(int i=0; i<m; i++) {
                node[n-1+i] = v[i];
            }
            for(int i=n-2; i>=0; i--) {
                node[i] = cmb_f(node[2*i+1], node[2*i+2]);
            }
        }
    }

    // initialize
    SegmentTree() {}
    SegmentTree(int n_, MonoidType E0_,
                Function upd_f_, Function cmb_f_,
                vector<MonoidType> v = vector<MonoidType>()) :
        E0(E0_), upd_f(upd_f_), cmb_f(cmb_f_) {
        build(n_, v);
    }

    // update k-th element (applied value: x)
    void update(int k, MonoidType x) {
        k += n - 1;
        node[k] = upd_f(node[k], x);
        while(k > 0) {
            k = (k - 1) / 2;
            node[k] = cmb_f(node[2*k+1], node[2*k+2]);
        }
    }

    // range query for [a, b)
    // 非再帰のアイデア: http://d.hatena.ne.jp/komiyam/20131202/1385992406
    MonoidType query(int a, int b) {
        MonoidType vl = E0, vr = E0;
        for(int l=a+n, r=b+n; l<r; l>>=1, r>>=1) {
            if(l & 1) vl = cmb_f(vl, node[(l++)-1]);
            if(r & 1) vr = cmb_f(node[(--r)-1], vr);
        }
        return cmb_f(vl, vr);
    }
};

int main() {
    int N, Q; cin >> N >> Q;
    vector<int> A(N);
    vector< pair<int, int> > P(N);
    for(int i=0; i<N; i++) {
        cin >> A[i];
        P[i] = make_pair(A[i], i);
    }
    sort(P.rbegin(), P.rend());
    
    SegmentTree<int> seg1(N, 0,
                          [](int a, int b) { return a + b; },
                          [](int a, int b) { return a + b; });

    vector<int> C(N, -1);
    for(int i=0; i<N; i++) {
        int k = P[i].second;
        // fprintf(stderr, "k = %d\n", k);

        // k よりインデックスが小さくて高いもののうち最も近いもの
        int ub = k+1, lb = -1;
        while(ub - lb > 1) {
            int mid = (ub + lb) / 2;
            if(seg1.query(mid, k) > 0) lb = mid;
            else ub = mid;
        }

        // fprintf(stderr, "lb = %d\n", lb);
        if(lb < k) C[k] = lb;
        seg1.update(k, 1);
    }

    /*
    for(int i=0; i<N; i++) {
        cerr << C[i] << " \n"[i + 1 == N];
    }
    */

    vector< tuple<int, int, int, int> > queries;
    for(int i=0; i<N; i++) {
        queries.emplace_back(C[i], -1, i, 1);
    }
    for(int i=0; i<Q; i++) {
        int q, l, r; cin >> q >> l >> r;
        l--; r--;
        queries.emplace_back(l, r, i, 2);
    }

    sort(queries.begin(), queries.end(), [](auto x, auto y) {
            int xl, xr, xi, xt; tie(xl, xr, xi, xt) = x;
            int yl, yr, yi, yt; tie(yl, yr, yi, yt) = y;
            if(xl != yl) return xl < yl;
            if(xt != yt) return xt > yt;
            return xr < yr;
        });

    SegmentTree<int> seg2(N, 0,
                          [](int a, int b) { return a + b; },
                          [](int a, int b) { return a + b; });

    vector<int> ans(Q);
    for(auto e : queries) {
        int l, r, i, t; tie(l, r, i, t) = e;
        if(t == 1) {
            seg2.update(i, 1);
        }
        if(t == 2) {
            r++;
            ans[i] = seg2.query(l, r);
        }
    }

    for(int i=0; i<Q; i++) cout << ans[i] << endl;
    return 0;
}
0