原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2648
纯暴力的方法T_T。。。
如下:
#include<cstdio>
#include<cstdlib>
#include<string>
#include<iostream>
#include<algorithm>
typedef char State[];
char *target = "memory";
const int Max_N = ;
struct Node{
State name;
int v, s;
Node *ch[];
inline void
set(int _v = , char *src = "", int _s = ,Node *p = NULL){
ch[] = ch[] = p;
v = _v, s = _s, strcpy(name, src);
}
inline void push_up(){
s = ch[]->s + ch[]->s + ;
}
inline int cmp(char *src) const{
if ( == strcmp(src, name)) return -;
else if (- == strcmp(src, name)) return ;
return ;
}
};
struct SizeBalanceTree{
Node *tail, *null, *root;
Node stack[Max_N];
void init(){
tail = &stack[];
null = tail++;
null->set();
root = null;
}
inline Node *newNode(char *name, int v){
Node *p = tail++;
p->set(v, name, , null);
return p;
}
inline void rotate(Node* &x, int d){
Node *k = x->ch[!d];
x->ch[!d] = k->ch[d];
k->ch[d] = x;
k->s = x->s;
x->push_up();
x = k;
}
inline void Maintain(Node* &x, int d){
if (x->ch[d] == null) return;
if (x->ch[d]->ch[d]->s > x->ch[!d]->s){
rotate(x, !d);
} else if (x->ch[d]->ch[!d]->s > x->ch[!d]->s){
rotate(x->ch[d], d), rotate(x, !d);
} else {
return;
}
Maintain(x, ), Maintain(x, );
}
inline void insert(Node* &x, char *name, int v){
if (x == null){
x = newNode(name, v);
return;
} else {
x->s++;
int d = x->cmp(name);
insert(x->ch[d], name, v);
x->push_up();
Maintain(x, d);
}
}
inline Node *Modify(Node *x, char *name){
if (x == null) return null;
int d = x->cmp(name);
if (- == d) return x;
else return Modify(x->ch[d], name);
}
inline void insert(char *str, int v = ){
insert(root, str, v);
return;
}
inline void Modify(char *str, int v){
Node* ret = Modify(root, str);
ret->v += v;
}
inline void dfs(Node *x, int v, int &ans){
if (x != null){
dfs(x->ch[], v, ans);
if (x->v > v) ans++;
dfs(x->ch[], v, ans);
}
}
inline void query(){
int cnt = , ans = ;
int v = Modify(root, target)->v;
dfs(root, v, ans);
printf("%d\n", ans + );
}
}sbt;
int main(){
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
char buf[];
int n, m, val;
while (~scanf("%d", &n)){
sbt.init();
for (int i = ; i < n; i++){
scanf("%s", buf);
sbt.insert(buf);
}
scanf("%d", &m);
while (m--){
for (int i = ; i < n; i++){
scanf("%d %s", &val, buf);
sbt.Modify(buf, val);
}
sbt.query();
}
}
return ;
}