結果

問題 No.618 labo-index
ユーザー merom686
提出日時 2017-12-19 23:28:47
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 47 ms / 6,000 ms
コード長 1,759 bytes
コンパイル時間 1,030 ms
コンパイル使用メモリ 83,704 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-12-16 02:24:49
合計ジャッジ時間 3,713 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

struct BIT {
    BIT(int n) : b(n + 1), n(n) {}
    void add(int i, int v) {
        for (int k = i + 1; k <= n; k += k & -k) b[k] += v;
    }
    int sum(int k) const {
        int s = 0;
        for (; k > 0; k -= k & -k) s += b[k];
        return s;
    }
    int lower_bound(int64_t s, const vector<pair<int64_t, int>>& a) const {
        if (s - a[0].first <= 0) return 0;
        int x = 0, w = 1;
        while (w << 1 < n) w <<= 1;
        do {
            int k = x + w;
            if (k < n && b[k] < s - a[k].first) {
                s -= b[k];
                x += w;
            }
        } while (w >>= 1);
        return x + 1;
    }
    vector<int> b;
    int n;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int q;
    cin >> q;

    vector<int> t(q), x(q);
    vector<pair<int64_t, int>> a;
    int64_t d = 0;
    for (int i = 0; i < q; i++) {
        cin >> t[i] >> x[i];
        if (t[i] == 1) a.push_back(pair<int64_t, int>(x[i] - d, i));
        if (t[i] == 2) x[i] = a[x[i] - 1].second;
        if (t[i] == 3) d += x[i];
    }
    int n = a.size();

    sort(a.begin(), a.end());

    vector<int> p(q, -1);
    for (int j = 0; j < n; j++) {
        p[a[j].second] = j;
    }

    BIT bt(n);
    d = 0;
    int m = 0;
    for (int i = 0; i < q; i++) {
        if (t[i] == 1) {
            bt.add(p[i], 1);
            m++;
        } else if (t[i] == 2) {
            bt.add(p[x[i]], -1);
            m--;
        } else {
            d += x[i];
        }
        int k = bt.lower_bound(m - d, a);
        cout << m - bt.sum(k) << '\n';
    }

    return 0;
}
0