HDU - 5096 ACM Rank (Treap)
平衡树的题,Treap破之,比较难搞的出现相同题数罚时的情况,解决方法是在每个结点用一个set,
保证结点值的时候可以把题数和罚时保存到一个int里,令v = n*MaxPenaltySum-penalty。这样就可以很方便地做比较了。
初始化有一定技巧。细节很多容易出错。 写静态版,如果有合并操作要内存池开到最大结点数量的两倍,内存多随意
写错一个变量名字查好久,尴尬
hdu似乎不资磁PB_ds这种黑科技
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
using namespace std; #define PB push_back
#define MP make_pair
#define fi first
#define se second #define cer(x) cout<<#x<<'='<<endl
typedef long long ll; const int maxn = 1e4+, MAXPEN = 1e6;//60*20+3000+666; int st[maxn], val[maxn], pen[maxn][],lst[maxn],lsa[maxn]; struct CMP
{
bool operator()(int a,int b) const{
return lsa[a] < lsa[b];
}
}; struct Node
{
int ch[],r,s,v;
set<int,CMP> vs;
}nds[maxn*];
queue<int> fre; inline int newNode(int x)
{
int i;
Node &u = nds[i = fre.front()]; fre.pop();
u.ch[] = u.ch[] = ;
u.r = rand(); u.s = ; u.v = val[x];
u.vs.clear(); u.vs.insert(x);
return i;
} inline void delt(int x)
{
fre.push(x);
} inline int cmp(int a,int b)
{
if(a == b) return -;
return a>b? :;
} inline void mt(int i)
{
Node&u = nds[i];
u.s = (int)u.vs.size() + nds[u.ch[]].s + nds[u.ch[]].s;
} inline void rot(int &o,int d)
{
int k = nds[o].ch[d^];
nds[o].ch[d^] = nds[k].ch[d];
nds[k].ch[d] = o;
mt(o); mt(k);
o = k;
} inline void initTreap(int n)
{
for(int i = ; i < n; i++) fre.push(i);
nds[].s = ;
} void inst(int &o,int x)
{
if(!o) {
o = newNode(x); return;
}
Node &u = nds[o];
int d = cmp(u.v,val[x]);
if(~d){
inst(u.ch[d],x);
if(u.r < nds[u.ch[d]].r) rot(o,d^);
//else
}else {
u.s++; u.vs.insert(x);
}
mt(o);
} void rmov(int &o,int x)
{
Node &u = nds[o];
int d = cmp(u.v,val[x]);
if(~d){
rmov(u.ch[d],x);
}else {
if((int)u.vs.size() > ){
u.vs.erase(x); u.s--;
}else {
if(u.ch[] && u.ch[]){
int d2 = nds[u.ch[]].r > nds[u.ch[]].r ? :;
rot(o,d2); rmov(nds[o].ch[d2],x);
}else {
delt(o);
if(!u.ch[]) o = u.ch[];
else o = u.ch[];
}
}
}
if(o) mt(o);
} int Rank(int o,int x)
{
Node &u = nds[o];
int d = cmp(u.v,val[x]);
if(d == ) return Rank(u.ch[],x);
int s = nds[u.ch[]].s;
if(d == ) return s+(int)u.vs.size()+Rank(u.ch[],x);
return s+;
} int Kth(int o,int k)
{
if(!o || k <= || k > nds[o].s) return -;//
Node &u = nds[o];
int s = nds[u.ch[]].s;
if(k == s+) return *(u.vs.begin());
if(k <= s) return Kth(u.ch[],k);
return Kth(u.ch[],k-s-(int)u.vs.size());
} void rmvTree(int o)
{
if(nds[o].ch[]) rmvTree(nds[o].ch[]);
if(nds[o].ch[]) rmvTree(nds[o].ch[]);
delt(o);
} //#define LOCAL
int main()
{
#ifdef LOCAL
freopen("data.txt","r",stdin);
#endif
int N,M;
char op[];
initTreap(maxn*);
int root = ;
while(~scanf("%d%d",&N,&M)){
if(root) rmvTree(root);
root = ;
fill(st,st+N,);
fill(val,val+N,);
fill(lst,lst+N,-);
memset(pen,,sizeof(pen));
for(int i = ; i < N; i++){
lsa[i] = -N+i;
inst(root,i);
}
int c = ;
while(~scanf("%s",op)&&*op != 'C'){
if(*op == 'S'){
int t,x,re; char pro; scanf("%d:%d:%c:%d",&t,&x,&pro,&re);
int p = pro-'A';
if(t - lst[x] < || (st[x]&(<<p))) continue;
lst[x] = t;
if(re == ){
printf("[%d][%c]\n",x,pro);
rmov(root,x);
lsa[x] = ++c;
st[x] |= <<p;
val[x] += MAXPEN-pen[x][p]-t;
inst(root,x);
}else pen[x][p] += ;
}else if(*op == 'R'){
int x; scanf("%d",&x);
printf("%d\n",Rank(root,x));
}else if(*op == 'T'){
int k; scanf("%d",&k);
printf("%d\n",Kth(root,k));
}
}
scanf("%s",op);
puts("");
}
return ;
}
HDU - 5096 ACM Rank (Treap)的更多相关文章
- hdu 1540 Tunnel Warfare(Treap)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1540 思路:三种操作: D摧毁一个点 R重建最晚被修改的那个点 Q询问点x联通的点有多少个 逆向思维,D操 ...
- HDU 5097 Page Rank (模拟)
题目背景是以前用来对网页进行排名的Page Rank算法,是早期Google的革命性发明. 背后的原理是矩阵和图论.这个数学模型是由Google的创始人拉里·佩奇和谢尔盖·布林发现的. 如果一个网页被 ...
- HDU 5938 Four Operations(四则运算)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- HDU 5775 Bubble Sort(冒泡排序)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- HDU 1711 Number Sequence(数列)
HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- HDU 1005 Number Sequence(数列)
HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- HDU 1176 免费馅饼 (动态规划)
HDU 1176 免费馅饼 (动态规划) Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼 ...
- 2021.12.07 P4291 [HAOI2008]排名系统(Treap)
2021.12.07 P4291 [HAOI2008]排名系统(Treap) https://www.luogu.com.cn/problem/P4291 双倍经验: https://www.luog ...
- HDU 4069 Squiggly Sudoku(DLX)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4069 Problem Description Today we play a squiggly sud ...
随机推荐
- 多进程小例子--fork+pipe
1 #include<stdio.h> 2 #include<unistd.h> 3 4 #define m 6 5 int main() 6 { 7 ...
- FZU - 2109 Mountain Number 数位dp
Mountain Number One integer number x is called "Mountain Number" if: (1) x>0 and x is a ...
- 阿里云ECS 自己搭建 hyperledger fabric的错误
常常有在本地搭建没问题,到阿里云上跑的时候 fabric启动不成功的问题. 引用: https://yq.aliyun.com/articles/238940 解决方案 1.在e2e_cli 下有个d ...
- Linux常用命令(补充)-grep
grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正 ...
- Node.js 第三方包的安装、升级、卸载,以及包依赖管理
本地安装: npm install package-name 全局全装: npm install -g package-name 举个栗子 全局安装 React项目的脚手架 npm install ...
- Codeforces Round #527 -A. Uniform String(思维)
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- Floyd判圈法
---恢复内容开始--- http://blog.csdn.net/ruoruo_cheng/article/details/53100656 ---恢复内容结束---
- jQuery基础(3)
摘要:jQuery的位置信息,JS的事件流的概念(重点),事件对象,jQuery的事件绑定和解绑,时间委托(时间代理) 一.jQuery的位置信息 jQuery的位置信息跟JS的client系列.of ...
- js:常用到的js操作记录
1:对参数去除空格 str.replace(/^\s+|\s+$/g, '');
- 事务&数据库连接池&DBUtils
事务的特性 原子性 指的是 事务中包含的逻辑,不可分割. 一致性 指的是 事务执行前后.数据完整性 隔离性 指的是 事务在执行期间不应该受到其他事务的影响 持久性 指的是 事务执行成功,那么数据应该持 ...