题目链接
不是双倍经验我会去\(debug\)一上午?
一开始我是用的\(map+string\),跑的太慢了,T了4个点。
后来我手写了\(string\),重载了小于号,依然用的\(map\),T了2个点。
然后我加入各种卡常,发现没有用。
\(\cdots\)
然后我把手写\(string\)改成字符串哈希,依然用\(map\)存,还是\(T\)了2个点。
然后继续各种优化,没有用。
然后去看\(yyb\)聚聚的题解,原来可以每操作几百次随机\(Splay\)一下来保证树的随机性,只\(T\)了一个点了。
我去\(BZOJ\)离线题库上把这题数据下载下来,我觉得应该是最大的那个极限数据\(n=250000\)的点T了。
于是开文件本机上了试了下,跑了\(1.03-1.2s\)之间。
然后我疯狂改随机数种子,发现根本没什么用。
\(\cdots\)
后来又把\(map+\)字符串哈希改成了字典树,跑的飞快
最大的点本机\(0.7s\)左右,没理由跑不过啊
但交到洛谷上还是T
交到BZOJ上还是T
交到CJOJ上还是T
交到CodeVS上还是T
诶,CodeVS上显示TLE的点,\(n=130000\)
???????
于是找到\(BZOJ\)数据里的那个点,woc
原来有人的分数超过了\(999999999\),也就是我设的\(INF\),也就是\(Splay\)中2个虚点的值。
\(\cdots\)
直接超过了,这也太猛了。
然后虚点不是最大了,\(GG\)了。
看来\(INF\)还是要写成\(2147483647\)啊。
果然,改了\(INF\)后跑的飞快,总共才跑了\(479ms\)
吊打pbds

突然发现\(Splay\)好容易写

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <map>
#pragma GCC optimize(2)
using namespace std;
const int MAXN = 250010;
inline int read(){int s = 0;char ch = getchar();while(ch < '0' || ch > '9')ch = getchar(); while(ch >= '0' && ch <= '9'){ s = s * 10 + ch - '0'; ch = getchar(); }return s;
}
struct info{int val, id;int operator > (const info A) const{return val == A.val ? id < A.id : val > A.val;}
};
struct splay{info val;int size, ch[2], fa;
}t[MAXN];
int root, num, T;
struct Trie{int val;Trie *ch[26];Trie(){ for(int i = 0; i < 26; ++i) ch[i] = NULL; val = 0; }
}rt;
int Insert(char *s, int pos){int len = strlen(s);Trie *u = &rt;for(int i = 0; i < len; ++i){if(u->ch[s[i] - 'A'] == NULL) u->ch[s[i] - 'A'] = new Trie();u = u->ch[s[i] - 'A'];}if(u->val) return u->val;u->val = pos;return 0;
}
inline void pushup(int x){t[x].size = t[t[x].ch[0]].size + t[t[x].ch[1]].size + 1;
}
inline void rotate(int x){int y = t[x].fa, z = t[y].fa, k = t[y].ch[1] == x;t[y].ch[k] = t[x].ch[k ^ 1]; t[t[x].ch[k ^ 1]].fa = y;t[y].fa = x; t[x].ch[k ^ 1] = y;t[x].fa = z; t[z].ch[t[z].ch[1] == y] = x;pushup(y); pushup(x);
}
inline void Splay(int x, int goal){int y, z;while(t[x].fa != goal){y = t[x].fa; z = t[y].fa;if(z != goal) rotate((t[y].ch[1] == x) ^ (t[z].ch[1] == y) ? x : y);rotate(x);}if(!goal) root = x;
}
inline int insert(info x, int num){if(!root){ root = num; t[root].val = x; t[root].size = 1; return root; }int u = root, fa = 0;while(u){ fa = u; u = t[u].ch[x > t[u].val]; }int id = num; t[id].val = x; t[id].size = 1; t[id].fa = fa; if(fa) t[fa].ch[x > t[fa].val] = id;Splay(id, 0);return id;
}
int limit, tmp, Time;
void find(int x){int u = root;while(233){if(t[t[u].ch[0]].size == x - 1) break;if(t[t[u].ch[0]].size >= x) u = t[u].ch[0];else x -= t[t[u].ch[0]].size + 1, u = t[u].ch[1];}Splay(u, 0);
}
char ch, name[MAXN][12];
int len[MAXN];
void dfs(int x){if(!limit) return;if(t[x].ch[1]) dfs(t[x].ch[1]);if(!limit) return;for(int i = 0; i < len[x]; ++i)putchar(name[x][i]);putchar(' ');--limit;if(t[x].ch[0]) dfs(t[x].ch[0]);
}
int next(int x, int mode){Splay(x, 0);int u = t[root].ch[mode];while(t[u].ch[!mode]) u = t[u].ch[!mode];return u;
}
char s[12];
int pq;
int main(){//freopen("1.txt","r",stdin);//freopen("2.txt","w",stdout);T = read(); insert((info){ -2147483646, 9999999 }, ++num); insert((info){ 2147483646, -1 }, ++num);while(T--){ch = getchar();while(ch != '+' && ch != '?') ch = getchar();if(ch == '+'){ scanf("%s", s);if(pq = Insert(s, num + 1)){int l = next(pq, 0), r = next(pq, 1); Splay(l, 0);Splay(r, l);t[t[root].ch[1]].ch[0] = 0;Splay(t[root].ch[1], 0);insert((info){ read(), ++Time }, pq);}else{insert((info){read(), ++Time}, ++num); memcpy(name[num], s, sizeof s);len[num] = strlen(name[num]);}}if(ch == '?'){ch = getchar();if(ch >= '0' && ch <= '9'){tmp = 0;while(ch >= '0' && ch <= '9'){ tmp = tmp * 10 + ch - '0'; ch = getchar(); }find(num - tmp); for(int i = 0; i < len[root]; ++i)putchar(name[root][i]);putchar(' '); limit = 9;if(t[root].ch[0]) dfs(t[root].ch[0]);printf("\n");}else{int p = 0;while(ch >= 'A' && ch <= 'Z'){ s[p++] = ch; ch = getchar(); }for(int i = p; i < 12; ++i) s[i] = 0;Splay(Insert(s, 233), 0);printf("%d\n", t[t[root].ch[1]].size);}}if(T % 200 == 0) Splay(rand() % num + 1, 0); }return 0;
}

