原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2648
纯暴力的方法T_T。。。
如下:
1 #include<cstdio> 2 #include<cstdlib> 3 #include<string> 4 #include<iostream> 5 #include<algorithm> 6 typedef char State[35]; 7 char *target = "memory"; 8 const int Max_N = 11000; 9 struct Node{ 10 State name; 11 int v, s; 12 Node *ch[2]; 13 inline void 14 set(int _v = 0, char *src = "", int _s = 0,Node *p = NULL){ 15 ch[0] = ch[1] = p; 16 v = _v, s = _s, strcpy(name, src); 17 } 18 inline void push_up(){ 19 s = ch[0]->s + ch[1]->s + 1; 20 } 21 inline int cmp(char *src) const{ 22 if (0 == strcmp(src, name)) return -1; 23 else if (-1 == strcmp(src, name)) return 0; 24 return 1; 25 } 26 }; 27 struct SizeBalanceTree{ 28 Node *tail, *null, *root; 29 Node stack[Max_N]; 30 void init(){ 31 tail = &stack[0]; 32 null = tail++; 33 null->set(); 34 root = null; 35 } 36 inline Node *newNode(char *name, int v){ 37 Node *p = tail++; 38 p->set(v, name, 1, null); 39 return p; 40 } 41 inline void rotate(Node* &x, int d){ 42 Node *k = x->ch[!d]; 43 x->ch[!d] = k->ch[d]; 44 k->ch[d] = x; 45 k->s = x->s; 46 x->push_up(); 47 x = k; 48 } 49 inline void Maintain(Node* &x, int d){ 50 if (x->ch[d] == null) return; 51 if (x->ch[d]->ch[d]->s > x->ch[!d]->s){ 52 rotate(x, !d); 53 } else if (x->ch[d]->ch[!d]->s > x->ch[!d]->s){ 54 rotate(x->ch[d], d), rotate(x, !d); 55 } else { 56 return; 57 } 58 Maintain(x, 0), Maintain(x, 1); 59 } 60 inline void insert(Node* &x, char *name, int v){ 61 if (x == null){ 62 x = newNode(name, v); 63 return; 64 } else { 65 x->s++; 66 int d = x->cmp(name); 67 insert(x->ch[d], name, v); 68 x->push_up(); 69 Maintain(x, d); 70 } 71 } 72 inline Node *Modify(Node *x, char *name){ 73 if (x == null) return null; 74 int d = x->cmp(name); 75 if (-1 == d) return x; 76 else return Modify(x->ch[d], name); 77 } 78 inline void insert(char *str, int v = 0){ 79 insert(root, str, v); 80 return; 81 } 82 inline void Modify(char *str, int v){ 83 Node* ret = Modify(root, str); 84 ret->v += v; 85 } 86 inline void dfs(Node *x, int v, int &ans){ 87 if (x != null){ 88 dfs(x->ch[0], v, ans); 89 if (x->v > v) ans++; 90 dfs(x->ch[1], v, ans); 91 } 92 } 93 inline void query(){ 94 int cnt = 0, ans = 0; 95 int v = Modify(root, target)->v; 96 dfs(root, v, ans); 97 printf("%d\n", ans + 1); 98 } 99 }sbt; 100 int main(){ 101 #ifdef LOCAL 102 freopen("in.txt", "r", stdin); 103 freopen("out.txt", "w+", stdout); 104 #endif 105 char buf[100]; 106 int n, m, val; 107 while (~scanf("%d", &n)){ 108 sbt.init(); 109 for (int i = 0; i < n; i++){ 110 scanf("%s", buf); 111 sbt.insert(buf); 112 } 113 scanf("%d", &m); 114 while (m--){ 115 for (int i = 0; i < n; i++){ 116 scanf("%d %s", &val, buf); 117 sbt.Modify(buf, val); 118 } 119 sbt.query(); 120 } 121 } 122 return 0; 123 }