「TJOI2019」甲苯先生的滚榜
问题分析
参照数据范围,我们需要一个能够在\(O(n\log n)\)复杂度内维护有序数列的数据结构。那么平衡树是很好的选择。参考程序中使用带旋Treap。
参考程序
#pragma GCC optimize( 3 )
#include <cstdio>
#include <ctime>
#include <algorithm>
namespace Treap {
struct member {
int Number, Time;
bool operator > ( const member Other ) const {
return Number < Other.Number || Number == Other.Number && Time > Other.Time;
};
bool operator == ( const member Other ) const {
return Number == Other.Number && Time == Other.Time;
};
bool operator < ( const member Other ) const {
return Number > Other.Number || Number == Other.Number && Time < Other.Time;
}
};
struct node {
int Random, Size, Cnt;
member Value;
node *LeftChild, *RightChild;
};
void Collect( node *A ) {
A->Size = A->Cnt + ( ( A->LeftChild != NULL ) ? A->LeftChild->Size : 0 ) + ( ( A->RightChild != NULL ) ? A->RightChild->Size : 0 );
return;
}
node *LeftRotate( node *A ) {
node *B = A->RightChild; A->RightChild = B->LeftChild; B->LeftChild = A; Collect( A ); Collect( B ); return B;
}
node *RightRotate( node *A ) {
node *B = A->LeftChild; A->LeftChild = B->RightChild; B->RightChild = A; Collect( A ); Collect( B ); return B;
}
node *Insert( node *Rt, member x ) {
if( Rt == NULL ) {
Rt = new node;
Rt->Random = rand() % 1000000000; Rt->Value = x; Rt->Size = 1; Rt->Cnt = 1; Rt->LeftChild = Rt->RightChild = NULL;
return Rt;
}
++( Rt->Size );
if( Rt->Value == x ) { ++( Rt->Cnt ); return Rt; }
if( Rt->Value < x ) {
Rt->RightChild = Insert( Rt->RightChild, x ); if( Rt->RightChild->Random < Rt->Random ) Rt = LeftRotate( Rt );
} else {
Rt->LeftChild = Insert( Rt->LeftChild, x ); if( Rt->LeftChild->Random < Rt->Random ) Rt = RightRotate( Rt );
}
return Rt;
}
node *Del( node *Rt, member x ) {
if( Rt == NULL ) { printf( "No such number called %d\n", x ); return Rt; }
if( Rt->Value == x ) {
if( Rt->Cnt > 1 ) { --( Rt->Cnt ); --( Rt->Size ); return Rt; }
if( Rt->LeftChild == NULL ) { node *T = Rt->RightChild; delete Rt; return T; }
if( Rt->RightChild == NULL ) { node *T = Rt->LeftChild; delete Rt; return T; }
if( Rt->LeftChild->Random <= Rt->RightChild->Random ) {
Rt = RightRotate( Rt ); --( Rt->Size ); Rt->RightChild = Del( Rt->RightChild, x ); return Rt;
} else {
Rt = LeftRotate( Rt ); --( Rt->Size ); Rt->LeftChild = Del( Rt->LeftChild, x ); return Rt;
}
return Rt;
}
--( Rt->Size );
if( Rt->Value < x ) { Rt->RightChild = Del( Rt->RightChild, x ); return Rt; }
else { Rt->LeftChild = Del( Rt->LeftChild, x ); return Rt; }
return Rt;
}
int QueryR( node *Rt, member x ) {
int Ans = 0;
for( ; Rt != NULL; ) {
if( Rt->Value == x ) return Ans + ( ( Rt->LeftChild != NULL ) ? Rt->LeftChild->Size : 0 ) + 1;
if( Rt->Value < x ) {
Ans += ( ( Rt->LeftChild != NULL ) ? Rt->LeftChild->Size : 0 ) + Rt->Cnt;
Rt = Rt->RightChild;
} else Rt = Rt->LeftChild;
}
return Ans + 1;
}
member QueryN( node *Rt, int x ) {
for( ; Rt != NULL; ) {
int Rc = 0; if( Rt->LeftChild != NULL ) Rc = Rt->LeftChild->Size;
if( x > Rc && x <= Rc + Rt->Cnt ) return Rt->Value;
if( x <= Rc ) Rt = Rt->LeftChild; else { x -= Rc + Rt->Cnt; Rt = Rt->RightChild; }
}
printf( "QueryNumber Failed.\n" );
return ( member ){ -1, -1 };
}
member pre( node *Rt, member x ) {
member Ans = x;
for( ; Rt != NULL; ) if( Rt->Value < x ) { Ans = Rt->Value; Rt = Rt->RightChild; } else Rt = Rt -> LeftChild;
if( Ans == x ) printf( "Query Pre Failed.\n" );
return Ans;
}
member suc( node *Rt, member x ) {
member Ans = x;
for( ; Rt != NULL; ) if( Rt->Value > x ) { Ans = Rt->Value; Rt = Rt->LeftChild; } else Rt = Rt -> RightChild;
if( Ans == x ) printf( "Query Suc Failed.\n" );
return Ans;
}
struct treap {
node *Root;
void clear() { delete [] Root; Root = NULL; srand( time( NULL ) ); return; }
void insert( member x ) { Root = Insert( Root, x ); return; }
void Delete( member x ) { Root = Del( Root, x ); return; }
int QueryRank( member x ) { return QueryR( Root, x ); }
member QueryNumber( int x ) { return QueryN( Root, x ); }
member Pre( member x ) { return pre( Root, x ); }
member Suc( member x ) { return suc( Root, x ); }
};
} //Treap
Treap::treap Tree;
namespace UI {
typedef unsigned int ui ;
ui randNum( ui& seed , ui last , const ui m){
seed = seed * 17 + last ; return seed % m + 1;
}
ui seed, last = 7;
void InSeed() { scanf( "%llu", &seed ); return; }
} //UI
const int Maxm = 100010;
Treap::member Rec[ Maxm ];
void MAIN() {
Tree.clear();
int n, m; scanf( "%d%d", &m, &n ); UI::InSeed();
for( int i = 1; i <= m; ++i ) {
Tree.insert( ( Treap::member ){ 0, 0 } );
Rec[ i ] = ( Treap::member ){ 0, 0 };
}
for( int i = 1; i <= n; ++i ) {
int x = UI::randNum( UI::seed, UI::last, m );
int y = UI::randNum( UI::seed, UI::last, m );
Tree.Delete( Rec[ x ] );
++Rec[ x ].Number;
Rec[ x ].Time += y;
Tree.insert( Rec[ x ] );
UI::last = Tree.QueryRank( Rec[ x ] ) - 1;
printf( "%llu\n", UI::last );
}
return;
}
int main() {
int TestCases; scanf( "%d", &TestCases );
for( ; TestCases--; ) MAIN();
return 0;
}
「TJOI2019」甲苯先生的滚榜的更多相关文章
- LG5338/BZOJ5509/LOJ3105 「TJOI2019」甲苯先生的滚榜 Treap
问题描述 LG5338 LOJ3105 BZOJ5509 题解 建立一棵\(\mathrm{Treap}\),把原来的\(val\)换成两个值\(ac,tim\) 原来的比较\(val_a<va ...
- 【LOJ】#3109. 「TJOI2019」甲苯先生的线段树
LOJ#3109. 「TJOI2019」甲苯先生的线段树 发现如果枚举路径两边的长度的话,如果根节点的值是$x$,左边走了$l$,右边走了$r$ 肯定答案会是$(2^{l + 1} + 2^{r + ...
- LOJ#3104「TJOI2019」甲苯先生的字符串
题目描述 一天小甲苯得到了一条神的指示,他要把神的指示写下来,但是又不能泄露天机,所以他要用一种方法把神的指示记下来. 神的指示是一个字符串,记为字符串 \(s_1\),\(s_1\) 仅包含小写字母 ...
- LG5337/BZOJ5508 「TJOI2019」甲苯先生的字符串 线性动态规划+矩阵加速
问题描述 LG5337 BZOJ5508 题解 设\(opt_{i,j}(i \in [1,n],j \in [1,26])\)代表区间\([1,i]\),结尾为\(j\)的写法. 设\(exist_ ...
- [TJOI2019]甲苯先生的滚榜——非旋转treap
题目链接: [TJOI2019]甲苯先生的滚榜 要求维护一个二维权值的集合并支持单点修改,用平衡树维护即可. 因为$n\le 10^6$但$m\le 10^5$,所以最多只有$10^5$个人被操作. ...
- 洛谷P5338 [TJOI2019]甲苯先生的滚榜
原题链接洛谷P5338 [TJOI2019]甲苯先生的滚榜 题目描述 甲苯先生在制作一个online judge,他发现做比赛的人们很关心自己的排名(显而易见),在acm赛制的比赛中,如果通过题目数量 ...
- BZOJ5509: [Tjoi2019]甲苯先生的滚榜
题解 开n个平衡树对每个AC数维护罚时,然后不同AC数用树状数组维护即可. 其实挺好写的...就是评测的时候评的巨久... #include <bits/stdc++.h> using n ...
- 【题解】Luogu P5338 [TJOI2019]甲苯先生的滚榜
原题传送门 这题明显可以平衡树直接大力整,所以我要说一下线段树+树状数组的做法 实际线段树+树状数组的做法也很暴力 我们先用树状数组维护每个ac数量有多少个队伍.这样就能快速求出有多少队伍ac数比现在 ...
- luogu P5338 [TJOI2019]甲苯先生的滚榜
传送门 首先,排名系统,一看就知道是原题,可以上平衡树来维护 然后考虑一种比较朴素的想法,因为我们要知道排名在一个人前面的人数,也就是AC数比他多的人数+AC数一样并且罚时少的人数,所以考虑维护那两个 ...
随机推荐
- Java中的mutable和immutable对象实例讲解
1.mutable(可变)和immutable(不可变)类型的区别 可变类型的对象:提供了可以改变其内部数据值的操作,其内部的值可以被重新更改. 不可变数据类型:其内部的操作不会改变内部的值,一旦试图 ...
- Robot Framework(三)项目实践出现的问题以及解决方法
导航: 1.元素定位失败 2.系统自带的确认弹窗 3.ElementNotVisibleException: Message: element not visible 1.元素定位失败(使用frame ...
- 第一篇 jQuery
1-1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3. ...
- python发起post请求获取json数据使用requests方法
最普通的答案 我一直就觉得GET和POST没有什么除了语义之外的区别,自打我开始学习Web编程开始就是这么理解的 . 可能很多人都已经猜到了答案是: 1.GET 使用URL或Cookie传参.而POS ...
- 微信开发新增拖动组件--movableview介绍
小程序的更新中,也新增了一个UI组件,它就是视图组件movable-view,它需要配合movable-area来一起使用.简单来说,它就是一个支持在指定区域内可以拖动内容的容器.我们来看一个简单的示 ...
- luogu P4482 [BJWC2018]Border 的四种求法
luogu 对于每个询问从大到小枚举长度,哈希判断是否合法,AC 假的(指数据) 考虑发掘border的限制条件,如果一个border的前缀部分的末尾位置位置\(x(l\le x < r)\)满 ...
- 深入理解hadoop之机架感知
深入理解hadoop之机架感知 机架感知 hadoop的replication为3,机架感知的策略为: 第一个block副本放在和client所在的datanode里(如果client不在集群范围内, ...
- Linux安装Redis、PHP安装Redis扩展模块
Redis的官方下载: http://redis.io/download 步骤一:下载安装包 步骤二:编译源程序 步骤三:移动文件.便于管理 步骤四:启动 Redis服务 查看是否启动成功 ...
- JS常用自定义函数总结
JS常用自定义函数总结 1.原生JavaScript实现字符串长度截取 2.原生JavaScript获取域名主机 3.原生JavaScript清除空格 4.原生JavaScript替换全部 5.原 ...
- IO模型(epoll)--详解-02
写在前面 从事服务端开发,少不了要接触网络编程.epoll作为linux下高性能网络服务器的必备技术至关重要,大部分游戏服务器都使用到这一多路复用技术.文章核心思想是:要让读者清晰明白EPOLL为什么 ...