二次联通门 : BZOJ 1503: [NOI2004]郁闷的出纳员

  1. /*
  2. BZOJ 1503: [NOI2004]郁闷的出纳员
  3.  
  4. 考虑这样一个事实
  5. 无论是加或减
  6. 都是针对全体人员的
  7.  
  8. 那么只需要记录一个总标记
  9.  
  10. 直接记录对于工资的加或减
  11.  
  12. 每次查询最低要求工资与标记的差的前驱,并旋到根处
  13.  
  14. 那么根节点的所有左子树都是不符合要求要走人的
  15. 每次记录一下搞一搞, 就不用考虑splay麻烦的标记下放问题了

  16.   但是却没调出来。。。
  17. */
  18. #include <cstdio>
  19.  
  20. void read (int &now)
  21. {
  22. register char word = getchar ();
  23. bool temp = false;
  24. for (now = ; word < '' || word > ''; word = getchar ())
  25. if (word == '-')
  26. temp = true;
  27. for (; word >= '' && word <= ''; now = now * + word - '', word = getchar ());
  28. if (temp)
  29. now = -now;
  30. }
  31.  
  32. int Min_N;
  33.  
  34. struct S_D
  35. {
  36. S_D *child[], *father;
  37.  
  38. int key;
  39. int weigth, size;
  40.  
  41. inline void Updata ()
  42. {
  43. this->size = this->weigth;
  44. if (this->child[])
  45. this->size += this->child[]->size;
  46. if (this->child[])
  47. this->size += this->child[]->size;
  48. }
  49.  
  50. S_D ()
  51. {
  52. child[] = child[] = NULL;
  53. size = weigth = ;
  54. key = ;
  55. father = NULL;
  56. }
  57.  
  58. inline int Get_Pos ()
  59. {
  60. return this->father->child[] == this;
  61. }
  62.  
  63. inline void Clear ()
  64. {
  65. this->child[] = this->child[] = father = NULL;
  66. }
  67. };
  68.  
  69. int Answer;
  70. class Splay_Tree_Type
  71. {
  72.  
  73. private :
  74.  
  75. S_D *Root;
  76.  
  77. inline void Rotate (S_D *now)
  78. {
  79. S_D *Father = now->father;
  80. register int pos = now->Get_Pos () ^ ;
  81. Father->child[pos ^ ] = now->child[pos];
  82. if (now->child[pos])
  83. now->child[pos]->father = Father;
  84. if ((now->father = Father->father) != NULL)
  85. now->father->child[now->father->child[] == Father] = now;
  86.  
  87. Father->father = now;
  88. now->child[pos] = Father;
  89.  
  90. Father->Updata ();
  91. now->Updata ();
  92.  
  93. if (now->father == NULL)
  94. Root = now;
  95. }
  96.  
  97. inline void Splay (S_D *now)
  98. {
  99. for (S_D *Father; Father = now->father; this->Rotate (now))
  100. if (Father->father != NULL)
  101. this->Rotate (now->Get_Pos () == Father->Get_Pos () ? Father : now);
  102. }
  103.  
  104. S_D* Find (int x)
  105. {
  106. S_D *now = Root;
  107. for (; (now != NULL && now->key != x); now = now->child[x > now->key]);
  108. if (now == NULL)
  109. return now;
  110. Splay (now);
  111. return now;
  112. }
  113.  
  114. void Delete_point (int x)
  115. {
  116. S_D *now = Find (x);
  117. if (now->weigth > )
  118. {
  119. now->weigth --;
  120. return ;
  121. }
  122. if (now->child[] == NULL && now->child[] == NULL)
  123. {
  124. now->Clear ();
  125. Root = NULL;
  126. return ;
  127. }
  128. S_D *res;
  129. if (now->child[] == NULL)
  130. {
  131. Root = now->child[];
  132. now->child[]->father = NULL;
  133. now->Clear ();
  134. return ;
  135. }
  136. if (now->child[] == NULL)
  137. {
  138. Root = now->child[];
  139. now->child[]->father = NULL;
  140. now->Clear ();
  141. return ;
  142. }
  143. S_D *Prefix = Find_Prefix (x);
  144. res = Root;
  145. Prefix->child[] = res->child[];
  146. res->child[]->father = Root;
  147. res->Clear ();
  148. Root->Updata ();
  149. }
  150.  
  151. void Delete_tree (int x)
  152. {
  153. S_D *now = Find_Suffix (x);
  154. if (now == NULL)
  155. return ;
  156. Answer += now->child[]->size;
  157. now->child[]->father = NULL;
  158. now->child[] = NULL;
  159. now->Updata ();
  160. }
  161.  
  162. S_D *Find_Suffix (int x)
  163. {
  164. this->Insert (x);
  165. S_D *now = Root;
  166. if (now->child[] == NULL)
  167. {
  168. this->Delete_point (x);
  169. return NULL;
  170. }
  171. for (now = now->child[]; now->child[]; now = now->child[]);
  172. this->Delete_point (x);
  173. if (now == NULL)
  174. return now;
  175. this->Splay (now);
  176. return now;
  177. }
  178.  
  179. S_D *Find_Prefix (int x)
  180. {
  181. this->Insert (x);
  182. S_D *now = Root;
  183. if (now->child[] == NULL)
  184. {
  185. this->Delete_point (x);
  186. return NULL;
  187. }
  188. for (now = now->child[]; now->child[]; now = now->child[]);
  189. this->Delete_point (x);
  190. if (now == NULL)
  191. return now;
  192. this->Splay (now);
  193. return now;
  194. }
  195.  
  196. public :
  197.  
  198. int Flandre_Scarlet;
  199.  
  200. void Insert (int x)
  201. {
  202. if (Root == NULL)
  203. {
  204. Root = new S_D ;
  205. Root->key = x;
  206. return ;
  207. }
  208. S_D *now = Root, *Father;
  209. for (; ;Father = now, now = now->child[x > now->key])
  210. {
  211. if (now == NULL)
  212. {
  213. now = new S_D;
  214. now->key = x;
  215. now->father = Father;
  216. Father->child[x > Father->key] = now;
  217. this->Splay (now);
  218. return ;
  219. }
  220. if (x == now->key)
  221. {
  222. now->weigth ++;
  223. this->Splay (now);
  224. return ;
  225. }
  226. }
  227. }
  228.  
  229. void Cut (int x)
  230. {
  231. int Need = Min_N - Flandre_Scarlet;
  232. S_D *now = this->Find_Prefix (Need);
  233. if (now != NULL)
  234. this->Delete_tree (now->key);
  235. }
  236.  
  237. int Query (int x)
  238. {
  239. S_D *now = Root;
  240. for (register int res = ; ; )
  241. {
  242. if (now->child[] && x < now->child[]->size)
  243. now = now->child[];
  244. res += (now->child[] ? now->child[]->size : ) + now->weigth;
  245. if (x <= res)
  246. return now->key;
  247. x -= res;
  248. now = now->child[];
  249. }
  250. }
  251.  
  252. void Change (int x)
  253. {
  254. Flandre_Scarlet += x;
  255. return ;
  256. }
  257.  
  258. int Query_Size ()
  259. {
  260. return Root->size;
  261. }
  262. };
  263.  
  264. Splay_Tree_Type Splay;
  265.  
  266. int main (int argc, char *argv[])
  267. {
  268. freopen ("cashier.in", "r", stdin);
  269. freopen ("cashier.out", "w", stdout);
  270. int N;
  271. read (N);
  272. read (Min_N);
  273. char type[];
  274. register int Size;
  275. for (int x; N --; )
  276. {
  277. scanf ("%s", type);
  278. read (x);
  279. if (type[] == 'I')
  280. Splay.Insert (x);
  281. else if (type[] == 'S')
  282. Splay.Change (-x);
  283. else if (type[] == 'A')
  284. Splay.Change (x);
  285. else
  286. {
  287. Splay.Cut (x);
  288. Size = Splay.Query_Size ();
  289. if (x > Size)
  290. puts ("-1");
  291. else
  292. printf ("%d\n", Splay.Query (Size - x) + Splay.Flandre_Scarlet);
  293. }
  294. }
  295. printf ("%d", Answer);
  296. return ;
  297. }
  298.  
  299. /*
  300. 20 0
  301. I 4
  302. F 1
  303. I 6
  304. S 9
  305. F 14
  306. I 6
  307. I 7
  308. A 8
  309. I 3
  310. F 2
  311. I 9
  312. I 6
  313. I 6
  314. S 3
  315. S 5
  316. I 6
  317. F 1
  318. I 3
  319. A 2
  320. F 5
  321. */
  322.  
  323. /*
  324. 4
  325. -1
  326. 14
  327. 7
  328. 3
  329. 5
  330. */

