/* -*- coding: utf-8 -*- * * 449.cc: No.447 ゆきこーだーの雨と雪 (4) - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 26; const int MAX_T = 100000; const int INF = 1 << 30; /* typedef */ typedef map msi; struct Stat { int id, sum, t; Stat() {} Stat(int _id, int _sum, int _t): id(_id), sum(_sum), t(_t) {} bool operator<(const Stat &s) const { return sum > s.sum || (sum == s.sum && t < s.t); } bool operator==(const Stat &s) const { return sum == s.sum && t == s.t; } }; const Stat STINF(-1, -1, INF); template struct Treap { struct Node { T key; int fix, size; Node *left, *right; void print(int k) { printf("%d: key=%lld, fix=%d, size=%d\n", k, key, fix, size); } void print() { print(0); } }; typedef Node *node; node root, nullnode, buf; Node buf0[BUFSIZE], buf1; const T TINF; Treap(T tinf): TINF(tinf) { nullnode = &buf1; nullnode->left = nullnode->right = nullnode; nullnode->fix = nullnode->size = 0; clear(); srand(time(NULL)); } void clear() { buf = buf0; root = nullnode; } int size() { return root->size; } void update(node& t) { t->size = 1 + t->left->size + t->right->size; } node gen_node(T x) { node t = buf++; t->left = t->right = nullnode; t->key = x; t->fix = rand(); t->size = 1; return t; } void rot_l(node& k1) { node k2 = k1->right; k1->right = k2->left; k2->left = k1; update(k1); update(k2); k1 = k2; } void rot_r(node& k1) { node k2 = k1->left; k1->left = k2->right; k2->right = k1; update(k1); update(k2); k1 = k2; } void insert(node& t, T x) { if (t == nullnode) t = gen_node(x); else { if (t->key == x) return; if (x < t->key) { insert(t->left, x); update(t); if (t->left->fix > t->fix) rot_r(t); } else { insert(t->right, x); update(t); if (t->right->fix > t->fix) rot_l(t); } } } void insert(T x) { insert(root, x); } void remove_node(node& t) { if (t == nullnode) return; if (t->left == nullnode || t->right == nullnode) { if (t->left == nullnode) t = t->right; else t = t->left; } else { if (t->left->fix < t->right->fix) { rot_l(t); remove_node(t->left); update(t); } else { rot_r(t); remove_node(t->right); update(t); } } } void remove(node& t, T x) { if (t != nullnode) { if (t->key == x) remove_node(t); else if (x < t->key) { remove(t->left, x); update(t); } else { remove(t->right, x); update(t); } } } void remove(T x) { remove(root, x); } int lower_bound(T x) { node t = root; int k = 0; while (t != nullnode) { if (t->key < x) { k += t->left->size + 1; t = t->right; } else { t = t->left; } } return k; } void print(node t, int k, int indent) { if (t == nullnode) return; if (t != NULL) { for (int i = 0; i < indent; i++) cout << " "; t->print(k + t->left->size); print(t->left, k, indent + 1); print(t->right, k + t->left->size + 1, indent + 1); } } void print() { print(root, 0, 0); } }; /* global variables */ int ls[MAX_N], acs[MAX_N], scs[MAX_T][MAX_N]; Stat ss[MAX_T]; string nms[MAX_T]; msi nmmap; Treap trp(STINF); /* subroutines */ /* main */ int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> ls[i]; int t, m = 0; cin >> t; for (int i = 0; i < t; i++) { string nmi, pi; cin >> nmi >> pi; msi::iterator mit = nmmap.find(nmi); int id; bool newst = false; if (mit == nmmap.end()) { id = nmmap[nmi] = m++; nms[id] = nmi; ss[id] = Stat(id, 0, 0); newst = true; } else id = mit->second; Stat &ssi = ss[id]; if (pi[0] == '?') { int k = trp.lower_bound(ssi); printf("%d\n", k + 1); } else { if (! newst) trp.remove(ssi); int li = pi[0] - 'A'; scs[id][li] = 50 * ls[li] + (50 * ls[li]) * 5 / (4 + ++acs[li]); ssi.sum += scs[id][li]; ssi.t = i; trp.insert(ssi); } } return 0; }