平衡树的题,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)的更多相关文章

  1. hdu 1540 Tunnel Warfare(Treap)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1540 思路:三种操作: D摧毁一个点 R重建最晚被修改的那个点 Q询问点x联通的点有多少个 逆向思维,D操 ...

  2. HDU 5097 Page Rank (模拟)

    题目背景是以前用来对网页进行排名的Page Rank算法,是早期Google的革命性发明. 背后的原理是矩阵和图论.这个数学模型是由Google的创始人拉里·佩奇和谢尔盖·布林发现的. 如果一个网页被 ...

  3. HDU 5938 Four Operations(四则运算)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  4. HDU 5775 Bubble Sort(冒泡排序)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  5. HDU 1711 Number Sequence(数列)

    HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...

  6. HDU 1005 Number Sequence(数列)

    HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...

  7. HDU 1176 免费馅饼 (动态规划)

    HDU 1176 免费馅饼 (动态规划) Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼 ...

  8. 2021.12.07 P4291 [HAOI2008]排名系统(Treap)

    2021.12.07 P4291 [HAOI2008]排名系统(Treap) https://www.luogu.com.cn/problem/P4291 双倍经验: https://www.luog ...

  9. 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 ...

随机推荐

  1. POJ 1064 Cable master (二分)

    题意:给定 n 条绳子,它们的长度分别为 ai,现在要从这些绳子中切出 m 条长度相同的绳子,求最长是多少. 析:其中就是一个二分的水题,但是有一个坑,那么就是最后输出不能四舍五入,只能向下取整. 代 ...

  2. HDU 1556 Color the ball 前缀和+思维

    Color the ball N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球 ...

  3. Python开发【第四篇】:运算符

      1. 算术运算符   算术运算符包括+.-.*./.%.//.**,即加减乘除,取余,取商(地板除法)以及幂运算. >>> 5 + 2 7 >>> 5 - 2 ...

  4. mysql由浅入深探究(二)----mysql用户操作

    上一节我们完成了mysql的安装,现在我们将开启实战模式,完成一些基本的mysql操作.这节我们分为一些几个内容: mysql中用户新建 mysql中用户删除 mysql中用户修改 mysql中用户查 ...

  5. SqlHelper 增删改查

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  6. MySQL数据库基础详解(非原创)

    文章大纲 一.数据库简介二.Mysql数据库简介三.Mysql安装与服务启动(Windows版本)四.Mysql图形化工具五.Mysql存储引擎精讲六.Mysql数据类型介绍七.Mysql主要专业名称 ...

  7. [Xcode 实际操作]六、媒体与动画-(14)使用SystemSoundId播放简短声音

    目录:[Swift]Xcode实际操作 本文将演示如何播放音频素材. 在项目名称上点击鼠标右键,弹出右键菜单, 选择[Add Files to "DemoApp"],往项目中导入文 ...

  8. 基于SSM搭建网站实现增删改查

    网站源码地址:https://github.com/MyCreazy/BasicOperateWebSite.git 使用maven搭建网站的时候,记得选用war包格式,有时候maven包没有引用进来 ...

  9. MySQL server has gone away和Maximum execution time of 120 seconds exceeded

    今天在写采集时碰到两个问题1.MySQL server has gone away2.Maximum execution time of 120 seconds exceeded 采集程序写好运行大概 ...

  10. CF 700E

    构建后缀自动机,求出后缀树 比较明显的dp 设 \(f[i]\) 表示从上而下到达当前点能够满足条件的最优值 只需要检查父亲节点是否在当前串中出现过两次就行了 这个判断用 \(endpos\) 来判断 ...