結果

問題 No.2421 entersys?
ユーザー t98slidert98slider
提出日時 2023-08-12 14:34:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 590 ms / 3,000 ms
コード長 3,255 bytes
コンパイル時間 2,763 ms
コンパイル使用メモリ 210,588 KB
実行使用メモリ 39,004 KB
最終ジャッジ日時 2024-04-30 06:29:08
合計ジャッジ時間 12,634 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 4 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 4 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 436 ms
30,384 KB
testcase_12 AC 438 ms
30,132 KB
testcase_13 AC 466 ms
30,148 KB
testcase_14 AC 445 ms
30,040 KB
testcase_15 AC 430 ms
30,252 KB
testcase_16 AC 432 ms
32,692 KB
testcase_17 AC 444 ms
32,688 KB
testcase_18 AC 442 ms
32,552 KB
testcase_19 AC 448 ms
32,812 KB
testcase_20 AC 454 ms
32,632 KB
testcase_21 AC 429 ms
31,744 KB
testcase_22 AC 311 ms
26,956 KB
testcase_23 AC 582 ms
39,004 KB
testcase_24 AC 569 ms
38,880 KB
testcase_25 AC 590 ms
38,912 KB
testcase_26 AC 289 ms
25,976 KB
testcase_27 AC 316 ms
25,724 KB
testcase_28 AC 312 ms
25,840 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template <class T> struct fenwick_tree {
    using U = T;

    public:
    fenwick_tree() : _n(0) {}
    fenwick_tree(int n) : _n(n), data(n) {}

    void add(int p, T x) {
        assert(0 <= p && p < _n);
        p++;
        while (p <= _n) {
            data[p - 1] += U(x);
            p += p & -p;
        }
    }

    T sum(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        return sum(r) - sum(l);
    }

    private:
    int _n;
    std::vector<U> data;

    U sum(int r) {
        U s = 0;
        while (r > 0) {
            s += data[r - 1];
            r -= r & -r;
        }
        return s;
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    vector<tuple<string, int, int>> a(n);
    vector<string> ca(n);
    vector<int> ct(2 * n);
    for(int i = 0; i < n; i++){
        string s;
        int l, r;
        cin >> s >> l >> r;
        a[i] = make_tuple(s, l, r);
        ca[i] = s;
        ct[i] = l;
        ct[i + n] = r;
        ct.push_back(r + 1);
    }
    int Q;
    cin >> Q;
    vector<tuple<int, string, int, int>> query(Q);
    for(int i = 0; i < Q; i++){
        string s;
        int cmd, l, r, x;
        cin >> cmd;
        if(cmd == 1){
            cin >> s >> x;
            ca.push_back(s);
            query[i] = make_tuple(cmd, s, x, 0);
            ct.push_back(x);
        }else if(cmd == 2){
            cin >> x;
            query[i] = make_tuple(cmd, "", x, 0);
            ct.push_back(x);
        }else if(cmd == 3){
            cin >> s >> l >> r;
            query[i] = make_tuple(cmd, s, l, r);
            ca.push_back(s);
            ct.push_back(l);
            ct.push_back(r);
            ct.push_back(r + 1);
        }
    }
    sort(ca.begin(), ca.end());
    ca.erase(unique(ca.begin(), ca.end()), ca.end());
    ct.push_back(0);
    sort(ct.begin(), ct.end());
    ct.erase(unique(ct.begin(), ct.end()), ct.end());

    fenwick_tree<int> fw(ct.size() + 1);
    vector<set<pair<int,int>>> S(ca.size());
    for(int i = 0; i < ca.size(); i++) S[i].insert({1 << 30, 1 << 30});
    auto add = [&](string s, int l, int r){
        int id = lower_bound(ca.begin(), ca.end(), s) - ca.begin();
        S[id].insert({r, l});
        l = lower_bound(ct.begin(), ct.end(), l) - ct.begin();
        r = lower_bound(ct.begin(), ct.end(), r) - ct.begin();
        fw.add(l, 1);
        fw.add(r + 1, -1);
    };
    auto search = [&](string s, int t){
        int id = lower_bound(ca.begin(), ca.end(), s) - ca.begin();
        auto it = S[id].lower_bound({t, -1});
        if(it->second <= t && t <= it->first) return true;
        return false;
    };

    for(int i = 0; i < n; i++){
        string s;
        int l, r;
        tie(s, l, r) = a[i];
        add(s, l, r);
    }

    for(int i = 0; i < Q; i++){
        string s;
        int cmd, l, r;
        tie(cmd, s, l, r) = query[i];
        if(cmd == 1){
            cout << (search(s, l) ? "Yes" : "No") << '\n';
        }else if(cmd == 2){
            int t = lower_bound(ct.begin(), ct.end(), l) - ct.begin();
            cout << fw.sum(0, t + 1) << '\n';
        }else{
            add(s, l, r);
        }
    }
}
0