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 ...
随机推荐
- WPF在ViewModel中绑定按钮点击(CommandBase定义)
定义CommandBase public class CommandBase:ICommand { private readonly Action<object> _commandpara ...
- 点击实现CSS样式切换
如图所示 代码如下图: 特别要注意的是:a标签不会继承上级的color,所以要单独为其设置 参看代码(并非上图代码)如下: <!DOCTYPE html> <html> < ...
- 51nod1117【贪心】
思路:哈夫曼树~~哇塞,那么有道理. 利用堆维护:每次从堆里取两个最小加起来,然后还是最小的两个,最后只剩一根总的 #include <bits/stdc++.h> using names ...
- wcf双工通信
一直以为感觉双工没弄懂,着实觉得很惆怅,在网上了解下双工的一些特点,直接上代码,以便以后项目中用的着: service层: 定义一个IDuplexHello服务接口 [ServiceContract( ...
- 解决sublime text无法安装插件问题
解决sublime text无法安装插件问题最近在sublime text3中使用命令ctrl+shift+p命令安装插件发现不能安装了,一会儿报错 这个错误表示没有可用的安装包,经过一番探索发现是配 ...
- 黑马学习Ajax 概念和基本使用
- 【VueJS】sublime text3支持VueJS语法高亮显示插件vue-syntax-highlight
1. sublime text3支持VueJS语法高亮显示插件vue-syntax-highlightgithub地址: https://github.com/vuejs/vue-syntax-hig ...
- Js 文件上传后缀验证
//img格式验证 function imgFormat(name) { //再对文件名进行截取,以取得后缀名 var namearr= name.split("."); //获取 ...
- 牛客练习赛41E(球的体积并)
球冠公式是\(\frac{\pi h^2(3R-h)}{3}\),这样再余弦公式用\(R_a\)和\(R_b\)导一导两个球冠的\(h\)就做完了.算是补了个camp时没做出来的小坑了. #inclu ...
- 一篇关于完全动态凸包的paper(侵删)
先放原文,挖个坑,到时候再来说人话ε=(´ο`*))) 作者:Franco P. Preparata 出处:Computational geometry An introduction The tec ...