转载于:https://www.cnblogs.com/Qihoo360/p/10374578.html

【洛谷 P4291】 [HAOI2008]排名系统(Splay,Trie)相关推荐

  1. 数据结构(Splay平衡树):HAOI2008 排名系统

    [HAOI2008] 排名系统 [题目描述] 排名系统通常要应付三种请求:上传一条新的得分记录.查询某个玩家的当前排名以及返回某个区段内的排名记录.当某个玩家上传自己最新的得分记录时,他原有的得分记录 ...

  2. luogu P2584 [ZJOI2006]GameZ游戏排名系统 Splay

    luogu P2584 [ZJOI2006]GameZ游戏排名系统 Splay 实在不想调了QAQ... Code: #include <cstdio> #include <algo ...

  3. bzoj 1056 [HAOI2008]排名系统(1862 [Zjoi2006]GameZ游戏排名系统)

    1056: [HAOI2008]排名系统 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 1854  Solved: 502 [Submit][Sta ...

  4. 洛谷P3391文艺平衡树(Splay)

    题目传送门 转载自https://www.cnblogs.com/yousiki/p/6147455.html,转载请注明出处 经典引文 空间效率:O(n) 时间效率:O(log n)插入.查找.删除 ...

  5. BZOJ 1862: [Zjoi2006]GameZ游戏排名系统 Splay

    Splay的基本操作,比较繁琐..... 有个一坑点,sorce会超int 1862: [Zjoi2006]GameZ游戏排名系统 Time Limit: 5 Sec  Memory Limit: 6 ...

  6. [HAOI2008] 排名系统

    这道题目我并没有AC,得分停留在81. 我用的是 Hash + Splay .应该是因为我的 Hash 并不能很好的处理名字只有1个字母的情况. 大致说一下思路. 通过 Hash 建立或找到一名玩家的 ...

  7. 洛谷P3168 [CQOI2015]任务查询系统 [主席树,差分]

    题目传送门 任务查询系统 题目描述 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任 ...

  8. [HAOI2008]排名系统 [Zjoi2006]GameZ游戏排名系统 BZOJ1862BZOJ1056

    分析: 平衡树裸题,(学完LCT感觉自己不会普通的Splay了...),维护每个节点的权值大小顺序,和时间戳顺序,之后map维护一下是否存在过,(懒得写字符串hash了). 附上代码: #includ ...

  9. ●洛谷P3168 [CQOI2015]任务查询系统

    题链: https://www.luogu.org/problemnew/show/P3168 题解: 主席树 强制在线? 那就直接对每一个前缀时间建一个线段树(可持久化线段树),线段树维护优先度权值 ...

最新文章

  1. php程序里的configini_php中配置文件操作 如config.php文件的读取修改等操作
  2. 第88天:HTML5中使用classList操作css类
  3. URAL 1993 This cheeseburger you don't need 模拟题
  4. 数据库实验三 SQL查询数据
  5. python合并数组输出重复项_python进行数组合并的方法
  6. HDU 4417 Super Mario 主席树
  7. 【20220825】【数学基础】用最小二乘法求解超定方程组
  8. 【Java】自建IOS应用(IPA)发布服务器
  9. 易语言大漠对雷电模拟器中控后台的绑定
  10. 前端HTML上传图片传BASE64数据,图片太大进行压缩
  11. npm 使用淘宝镜像及切换回官方源
  12. Doxygen简介及使用说明
  13. 不要以抄底的心态去投资
  14. 毕业设计java 课程精品网站,java毕业设计_springboot框架的精品课程网站
  15. 求是科学班计算机怎么能够进,浙大游泳队获三全国冠军 都是学霸高考分数超700...
  16. UE4入门学习笔记(一)准备设计数据的优化处理
  17. python学习笔记 之爬取图片
  18. 赢得值系列1:赢得值管理的历史
  19. 阿里,华为,腾讯,小米2017实习面试经历
  20. 算法评价与神经网络算法

热门文章

  1. spring学习--引入外部文件,初始化属性
  2. 对超级计算机的认识有关论文,计算机科与技术专业的认识与思考.docx
  3. 绘制卡方分布的概率密度函数 matlab,MATLAB如何使用chi2pdf函数计算卡方分布的概率密度...
  4. 内置对象session与httpSession对象是同一个东西么?
  5. bootstrap-suggest插件处理复杂对象时的解决方案
  6. 设置广告 php,设置ecshop广告位
  7. python的设计哲学_Python的设计哲学
  8. linux命令 创建目录权限,linux创建用户并设置目录权限
  9. python framework jdon_python – Django Rest Framework和JSONField
  10. mysql 数据导出语句_MySQL 数据导出