結果

問題 No.2223 Merged Med
ユーザー ぷらぷら
提出日時 2023-02-17 22:34:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,548 ms / 7,000 ms
コード長 2,574 bytes
コンパイル時間 2,637 ms
コンパイル使用メモリ 214,160 KB
実行使用メモリ 8,788 KB
最終ジャッジ日時 2023-09-26 19:54:19
合計ジャッジ時間 30,234 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,500 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 715 ms
4,916 KB
testcase_14 AC 561 ms
4,580 KB
testcase_15 AC 502 ms
6,932 KB
testcase_16 AC 1,206 ms
8,192 KB
testcase_17 AC 357 ms
5,312 KB
testcase_18 AC 509 ms
4,376 KB
testcase_19 AC 666 ms
4,928 KB
testcase_20 AC 1,525 ms
8,784 KB
testcase_21 AC 1,530 ms
8,776 KB
testcase_22 AC 1,543 ms
8,768 KB
testcase_23 AC 1,545 ms
8,780 KB
testcase_24 AC 1,505 ms
8,720 KB
testcase_25 AC 1,515 ms
8,788 KB
testcase_26 AC 1,548 ms
8,780 KB
testcase_27 AC 1,526 ms
8,724 KB
testcase_28 AC 1,532 ms
8,664 KB
testcase_29 AC 1,532 ms
8,764 KB
testcase_30 AC 720 ms
8,764 KB
testcase_31 AC 705 ms
8,684 KB
testcase_32 AC 358 ms
5,144 KB
testcase_33 AC 1,275 ms
8,720 KB
testcase_34 AC 1,085 ms
8,780 KB
testcase_35 AC 1,072 ms
8,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int inf = 1001001001;

array<int,4> mrg(array<int,4>&a,array<int,4>&b) {
    array<int,4>ar = {0,0,0,0};
    ar[0] = max(a[0],a[2]+b[0]);
    ar[1] = max(b[1],b[2]+a[1]);
    ar[2] = a[2]+b[2];
    ar[3] = max({a[3],b[3],a[1]+b[0]});
    return ar;
}

struct SegmentTree {
    int n;
    vector<array<int,4>> dat;
    SegmentTree() {}
    SegmentTree(int N) {
        n = 1;
        while(n < N) {
            n *= 2;
        }
        dat.resize(2*n-1);
        for(int i = n-1; i < 2*n-1; i++) {
            dat[i] = {1,1,1,1};
        }
        for(int i = n-2; i >= 0; i--) {
            dat[i] = mrg(dat[i*2+1],dat[i*2+2]);
        }
    }
    void update(int x) {
        x += n-1;
        dat[x] = {0,0,-1,0};
        while(x > 0) {
            x = (x-1)/2;
            dat[x] = mrg(dat[2*x+1],dat[2*x+2]);
        }
    }
    array<int,4> query(int a, int b, int k, int l, int r) {
        if(r <= a || b <= l) return {0,0,0,0};
        if(a <= l && r <= b) return dat[k];
        array<int,4> vl = query(a, b, 2*k+1, l, (l+r)/2);
        array<int,4> vr = query(a, b, 2*k+2, (l+r)/2, r);
        return mrg(vl, vr);
    }
    array<int,4> query(int a,int b) {//[a,b)
        return query(a,b,0,0,n);
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N,Q;
    cin >> N >> Q;
    vector<int>A(N);
    for(int i = 0; i < N; i++) {
        cin >> A[i];
    }
    vector<int>L(Q),R(Q);
    for(int i = 0; i < Q; i++) {
        cin >> L[i] >> R[i];
        L[i]--;
        R[i]--;
    }
    vector<int>l(Q,0),r(Q,inf);
    while(true) {
        vector<array<int,3>>query;
        for(int i = 0; i < Q; i++) {
            if(l[i]+1 < r[i]) {
                int mid = (l[i]+r[i])/2;
                query.push_back({mid,1,i});
            }
        }
        if(query.empty()) break;
        for(int i = 0; i < N; i++) {
            query.push_back({A[i],0,i});
        }
        sort(query.begin(),query.end());
        SegmentTree Seg(N);
        for(int i = 0; i < query.size(); i++) {
            if(query[i][1] == 0) {
                Seg.update(query[i][2]);
            }
            else {
                int id = query[i][2];
                array<int,4> now = Seg.query(L[id],R[id]+1);
                if(-now[2]+max(0,now[3]-1) >= 1) {
                    r[id] = query[i][0];
                }
                else {
                    l[id] = query[i][0];
                }
            }
        }
    }
    for(int i = 0; i < Q; i++) {
        cout << r[i] << "\n";
    }
}
0