結果

問題 No.2077 Get Minimum Algorithm
ユーザー 👑 NachiaNachia
提出日時 2022-09-16 21:39:38
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 570 ms / 3,000 ms
コード長 2,832 bytes
コンパイル時間 795 ms
コンパイル使用メモリ 84,440 KB
実行使用メモリ 80,460 KB
最終ジャッジ日時 2023-08-23 14:37:55
合計ジャッジ時間 15,700 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
19,868 KB
testcase_01 AC 26 ms
19,732 KB
testcase_02 AC 25 ms
19,796 KB
testcase_03 AC 28 ms
19,780 KB
testcase_04 AC 31 ms
19,860 KB
testcase_05 AC 28 ms
19,860 KB
testcase_06 AC 29 ms
19,932 KB
testcase_07 AC 27 ms
20,016 KB
testcase_08 AC 30 ms
19,988 KB
testcase_09 AC 31 ms
19,908 KB
testcase_10 AC 29 ms
19,820 KB
testcase_11 AC 31 ms
19,864 KB
testcase_12 AC 540 ms
80,268 KB
testcase_13 AC 520 ms
80,368 KB
testcase_14 AC 538 ms
80,368 KB
testcase_15 AC 566 ms
80,364 KB
testcase_16 AC 570 ms
80,324 KB
testcase_17 AC 564 ms
80,316 KB
testcase_18 AC 545 ms
80,276 KB
testcase_19 AC 549 ms
80,272 KB
testcase_20 AC 554 ms
80,460 KB
testcase_21 AC 545 ms
80,276 KB
testcase_22 AC 357 ms
80,328 KB
testcase_23 AC 360 ms
80,368 KB
testcase_24 AC 353 ms
80,268 KB
testcase_25 AC 362 ms
80,288 KB
testcase_26 AC 358 ms
80,384 KB
testcase_27 AC 479 ms
80,368 KB
testcase_28 AC 337 ms
49,872 KB
testcase_29 AC 165 ms
25,776 KB
testcase_30 AC 309 ms
80,284 KB
testcase_31 AC 249 ms
50,012 KB
testcase_32 AC 149 ms
25,820 KB
testcase_33 AC 247 ms
80,336 KB
testcase_34 AC 189 ms
50,160 KB
testcase_35 AC 116 ms
25,748 KB
testcase_36 AC 384 ms
80,324 KB
testcase_37 AC 321 ms
80,264 KB
testcase_38 AC 26 ms
19,808 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