#pragma GCC optimize ("O3") #pragma GCC target ("avx") #include #define getchar getchar_unlocked #define putchar putchar_unlocked using namespace std; // left-leaning red-black tree template 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 L(n), cnt(n); for (int i = 0; i < n; i++) L[i] = read_int(); int T = read_int(); unordered_map> score; score.reserve(T + 100); Set> 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 }; } } }