結果

問題 No.2809 Sort Query
ユーザー GOTKAKOGOTKAKO
提出日時 2024-07-12 23:20:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 454 ms / 2,000 ms
コード長 5,611 bytes
コンパイル時間 3,293 ms
コンパイル使用メモリ 224,416 KB
実行使用メモリ 44,416 KB
最終ジャッジ日時 2024-07-12 23:21:08
合計ジャッジ時間 33,469 ms
ジャッジサーバーID
(参考情報)
judge1 / judge6
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 331 ms
26,320 KB
testcase_02 AC 360 ms
26,232 KB
testcase_03 AC 341 ms
26,260 KB
testcase_04 AC 332 ms
26,324 KB
testcase_05 AC 351 ms
26,148 KB
testcase_06 AC 243 ms
21,220 KB
testcase_07 AC 246 ms
21,576 KB
testcase_08 AC 241 ms
21,384 KB
testcase_09 AC 256 ms
21,044 KB
testcase_10 AC 239 ms
21,108 KB
testcase_11 AC 249 ms
28,416 KB
testcase_12 AC 244 ms
27,776 KB
testcase_13 AC 267 ms
27,904 KB
testcase_14 AC 250 ms
28,288 KB
testcase_15 AC 247 ms
27,648 KB
testcase_16 AC 254 ms
28,032 KB
testcase_17 AC 256 ms
28,672 KB
testcase_18 AC 265 ms
28,800 KB
testcase_19 AC 251 ms
28,164 KB
testcase_20 AC 246 ms
27,648 KB
testcase_21 AC 421 ms
44,164 KB
testcase_22 AC 446 ms
44,168 KB
testcase_23 AC 417 ms
44,416 KB
testcase_24 AC 417 ms
44,292 KB
testcase_25 AC 454 ms
44,036 KB
testcase_26 AC 389 ms
30,808 KB
testcase_27 AC 407 ms
30,804 KB
testcase_28 AC 386 ms
30,756 KB
testcase_29 AC 388 ms
30,736 KB
testcase_30 AC 415 ms
29,768 KB
testcase_31 AC 183 ms
22,372 KB
testcase_32 AC 181 ms
21,944 KB
testcase_33 AC 179 ms
21,856 KB
testcase_34 AC 181 ms
21,916 KB
testcase_35 AC 180 ms
21,796 KB
testcase_36 AC 187 ms
27,648 KB
testcase_37 AC 188 ms
26,240 KB
testcase_38 AC 205 ms
26,112 KB
testcase_39 AC 193 ms
26,756 KB
testcase_40 AC 190 ms
27,264 KB
testcase_41 AC 379 ms
25,432 KB
testcase_42 AC 387 ms
25,452 KB
testcase_43 AC 381 ms
25,452 KB
testcase_44 AC 378 ms
25,436 KB
testcase_45 AC 390 ms
25,436 KB
testcase_46 AC 325 ms
25,500 KB
testcase_47 AC 320 ms
25,396 KB
testcase_48 AC 326 ms
25,436 KB
testcase_49 AC 332 ms
25,452 KB
testcase_50 AC 321 ms
25,480 KB
testcase_51 AC 186 ms
25,496 KB
testcase_52 AC 186 ms
25,476 KB
testcase_53 AC 187 ms
25,420 KB
testcase_54 AC 190 ms
25,332 KB
testcase_55 AC 186 ms
25,424 KB
testcase_56 AC 274 ms
18,120 KB
testcase_57 AC 228 ms
16,428 KB
testcase_58 AC 206 ms
15,880 KB
testcase_59 AC 271 ms
16,992 KB
testcase_60 AC 217 ms
20,492 KB
testcase_61 AC 272 ms
22,860 KB
testcase_62 AC 210 ms
16,324 KB
testcase_63 AC 283 ms
22,624 KB
testcase_64 AC 340 ms
24,628 KB
testcase_65 AC 182 ms
14,936 KB
testcase_66 AC 2 ms
6,940 KB
testcase_67 AC 2 ms
6,940 KB
testcase_68 AC 2 ms
6,940 KB
testcase_69 AC 2 ms
6,940 KB
testcase_70 AC 2 ms
6,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

vector<long long> zaatull(vector<long long> &A){
    vector<long long> B = A;
    sort(B.begin(),B.end());
    B.erase(unique(B.begin(),B.end()),B.end());
    
    return B;
}

using SS = long long;
class SegmentTree{
    public:
    long long siz = 1,n;
    vector<SS> dat;
 
    SS op(SS a, SS b){return a+b;}
    SS e(){return 0;}
    void renew (SS &a,SS x){
        a = op(a,x);
        //a = x;
        //その他.
    }
 
    void make(long long N){
        while(siz < N) siz *= 2;
        n = N; dat.resize(siz*2,e());
    }
 
    void make2(long long N,vector<SS> &A){
        make(N);
        for(long long i=0; i<N; i++){
            long long pos = i+siz;
            dat.at(pos) = A.at(i);
        }
        for(long long i=siz-1; i>0; i--) dat.at(i) = op(dat.at(i*2),dat.at(i*2+1));
    }
 
