結果

問題 No.2077 Get Minimum Algorithm
ユーザー NachiaNachia
提出日時 2022-09-16 21:39:38
言語 C++17
(gcc 11.2.0 + boost 1.78.0)
結果
AC  
実行時間 532 ms / 3,000 ms
コード長 2,832 bytes
コンパイル時間 782 ms
使用メモリ 80,488 KB
最終ジャッジ日時 2023-01-11 06:14:26
合計ジャッジ時間 13,571 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 27 ms
19,920 KB
testcase_01 AC 27 ms
19,904 KB
testcase_02 AC 27 ms
19,888 KB
testcase_03 AC 29 ms
19,920 KB
testcase_04 AC 32 ms
19,900 KB
testcase_05 AC 30 ms
20,000 KB
testcase_06 AC 30 ms
19,932 KB
testcase_07 AC 29 ms
19,852 KB
testcase_08 AC 32 ms
19,904 KB
testcase_09 AC 34 ms
19,912 KB
testcase_10 AC 30 ms
19,936 KB
testcase_11 AC 33 ms
19,952 KB
testcase_12 AC 431 ms
80,292 KB
testcase_13 AC 440 ms
80,376 KB
testcase_14 AC 442 ms
80,300 KB
testcase_15 AC 433 ms
80,404 KB
testcase_16 AC 532 ms
80,292 KB
testcase_17 AC 432 ms
80,344 KB
testcase_18 AC 458 ms
80,296 KB
testcase_19 AC 433 ms
80,300 KB
testcase_20 AC 430 ms
80,372 KB
testcase_21 AC 428 ms
80,304 KB
testcase_22 AC 332 ms
80,416 KB
testcase_23 AC 324 ms
80,412 KB
testcase_24 AC 330 ms
80,300 KB
testcase_25 AC 332 ms
80,488 KB
testcase_26 AC 329 ms
80,400 KB
testcase_27 AC 399 ms
80,352 KB
testcase_28 AC 283 ms
49,948 KB
testcase_29 AC 156 ms
25,876 KB
testcase_30 AC 278 ms
80,372 KB
testcase_31 AC 222 ms
49,896 KB
testcase_32 AC 133 ms
25,940 KB
testcase_33 AC 237 ms
80,440 KB
testcase_34 AC 173 ms
49,896 KB
testcase_35 AC 113 ms
25,856 KB
testcase_36 AC 348 ms
80,296 KB
testcase_37 AC 301 ms
80,420 KB
testcase_38 AC 27 ms
19,884 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <atcoder/modint>

using namespace std;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
#define rep(i,n) for(int i=0; i<(int)(n); i++)

using Modint = atcoder::static_modint<998244353>;



int SegN = 1<<18;

struct PersistentSegtreeNode{
    PersistentSegtreeNode *l;
    PersistentSegtreeNode *r;
    int sum;

    static PersistentSegtreeNode* up(
        PersistentSegtreeNode* l,
        PersistentSegtreeNode* r
    ){
        PersistentSegtreeNode* res = new PersistentSegtreeNode();
        res->l = l;
        res->r = r;
        res->sum = l->sum + r->sum;
        return res;
    }
};

PersistentSegtreeNode* nodeSeg(int val){
    PersistentSegtreeNode* res = new PersistentSegtreeNode();
    res->l = res->r = nullptr;
    res->sum = val;
    return res;
}

PersistentSegtreeNode* initSeg(int a, int b){
    if(b-a == 1) return nodeSeg(0);
    int mid = (a + b) / 2;
    return PersistentSegtreeNode::up(initSeg(a, mid), initSeg(mid, b));
}

PersistentSegtreeNode* setSeg(
    PersistentSegtreeNode* p,
    int pos,
    int val,
    int a = -1,
    int b = -1
){
    if(a == -1){ a = 0; b = SegN; }
    assert(a <= pos); assert(pos < b);
    if(b-a == 1) return nodeSeg(val);
    int mid = (a + b) / 2;
    if(pos < mid){
        return PersistentSegtreeNode::up(
            setSeg(p->l, pos, val, a, mid),
            p->r
        );
    }
    else{
        return PersistentSegtreeNode::up(
            p->l,
            setSeg(p->r, pos, val, mid, b)
        );
    }
}

int main(){
    int N; cin >> N;
    vector<int> P(N);
    rep(i,N) cin >> P[i];
    vector<int> I(N);
    rep(i,N) I[P[i]-1] = i;

    vector<PersistentSegtreeNode*> seg;
    seg.push_back(initSeg(0, SegN));
    rep(i,N) seg.push_back(setSeg(seg.back(), I[i], 1));

    int Q; cin >> Q;
    rep(i,Q){
        int u,v; cin >> u >> v;
        int x = v-1;
        int a = 0, b = SegN;
        vector<pair<PersistentSegtreeNode*, int>> T;
        T.push_back({ seg[v], 1 });
        while(b-a > 1){
            int m = (a+b) / 2;
            int tmp = 0;
            for(auto& Tn : T){
                auto q = Tn.first;
                auto w = Tn.second;
                tmp += q->l->sum * w;
            }
            if(tmp <= u){
                a = m;
                for(auto& Tn : T) Tn.first = Tn.first->r;
                u -= tmp;
            }
            else{
                b = m;
                for(auto& Tn : T) Tn.first = Tn.first->l;
            }
        }
        int ans = max(a, I[x]) + 1;
        cout << ans << '\n';
    }
    return 0;
}


struct ios_do_not_sync{
    ios_do_not_sync(){
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
    }
} ios_do_not_sync_instance;


0