結果

問題 No.449 ゆきこーだーの雨と雪 (4)
ユーザー pekempey
提出日時 2016-11-19 03:08:42
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 75 ms / 5,000 ms
コード長 3,726 bytes
コンパイル時間 3,345 ms
コンパイル使用メモリ 202,952 KB
実行使用メモリ 16,752 KB
最終ジャッジ日時 2024-09-22 10:22:12
合計ジャッジ時間 7,355 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("O3")
#pragma GCC target ("avx")
#include <bits/stdc++.h>
#define getchar getchar_unlocked
#define putchar putchar_unlocked
using namespace std;

// left-leaning red-black tree
template<class TKey>
class Set {
public:
    void insert(TKey key) {
        root = insert(root, create(key));
        root->c = false;
    }

    void erase(TKey key) {
        root = erase(root, key);
    }

    int index_of(TKey key) {
        return index_of(root, key);
    }

private:
    struct node {
        node *l;
        node *r;
        bool c = 0;
        bool removed = false;
        int sz = 0;
        TKey key;
    };

    int ptr = 0;
    node pool[101010];

    node *nil = new node();
    node *root = nil;

    node *create(TKey key) {
        node *res = &pool[ptr++];
        res->l = res->r = nil;
        res->c = true;
        res->sz = 1;
        res->key = key;
        return res;
    }

    node *fix(node *x) {
        if (x == nil) return x;
        x->sz = !x->removed + x->l->sz + x->r->sz;
        return x;
    }

    node *rotL(node *x) {
        node *y = x->r;
        node *t = y->l;
        y->l = x;
        x->r = t;
        y->c = x->c;
        x->c = true;
        fix(x);
        return fix(y);
    }

    node *rotR(node *x) {
        node *y = x->l;
        node *t = y->r;
        y->r = x;
        x->l = t;
        y->l->c = false;
        x->c = false;
        y->c = true;
        fix(x);
        return fix(y);
    }

    node *insert(node *x, node *y) {
        if (x == nil) return y;
        if (y->key < x->key) {
            x->l = insert(x->l, y);
        } else {
            x->r = insert(x->r, y);
        }
        fix(x);
        if (x->r->c) x = rotL(x);
        if (x->l->c && x->l->l->c) x = rotR(x);
        return x;
    }

    node *erase(node *x, TKey key) {
        if (!x->removed && x->key == key) {
            x->removed = true;
            return fix(x);
        }
        if (key < x->key) {
            x->l = erase(x->l, key);
        } else {
            x->r = erase(x->r, key);
        }
        return fix(x);
    }

    int index_of(node *x, TKey key) {
        if (x->key == key) return x->l->sz;
        if (key < x->key) {
            return index_of(x->l, key);
        } else {
            return x->l->sz + !x->removed + index_of(x->r, key);
        }
    }
};

int read_int() {
    int n, c;
    while ((c = getchar()) < '0') if (c == EOF) abort();
    n = c - '0';
    while ((c = getchar()) >= '0') n = n * 10 + c - '0';
    return n;
}

string read_string() {
    char buf[256];
    int c, len = 0;
    while ((c = getchar()) < '!') if (c == EOF) abort();
    buf[len++] = c;
    while ((c = getchar()) >= '!') buf[len++] = c;
    buf[len] = '\0';
    return string(buf);
}

void put_int(int n) {
    int res[11], i = 0;
    do { res[i++] = n % 10, n /= 10; } while (n);
    while (i) putchar(res[--i] + '0');
    putchar('\n');
}

int main() {
    int n = read_int();
    vector<int> L(n), cnt(n);
    for (int i = 0; i < n; i++) L[i] = read_int();
    int T = read_int();
    unordered_map<string, pair<int, int>> score;
    score.reserve(T + 100);
    Set<pair<int, int>> st;
    for (int ii = 0; ii < T; ii++) {
        string name = read_string();
        char prob = read_string()[0];
        int s, t;
        auto &tmp = score[name];
        tie(s, t) = tmp;
        if (prob == '?') {
            put_int(score.size() - st.index_of(score[name]));
        } else {
            int id = prob - 'A';
            cnt[id]++;
            int ss = s + 50 * L[id] + 500 * L[id] / (8 + 2 * cnt[id]);
            if (s > 0) st.erase({ s, t });
            st.insert({ ss, -ii });
            tmp = { ss, -ii };
        }
    }
}
0