记录编号 192407 评测结果 AAAAAAAAAA
题目名称 [HAOI 2008]排名系统 最终得分 100
用户昵称 Gravataryyy 是否通过 通过
代码语言 C++ 运行时间 2.476 s
提交时间 2015-10-10 20:55:35 内存使用 38.46 MiB
显示代码纯文本
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <string>
  7. #include <map>
  8. #include <time.h>
  9. using std::string;
  10. typedef long long Long;
  11. std::map<string,int> node;
  12. namespace output
  13. {
  14. struct node
  15. {
  16. string s;
  17. int n;
  18. Long val;
  19. node(string ss,int nn,Long vval)
  20. {
  21. s = ss;
  22. n = nn;
  23. val = vval;
  24. }
  25. node()
  26. {}
  27. bool operator<(const node & b)const
  28. {
  29. if(val == b.val)
  30. return n < b.n;
  31. return val < b.val;
  32. }
  33. };
  34. std::vector<node> vec;
  35. void output()
  36. {
  37. std::sort(vec.begin(),vec.end());
  38. for(int i = 0;i < vec.size();i++)
  39. {
  40. if(i == 0)
  41. printf("%s",vec[i].s.c_str());
  42. else printf(" %s",vec[i].s.c_str());
  43. }
  44. printf("\n");
  45. }
  46. }
  47.  
  48. const int N = 500000;
  49. namespace splay
  50. {
  51. struct Int
  52. {
  53. Long val;
  54. int time;
  55. bool operator < (const Int & b)const
  56. {
  57. if(val == b.val)
  58. return time < b.time;
  59. return val < b.val;
  60. }
  61. bool operator > (const Int & b)const
  62. {
  63. if(val == b.val)
  64. return time > b.time;
  65. return val > b.val;
  66. }
  67. bool operator == (const Int & b)const
  68. {
  69. return val == b.val && time == b.time;
  70. }
  71. bool operator != (const Int & b)const
  72. {
  73. return val != b.val || time != b.time;
  74. }
  75. Int(Long a,int b)
  76. {
  77. val = a;
  78. time = b;
  79. }
  80. Int()
  81. {}
  82. };
  83. struct node
  84. {
  85. string name;
  86. Int val;
  87. int son[2];
  88. int size;
  89. int is_right;
  90. int father;
  91. bool lazy;
  92. };
  93. int tot = 0;
  94. node nodes[2 * N + 100];
  95. #define LC(a) nodes[a].son[0]
  96. #define RC(a) nodes[a].son[1]
  97. #define SIZE(a) nodes[a].size
  98. #define VAL(a) nodes[a].val
  99. #define TIME(a) hash::time_in[nodes[a].name]
  100. #define FATHER_ME(a) nodes[nodes[a].father].son[nodes[a].is_right]
  101. int root = 0;
  102. string now_name;
  103. int newnode(Long val,int ti)
  104. {
  105. tot++;
  106. nodes[tot].name = now_name;
  107. nodes[tot].val = Int(val,ti);
  108. nodes[tot].size = 1;
  109. return tot;
  110. }
  111. void updata(int d)
  112. {
  113. if(!d)
  114. return;
  115. if(nodes[d].lazy)
  116. SIZE(d) = SIZE(LC(d)) + SIZE(RC(d));
  117. else SIZE(d) = SIZE(LC(d)) + SIZE(RC(d)) + 1;
  118. }
  119. void add(int & d,Long val,int is_right,const int & ti,const int & fat = 0)
  120. {
  121. if(!d)
  122. {
  123. d = newnode(val,ti);
  124. nodes[d].is_right = is_right;
  125. nodes[d].father = fat;
  126. return;
  127. }
  128. if(Int(val,ti) > VAL(d))
  129. add(RC(d),val,1,ti,d);
  130. else add(LC(d),val,0,ti,d);
  131. updata(d);
  132. }
  133. void r_r(int f)
  134. {
  135. if(!f)
  136. return;
  137. int s = nodes[f].son[0];
  138. int b = nodes[s].son[1];
  139. if(f == root)
  140. root = s;
  141. else FATHER_ME(f) = s;
  142. nodes[s].is_right = nodes[f].is_right;
  143. nodes[s].father = nodes[f].father;
  144. nodes[f].father = s;
  145. nodes[f].is_right = 1;
  146. nodes[s].son[1] = f;
  147. nodes[b].father = f;
  148. nodes[b].is_right = 0;
  149. nodes[f].son[0] = b;
  150. updata(f);
  151. updata(s);
  152. }
  153. void l_r(int f)
  154. {
  155. if(!f)
  156. return;
  157. int s = nodes[f].son[1];
  158. int b = nodes[s].son[0];
  159. if(f == root)
  160. root = s;
  161. else FATHER_ME(f) = s;
  162. nodes[s].is_right = nodes[f].is_right;
  163. nodes[s].father = nodes[f].father;
  164. nodes[f].father = s;
  165. nodes[f].is_right = 0;
  166. nodes[s].son[0] = f;
  167. nodes[b].father = f;
  168. nodes[b].is_right = 1;
  169. nodes[f].son[1] = b;
  170. updata(f);
  171. updata(s);
  172. }
  173. void del(int d)
  174. {
  175. if(!d)
  176. return;
  177. nodes[d].lazy = true;
  178. updata(d);
  179. int f = nodes[d].father;
  180. while(f)
  181. {
  182. updata(f);
  183. f = nodes[f].father;
  184. }
  185. }
  186. int find_kth(int d,int k,int f = 0)
  187. {
  188. if(!d)
  189. return 0;
  190. if(nodes[d].lazy)
  191. {
  192. if(k <= SIZE(LC(d)))
  193. return find_kth(LC(d),k);
  194. return find_kth(RC(d),k - SIZE(LC(d)),d);
  195. }
  196. if(SIZE(LC(d)) == k - 1)
  197. return d;
  198. if(k <= SIZE(LC(d)))
  199. return find_kth(LC(d),k);
  200. return find_kth(RC(d),k - SIZE(LC(d)) - 1);
  201. }
  202. int find_num(int d,Long val,int ti)
  203. {
  204. if(!d)
  205. return 0;
  206. int sz = 0;
  207. Int v = Int(val,ti);
  208. while(nodes[d].val != v)
  209. {
  210. if(v > nodes[d].val)
  211. {
  212. sz += SIZE(LC(d)) + 1;
  213. if(nodes[d].lazy)
  214. sz--;
  215. d = RC(d);
  216. }
  217. else d = LC(d);
  218. }
  219. sz += SIZE(LC(d)) + 1;
  220. if(nodes[d].lazy)
  221. sz--;
  222. return sz;
  223. }
  224. void rot(int a)
  225. {
  226. if(!a)
  227. return;
  228. if(nodes[a].is_right)
  229. l_r(nodes[a].father);
  230. else r_r(nodes[a].father);
  231. }
  232. void splay(int a)
  233. {
  234. if(!a)
  235. return;
  236. while(a != root)
  237. {
  238. if(nodes[a].is_right != nodes[nodes[a].father].is_right)
  239. {
  240. rot(a);
  241. rot(a);
  242. }
  243. else
  244. {
  245. rot(nodes[a].father);
  246. rot(a);
  247. }
  248. }
  249. }
  250. };
  251. char str[20];
  252. int main()
  253. {
  254. {
  255. int _q=10<<20;
  256. char *_p=(char*)malloc(_q)+_q;
  257. __asm__("movl %0, %%esp\n"::"r"(_p));
  258. }
  259. freopen("rank.in","r",stdin);
  260. freopen("rank.out","w",stdout);
  261. int n = 0;
  262. scanf("%d",&n);
  263. char c = 0;
  264. int op = 0;
  265. int num = 0;
  266. int re = 0;
  267. int m = 0;
  268. Long Lnum = 0LL;
  269. int tt = 0;
  270. string nm;
  271. for(int i = 0;i < n;i++)
  272. {
  273. do
  274. {
  275. c = getchar();
  276. }
  277. while(c == ' ' || c == '\n' || c == '\t');
  278. if(c == '?')
  279. {
  280. c = getchar();
  281. if(c >= '0' && c <= '9')
  282. op = 1;//给定排名问名字
  283. else op = 2;//给定名字问排名
  284. ungetc(c,stdin);
  285. }
  286. else op = 3;//改分数
  287. if(op == 1)
  288. {
  289. scanf("%d",&num);
  290. //continue;
  291. output::vec.clear();
  292. re = 0;
  293. for(int p = num;p < num + 10;p++)
  294. {
  295. if(p > splay::nodes[splay::root].size)
  296. break;
  297. if(re == 0)
  298. re = splay::find_kth(splay::root,p);
  299. else re = splay::find_kth(splay::nodes[re].son[1],1);
  300. splay::splay(re);
  301. output::vec.push_back(output::node(splay::nodes[re].name,splay::nodes[re].val.time,splay::nodes[re].val.val));
  302. }
  303. output::output();
  304. }
  305. else if(op == 2)
  306. {
  307. scanf("%s",str);
  308. //continue;
  309. int hh = node[string(str)];
  310. splay::splay(hh);
  311. Lnum = splay::nodes[hh].val.val;
  312. tt = splay::nodes[hh].val.time;
  313. printf("%d\n",splay::find_num(splay::root,Lnum,tt));
  314. }
  315. else
  316. {
  317. scanf("%s",str);
  318. std::cin>>Lnum;
  319. Lnum = -Lnum;
  320. nm = str;
  321. m = node[nm];
  322. splay::del(m);
  323. if(m)
  324. splay::splay(m);
  325. splay::now_name = nm;
  326. splay::add(splay::root,Lnum,0,i + 1);
  327. node[nm] = splay::tot;
  328. splay::splay(splay::tot);
  329. }
  330. //if(i % 100000 == 0)
  331. // fprintf(stderr,"%d %d %d\n",i,clock(),op);
  332. }
  333. return 0;
  334. }