[NOI2004]郁闷的出纳员
2016-05-03 20:15
351 查看
BZOJ1503
因为这道题是对所有人的工资进行修改,所以用一个“基准值”,每次只需要修改基准值就可以了。
实际工资 = 相对工资 + 基准值
剩下的就是treap
因为这道题是对所有人的工资进行修改,所以用一个“基准值”,每次只需要修改基准值就可以了。
实际工资 = 相对工资 + 基准值
剩下的就是treap
#include<cstdio> #include<iostream> using namespace std; int delta, n, m, leave; struct Treap{ struct Node{ int v, r, s; Node* ch[2]; int cmp(int x) const{ if(x == v) return -1; else return x < v ? 0 : 1; } void maintain(){ s = ch[0]->s + ch[1]->s +1; } }; Node *root, *null; Treap(){ null = new Node(); root = null; } void rotate(Node* &o, int d){ Node* k = o->ch[d^1]; o->ch[d^1] = k->ch[d]; k->ch[d] = o; o->maintain(); k->maintain(); o = k; } void insert(Node* &o, int x){ if(o == null){ o = new Node(); o->ch[0] = o->ch[1] = null; o->v = x; o->r = rand(); o->s = 1; } else{ int d = (x < o->v ? 0 : 1); insert(o->ch[d], x); if(o->ch[d]->r > o->r) rotate(o, d^1); } o->maintain(); } int del(Node* &o, int x){ if(o == null) return 0; if(o->v < x){ int t = o->ch[0]->s + 1; o = o->ch[1]; return t + del(o, x); } else{ int t = del(o->ch[0], x); o->s -= t; return t; } } int find(Node* o, int k){ if(o == null || k <= 0 || k > o->s) return 0; int s = (o->ch[1] == null ? 0 : o->ch[1]->s); if(s + 1 == k) return o->v; if(s >= k) return find(o->ch[1], k); return find(o->ch[0], k-s-1); } } tp; int main(){ //freopen("p1503.in", "r", stdin); scanf("%d %d\n", &n, &m); char p[10]; int x; tp = Treap(); while(n--){ scanf("%s%d\n", p, &x); if(p[0] == 'I') if(x >= m) tp.insert(tp.root, x-delta); if(p[0] == 'A') delta += x; if(p[0] == 'S'){ delta -= x; leave += tp.del(tp.root, m-delta); } if(p[0] == 'F'){ if(x > tp.root->s) printf("-1\n"); else printf("%d\n", tp.find(tp.root, x)+delta); } } printf("%d\n", leave); return 0; }
相关文章推荐
- mongodb 常用操作
- 关于Ubuntu14.04 中文字体乱码问题
- 10.数值的整数次方
- 驱动开发-字符设备2
- [leetcode]Longest Increasing Subsequence
- wait和waitpid函数
- 如何划分子网
- CentOS 7 安装 Oracle 11.2.0.4
- 从iPhone下滑 看科技转型之困
- fmt标签的格式化日期数字
- 线程池续
- Idea erlang 引入 .hrl文件中的宏,idea 显示Unresolved
- recyclerView导入问题
- Redis+Spring缓存实例(windows环境,附实例源码及详解)
- TF-IDF介绍及应用
- 导入viewPagerIndicator时报错提示FloatMath.ceil()不存在解决方法
- SDAU 课程练习3 1023
- LeetCode.345. Reverse Vowels of a String
- 【HUSTOJ】1020: 平方和
- openflow协议规范1.1.0翻译