結果

問題 No.2077 Get Minimum Algorithm
ユーザー 👑 NachiaNachia
提出日時 2022-09-16 21:39:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 545 ms / 3,000 ms
コード長 2,832 bytes
コンパイル時間 790 ms
コンパイル使用メモリ 84,932 KB
実行使用メモリ 80,756 KB
最終ジャッジ日時 2024-06-01 12:09:26
合計ジャッジ時間 15,102 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 27 ms
19,840 KB
testcase_01 AC 28 ms
19,840 KB
testcase_02 AC 28 ms
19,840 KB
testcase_03 AC 29 ms
19,840 KB
testcase_04 AC 33 ms
19,840 KB
testcase_05 AC 29 ms
19,840 KB
testcase_06 AC 30 ms
19,840 KB
testcase_07 AC 29 ms
19,840 KB
testcase_08 AC 32 ms
19,840 KB
testcase_09 AC 34 ms
19,968 KB
testcase_10 AC 29 ms
19,840 KB
testcase_11 AC 33 ms
20,096 KB
testcase_12 AC 522 ms
80,632 KB
testcase_13 AC 527 ms
80,628 KB
testcase_14 AC 520 ms
80,632 KB
testcase_15 AC 518 ms
80,632 KB
testcase_16 AC 528 ms
80,632 KB
testcase_17 AC 524 ms
80,628 KB
testcase_18 AC 535 ms
80,504 KB
testcase_19 AC 545 ms
80,632 KB
testcase_20 AC 528 ms
80,632 KB
testcase_21 AC 538 ms
80,628 KB
testcase_22 AC 370 ms
80,632 KB
testcase_23 AC 381 ms
80,628 KB
testcase_24 AC 370 ms
80,576 KB
testcase_25 AC 372 ms
80,628 KB
testcase_26 AC 379 ms
80,756 KB
testcase_27 AC 485 ms
80,636 KB
testcase_28 AC 344 ms
50,156 KB
testcase_29 AC 169 ms
25,856 KB
testcase_30 AC 311 ms
80,504 KB
testcase_31 AC 250 ms
50,036 KB
testcase_32 AC 141 ms
25,856 KB
testcase_33 AC 251 ms
80,628 KB
testcase_34 AC 196 ms
50,032 KB
testcase_35 AC 117 ms
25,856 KB
testcase_36 AC 386 ms
80,628 KB
testcase_37 AC 323 ms
80,632 KB
testcase_38 AC 28 ms
19,840 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