(WA)BZOJ 1503: [NOI2004]郁闷的出纳员的更多相关文章

  1. BZOJ 1503: [NOI2004]郁闷的出纳员

    1503: [NOI2004]郁闷的出纳员 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 10526  Solved: 3685[Submit][Stat ...

  2. BZOJ 1503: [NOI2004]郁闷的出纳员 splay

    1503: [NOI2004]郁闷的出纳员 Description OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作 ...

  3. bzoj 1503: [NOI2004]郁闷的出纳员 Treap

    1503: [NOI2004]郁闷的出纳员 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 6263  Solved: 2190[Submit][Statu ...

  4. bzoj 1503: [NOI2004]郁闷的出纳员 -- 权值线段树

    1503: [NOI2004]郁闷的出纳员 Time Limit: 5 Sec  Memory Limit: 64 MB Description OIER公司是一家大型专业化软件公司,有着数以万计的员 ...

  5. 1503: [NOI2004]郁闷的出纳员 (SBT)

    1503: [NOI2004]郁闷的出纳员 http://www.lydsy.com/JudgeOnline/problem.php?id=1503 Time Limit: 5 Sec  Memory ...

  6. bzoj 3224 NOI2004郁闷的出纳员

    NOI2004郁闷的出纳员 2013年12月26日6,1818 输入描述 Input Description 第一行有两个非负整数n和min.n表示下面有多少条命令,min表示工资下界. 接下来的n行 ...

  7. 【BZOJ】1503: [NOI2004]郁闷的出纳员(Splay)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1503 这题没有看题解就1a了-好开心,, 其实后面去看题解发现他们的都很麻烦,其实有种很简单的做法: ...

  8. 1503. [NOI2004]郁闷的出纳员【平衡树-splay】

    Description OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的 工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经 ...

  9. 1503: [NOI2004]郁闷的出纳员

    Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 13723  Solved: 4989[Submit][Status][Discuss] Descripti ...

随机推荐

  1. Activate注解

    Activate注解 被该注解修饰的接口,扩展类可能会被加载 ProtocolFilterWrapper.buildInvokerChain @Documented @Retention(Retent ...

  2. Linux下/etc/login.defs文件

    /etc/login.defs文件定义了与/etc/password和/etc/shadow配套的用户限制设定.这个文件是需要的,缺失并不会影响系统的使用,但是也许会产生意想不到的错误. 如果/etc ...

  3. 音视频入门-02-RGB拼图

    * 音视频入门文章目录 * 图片 & 像素点 & RGB 平时浏览的图片看不出像素点: 图片放大时,可以看出图片是一个个像素点组成的: 每个像素点的颜色可以用 RGB 表示: RGB ...

  4. codefroce 854 A.Fraction

    题解:贪心,每次从能够出发的飞机中取一个最大的就好啦,用一个队列维护一下~ ac代码: #include <cstdio> #include <iostream> #inclu ...

  5. (十四)角色管理(Ztree插件的基本使用)

    1. 建表 角色表 菜单表 角色-菜单(这个表中的role_id和menuu_id都不能被设置为主键,否则当插入一个新角色的时候,一个角色可能拥有多个菜单(role_id重复),一个菜单可能被多个角色 ...

  6. 上传文件-layui+ashx

    public void ProcessRequest (HttpContext context) { if (true) { context.Response.ContentType = " ...

  7. .net Core CLR

    .net Core CLR是开源的.大部分文件是C++写成.这样他就可以编译后再不同的平台运行. https://github.com/dotnet/coreclr

  8. Flutter 自定义绘制 View

    在 Flutter 中自定义 View 有两种方式: 组合已有控件 自定义绘制 如何自定义绘制 有两个类做这件事情: CustomPaint :会在绘制阶段提供一个 Canvas 画布 CustomP ...

  9. [LeetCode] 22. 括号生成 ☆☆☆(回溯)

    描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结果为: [ "((()))", "(()( ...

  10. element-ui Drawer抽屉组件封装

    <template> <div class="com"> <el-drawer title="我是标题" :visible.syn ...