結果

問題 No.2220 Range Insert & Point Mex
ユーザー ぷらぷら
提出日時 2023-01-31 04:28:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,049 ms / 2,000 ms
コード長 3,339 bytes
コンパイル時間 2,816 ms
コンパイル使用メモリ 227,128 KB
実行使用メモリ 42,444 KB
最終ジャッジ日時 2023-09-28 17:59:49
合計ジャッジ時間 15,937 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 201 ms
10,808 KB
testcase_14 AC 202 ms
11,040 KB
testcase_15 AC 204 ms
10,788 KB
testcase_16 AC 204 ms
10,720 KB
testcase_17 AC 203 ms
10,740 KB
testcase_18 AC 202 ms
10,884 KB
testcase_19 AC 204 ms
10,836 KB
testcase_20 AC 1,049 ms
42,380 KB
testcase_21 AC 1,046 ms
42,444 KB
testcase_22 AC 1,038 ms
42,404 KB
testcase_23 AC 805 ms
42,040 KB
testcase_24 AC 743 ms
40,380 KB
testcase_25 AC 448 ms
24,664 KB
testcase_26 AC 743 ms
42,056 KB
testcase_27 AC 526 ms
37,916 KB
testcase_28 AC 32 ms
7,216 KB
testcase_29 AC 36 ms
7,216 KB
testcase_30 AC 36 ms
7,220 KB
testcase_31 AC 122 ms
9,956 KB
testcase_32 AC 148 ms
10,180 KB
testcase_33 AC 148 ms
10,316 KB
testcase_34 AC 475 ms
22,952 KB
testcase_35 AC 469 ms
22,808 KB
testcase_36 AC 437 ms
22,364 KB
testcase_37 AC 434 ms
22,220 KB
testcase_38 AC 513 ms
26,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template <typename T>
struct BinaryIndexedTree {
    int N;
    vector<T>bit;
    BinaryIndexedTree(){}
    BinaryIndexedTree(int x) {
        N = x;
        bit.resize(x+1);
    }
    void add(int x,T y) {
        x++;
        while(x <= N) {
            bit[x] += y;
            x += x&-x;
        }
    }
    T sum(int x) {
        x++;
        T res = 0;
        while(x) {
            res += bit[x];
            x -= x&-x;
        }
        return res;
    }
    inline T sum(int l, int r) {//[l,r)
        return sum(r-1)-sum(l-1);
    }
    inline T operator[](int k) {
        return sum(k)-sum(k-1);
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N;
    cin >> N;
    vector<int>L(N),R(N),a(N),cmp;
    for(int i = 0; i < N; i++) {
        cin >> L[i] >> R[i] >> a[i];
        cmp.push_back(L[i]);
        cmp.push_back(R[i]+1);
    }
    int Q;
    cin >> Q;
    vector<int>x(Q);
    for(int i = 0; i < Q; i++) {
        cin >> x[i];
        cmp.push_back(x[i]);
    }
    sort(cmp.begin(),cmp.end());
    cmp.erase(unique(cmp.begin(),cmp.end()),cmp.end());
    for(int i = 0; i < N; i++) {
        L[i] = lower_bound(cmp.begin(),cmp.end(),L[i])-cmp.begin();
        R[i] = lower_bound(cmp.begin(),cmp.end(),R[i]+1)-cmp.begin();
    }
    for(int i = 0; i < Q; i++) {
        x[i] = lower_bound(cmp.begin(),cmp.end(),x[i])-cmp.begin();
    }
    map<int,vector<pair<int,int>>>mp;
    for(int i = 0; i < N; i++) {
        mp[a[i]].push_back({L[i],R[i]});
    }
    map<int,vector<pair<int,int>>>mp2;
    for(auto i:mp) {
        vector<pair<int,int>>tmp = i.second;
        sort(tmp.begin(),tmp.end());
        vector<pair<int,int>>res;
        int mi = -1,mx = -1;
        for(int k = 0; k < tmp.size(); k++) {
            if(mx == -1) {
                mi = tmp[k].first;
                mx = tmp[k].second;
            }
            else if(mx <= tmp[k].first) {
                res.push_back({mi,mx});
                mi = tmp[k].first;
                mx = tmp[k].second;
            }
            else {
                mx = max(mx,tmp[k].second);
            }
        }
        res.push_back({mi,mx});
        mp2[i.first] = res;
    }
    vector<int>l(Q),r(Q,N+3);
    while(true) {
        bool flag = false;
        for(int i = 0; i < Q; i++) {
            if(l[i]+1 < r[i]) {
                flag = true;
            }
        }
        if(!flag) break;
        map<int,vector<pair<int,int>>>mp3 = mp2;
        for(int i = 0; i < Q; i++) {
            if(l[i]+1 < r[i]) {
                int mid = (l[i]+r[i])/2;
                mp3[mid].push_back({i,-1});
            }
        }
        BinaryIndexedTree<int>bit(cmp.size());
        for(auto i:mp3) {
            for(auto k:i.second) {
                if(k.second == -1) {
                    if(bit.sum(x[k.first]) == i.first) {
                        l[k.first] = i.first;
                    }
                    else {
                        r[k.first] = i.first;
                    }
                }
            }
            for(auto k:i.second) {
                if(k.second == -1) continue;
                bit.add(k.first,1);
                bit.add(k.second,-1);
            }
        }
    }
    for(int i = 0; i < Q; i++) {
        cout << l[i] << "\n";
    }
}
0