https://www.luogu.org/problem/show?pid=1198

之前刚学完Splay想找题练手的时候做的,写完Splay交上去了才发现这应该是线段树裸题23333

Splay解法

按照要求操作即可……

#include <iostream>
using namespace std;
int m, d, t;
const int inf = 0x7fffffff;
namespace splay
{
struct node;
node *nil = , *root;
struct node
{
int val, size, maxnum;
node *ch[];
node(int v) : val(v), size(), maxnum(v)
{
ch[] = ch[] = nil;
}
void pull_up()
{
size = ch[]->size + ch[]->size + ;
maxnum = max(val, max(ch[]->maxnum, ch[]->maxnum));
}
int cmp(int k)
{
if(this == nil || k == ch[]->size + )
return -;
else
return (k <= ch[]->size) ? : ;
}
};
void init()
{
if(!nil)
nil = new node(-inf);
nil->size = ;
nil->ch[] = nil->ch[] = nil;
root = new node(-inf);
root->ch[] = new node(-inf);
root->size = ;
}
void rotate(node *&t, int d)
{
node *k = t->ch[d ^ ];
t->ch[d ^ ] = k->ch[d];
k->ch[d] = t;
t->pull_up();
k->pull_up();
t = k;
}
void splay(int k, node *&t = root)
{
int d1 = t->cmp(k);
if(d1 == )
k = k - t->ch[]->size - ;
if(d1 != -)
{
int d2 = t->ch[d1]->cmp(k);
if(d2 != -)
{
int k2 = (d2 == ) ? (k - t->ch[d1]->ch[]->size - ) : k;
splay(k2, t->ch[d1]->ch[d2]);
if(d1 != d2)
{
rotate(t->ch[d1], d2 ^ );
rotate(t, d1 ^ );
}
else
{
rotate(t, d1 ^ );
rotate(t, d2 ^ );
}
}
else
rotate(t, d1 ^ );
}
}
void insert(int val)
{
splay(root->size - );
node *k = root->ch[];
root->ch[] = new node(val);
root->ch[]->ch[] = k;
k->pull_up();
root->ch[]->pull_up();
root->pull_up();
}
int get_maxnum(int len)
{
int from = root->size - len;
int to = from + len - ;
splay(to + , root);
splay(from - , root->ch[]);
return root->ch[]->ch[]->maxnum;
}
}
int main()
{
using namespace splay;
cin >> m >> d;
init();
char a;
int b;
while(m--)
{
cin >> a >> b;
switch(a)
{
case 'A':
insert((b + t) % d);
break;
case 'Q':
t = get_maxnum(b);
cout << t << endl;
break;
}
}
return ;
}

线段树解法

M<=2e5,也就是极限状况下最多2e5个数。开个长度是2e5的线段树,再记录下已经加了N个数了。每次插入就是更改第N+1个元素,查询就是查询区间[N-L+1,N]的最大值。

懒得重写一遍了……

【JSOI2008】最大数的更多相关文章

  1. BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]

    1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 8748  Solved: 3835[Submi ...

  2. BZOJ-1012[JSOI2008]最大数maxnumber 线段树区间最值

    这道题相对简单下面是题目: 1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec Memory Limit: 162 MB Submit: 6542 Solve ...

  3. 洛谷P1198 [JSOI2008]最大数

    P1198 [JSOI2008]最大数 267通过 1.2K提交 题目提供者该用户不存在 标签线段树各省省选 难度提高+/省选- 提交该题 讨论 题解 记录 最新讨论 WA80的戳这QwQ BZOJ都 ...

  4. 【bzoj1012】[JSOI2008]最大数maxnumber

    1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 8339  Solved: 3624[Submi ...

  5. Cogs 1844. [JSOI2008]最大数maxnumber

    [JSOI2008]最大数maxnumber ★★ 输入文件:bzoj_1012.in 输出文件:bzoj_1012.out 简单对比 时间限制:3 s 内存限制:162 MB [题目描述] 现在请求 ...

  6. BZOJ 1012: [JSOI2008]最大数maxnumber【线段树单点更新求最值,单调队列,多解】

    1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 10374  Solved: 4535[Subm ...

  7. [JSOI2008]最大数maxnumber

    [JSOI2008]最大数maxnumber 标签: 线段树 单独队列 题目链接 题解 线段树裸题. 如果一直RE可能是你用的cin/cout. Code #include<cstdio> ...

  8. bzoj 1012: [JSOI2008]最大数maxnumber (线段树)

    1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 13081  Solved: 5654[Subm ...

  9. 洛谷 P1198 [JSOI2008]最大数

    洛谷 P1198 [JSOI2008]最大数 题目描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值. ...

  10. BZOJ 1012: [JSOI2008]最大数maxnumber 单调队列/线段树/树状数组/乱搞

    1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 4750  Solved: 2145[Submi ...

随机推荐

  1. Hacker Rank: Kingdom Division 不完全报告

    原题链接: Kingdom Division 由于树的层次可能很深,所以这里不能使用递归版的DFS.我使用了BFS. BFS确定各结点的父结点和它的孩子数. 用逆拓扑排序确定结点的计算顺序. same ...

  2. Windows解决anaconda下双python版本安装TensorFlow

    首先,就是双版本anaconda的安装: 以前安装好的是python2.7版本,而TensorFlow的安装仅支持3.5版本的.但是自己本来的2.7版本又不想遗弃.所以安装双版本的: 在anacond ...

  3. touchmove Bug 工作遇到

    touchmove在安卓浏览器上只会触发一次,需要preventDefault() touchmove events in Android web browsers have a really ser ...

  4. Python datetime之timedelta

    该函数表示两个时间的间隔 参数可选.默认值都为0:datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minut ...

  5. zip 安装mysql 和遇到的坑

    在官网下载了mysql 社区版的,官方网址:https://dev.mysql.com/downloads/mysql/ 解压后发现里面没有安装快捷方式,才知道是zip解压,dos窗口安装.这就比界面 ...

  6. Tinyhttpd精读解析

    首先,本人刚刚开始开源代码精读,写的不对的地方,大家轻拍,一起进步.本文是对Tinyhttpd的一次精读,大家每天都在用着http服务,很多人也一直活跃在上层,使用IIS.Apache等,大家是否想看 ...

  7. python迭代器以及itertools模块

    迭代器 在python中,迭代器协议就是实现对象的__iter()方法和next()方法,其中前者返回对象本身,后者返回容器的下一个元素.实现了这两个方法的对象就是可迭代对象.迭代器是有惰性的,只有在 ...

  8. python模块导入的方法与区别

    import ..   #导入整个模块 from .. import .. #导入模块中的类.函数或者变量 from .. import *  #导入模块中的所有公开成员 from .. import ...

  9. 删除“自豪的采用wordpress”

    网上的都是老一套了,方法不对. 听我的~ 先进入wordpress的安装目录,比如我的是:cd /www/wwwroot/www.yangnan.tk然后再进入,我的主题是twentyseventee ...

  10. C#中静态和非静态的区别

    今天下午面试,HR问道:C#中静态类或静态方法和非静态类静态方法有什么区别?我回答是静态的可以直接调用而非静态的需要实例化.HR说这谁都知道,我问的是本质区别.我当时就郁闷了,我只有8个月的编程经验, ...