HDU - 5096 ACM Rank (Treap)
2024-09-05 05:33:25
平衡树的题,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 ;
}
最新文章
- 翻译:使用 ASP.NET MVC 4, EF, Knockoutjs and Bootstrap 设计和开发站点 - 5 - 数据库设计
- vue-resource 拦截器使用
- 图说设计模式(UML和设计模式)
- c# 验证码类
- ARC指南2 - ARC的开启和禁止
- Test4J与Jtester单元测试常用注解比较
- 到底UDP和TCP是什么个概念?
- android adt与android sdk有什么关系,他们在开发中各起到什么作用
- ps一般常用的快捷键
- SVM推导
- XSS分析及预防(转)
- 使用myeclipse创建带注解的model实体类
- spring mvc中,直接注入的HttpServletRequst是否安全呢?
- POJ 3279 枚举(思维)
- ApplicationContextAware 接口的作用
- 配置Java文件
- Oracle学习DayFive(PL/SQL)
- 第一册:lesson eighty three.
- English trip EM2-PE-5A Plan a dinner party Teacher:Lamb
- Moco服务器jar包实现简易的API搭建
热门文章
- java发送udp广播包
- NHibernate 打不开工厂有可能是这几个原因
- PHP正则表达式,看这一篇就够啦!
- C++的dllexport和dllimport
- vue+element级联选择器对接后台数据
- falsk-sqlalchemy 连接数据库出现 No module named &#39;MySQLdb&#39;
- Hexo瞎折腾系列(8) - 添加评论系统
- render函数和redirect函数的区别+反向解析
- Codeforces Round #566 (Div. 2) A. Filling Shapes
- Net Core构建Angular4应用程序