    void update(long long pos,SS x){
        pos = pos+siz;
        renew(dat.at(pos),x);
        while(pos != 1){
            pos = pos/2;
            dat.at(pos) = op(dat.at(pos*2),dat.at(pos*2+1));
        }
    }
 
    SS findans(long long l, long long r){
        SS retl = e(),retr = e();
        l += siz,r += siz;
        while(l < r){
            if(l&1) retl = op(retl,dat.at(l++));
            if(r&1) retr = op(dat.at(--r),retr);
            l >>= 1; r >>= 1;
        }
        return op(retl,retr);
    }
 
    SS get(long long pos){return dat.at(pos+siz);}
    SS rangeans(long long l, long long r){return findans(l,r);}
    SS allrange(){return dat.at(1);}
 
    //rightは) leftは[で 渡す&返す. 
    long long maxright(const function<bool(SS)> f,long long l = 0){
        l += siz; long long r = n+siz;
        vector<long long> ls,rs;
        while(l < r){
            if(l&1) ls.push_back(l++);
            if(r&1) rs.push_back(--r);
            l >>= 1; r >>= 1; 
        }
        SS okl = e();
        for(long long i=0; i<ls.size(); i++){
            l = ls.at(i);
            SS now = op(okl,dat.at(l));
            if(!f(now)){
                while(l < siz){
                    l <<= 1;
                    now = op(okl,dat.at(l));
                    if(f(now)){okl = now; l++;}
                }
                return l-siz;
            } 
            okl = now;
        }
        for(long long i=rs.size()-1; i>=0; i--){
            l = rs.at(i);
            SS now = op(okl,dat.at(l));
            if(!f(now)){
                while(l < siz){
                    l <<= 1;
                    now = op(okl,dat.at(l));
                    if(f(now)){okl = now; l++;}
                }
                return l-siz;
            } 
            okl = now;
        }
        return n;
    }
    long long minleft(const function<bool(SS)> f,long long r = -1){
        if(r == -1) r = n;
        long long l = siz; r += siz;
        vector<long long> ls,rs;
        while(l < r){
            if(l&1) ls.push_back(l++);
            if(r&1) rs.push_back(--r);
            l >>= 1; r >>= 1; 
        }
        SS okr = e();
        for(long long i=0; i<rs.size(); i++){
            r = rs.at(i);
            SS now = op(dat.at(r),okr);
            if(!f(now)){
                while(r < siz){
                    r <<= 1; r++;
                    now = op(dat.at(r),okr);
                    if(f(now)){okr = now; r--;}
                }
                return r+1-siz;
            }
        }
        for(long long i=ls.size()-1; i>=0; i--){
            r = ls.at(i);
            SS now = op(dat.at(r),okr);
            if(!f(now)){
                while(r < siz){
                    r <<= 1; r++;
                    now = op(dat.at(r),okr);
                    if(f(now)){okr = now; r--;}
                }
                return r+1-siz;
            }
        }
        return 0;
    }
    //ノーマルセグ木.一年の時を経て漸く二分探索実装した.
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    long long N,Q; cin >> N >> Q;
    vector<long long> B(N,-1);
    vector<long long> St;
    vector<long long> A(N),all;
    for(auto &a : A) cin >> a;
    all = A;
    vector<tuple<long long,long long,long long>> Query(Q,{-1,-1,-1});
    for(auto &[t,k,x] : Query){
        cin >> t;
        if(t == 1) cin >> k >> x,k--,all.push_back(x);
        if(t == 3) cin >> k,k--;
    }
    all = zaatull(all);
    long long n = all.size();

    for(auto &a : A) a = lower_bound(all.begin(),all.end(),a)-all.begin();
    for(auto &[t,k,x] : Query) if(t == 1) x = lower_bound(all.begin(),all.end(),x)-all.begin();

    SegmentTree Z; Z.make(n);
    for(auto &a : A) Z.update(a,1);

    long long border = -1;
    auto f = [&](SS x){return x<border;};

    bool sorted = false;
    for(auto &[t,k,x] : Query){
        if(t == 1){
            St.push_back(k);
            B.at(k) = x;
        }
        if(t == 2){
            vector<pair<int,int>> change;
            for(auto pos : St){
                if(B.at(pos) == -1) continue;
                border = pos+1;
                long long a1 = Z.maxright(f),a2 = B.at(pos);
                if(sorted == false) a1 = A.at(pos);
                change.push_back({a1,a2}); 
                B.at(pos) = -1;
            }
            for(auto &[a1,a2] : change) Z.update(a1,-1),Z.update(a2,1);
            sorted = true;
            St.clear();
        }
        if(t == 3){
            if(B.at(k) != -1) cout << all.at(B.at(k)) << "\n";
            else if(sorted == false) cout << all.at(A.at(k)) << "\n";
            else{
                border = k+1;
                cout << all.at(Z.maxright(f)) << "\n";
            }
        }
    }
}
0