题目链接

题目

题目描述

现在请求你维护一个数列,要求提供以下两种操作:

1、 查询操作。语法:Q L 功能:查询当前数列中末尾L 个数中的最大的数,并输出这个数的值。限制:L不超过当前数列的长度。

2、 插入操作。语法:A n 功能:将n加 上t,其中t是最近一次查询操作的答案(如果还未执行过查询操作,则t=0),并将所得结果对一个固定的常数D取模,将所得答案插入到数列的末尾。限制:n是非负整数并且在长整范围内。

注意:初始时数列是空的,没有一个数。

输入描述

第一行两个整数,M和D,其中M表示操作的个数(M ≤ 200,000),D如上文中所述,满足D在longint内。

接下来M行,查询操作或者插入操作。

输出描述

对于每一个询问操作,输出一行。该行只有一个数,即序列中最后L个数的最大数。

示例1

输入

5 100
A 96
Q 1
A 97
Q 1
Q 2

输出

96
93
96

备注

对于全部的测试点,保证 \(1 \leq M \leq 2 \times 10^5,1 \leq D \leq 2 \times 10^9\) 。

题解

方法一

知识点:线段树。

可以先开满空间,之后就是普通的单点修改、区间查询了。

时间复杂度 \(O(m \log m)\)

空间复杂度 \(O(m)\)

方法二

知识点:ST表。

因为修改操作是追加形式的,同样可以写成向前合并的ST表,维护向后追加。

时间复杂度 \(O(m \log m)\)

空间复杂度 \(O(m)\)

代码

方法一

#include <bits/stdc++.h>
using namespace std;
using ll = long long; struct T {
ll mx;
static T e() { return { 1LL << 63 }; }
friend T operator+(const T &a, const T &b) { return { max(a.mx, b.mx) }; }
};
struct F {
ll upd;
T operator()(const T &x) { return { upd }; }
};
template<class T, class F>
class SegmentTree {
int n;
vector<T> node; void update(int rt, int l, int r, int x, F f) {
if (r < x || x < l)return;
if (l == r) return node[rt] = f(node[rt]), void();
int mid = l + r >> 1;
update(rt << 1, l, mid, x, f);
update(rt << 1 | 1, mid + 1, r, x, f);
node[rt] = node[rt << 1] + node[rt << 1 | 1];
} T query(int rt, int l, int r, int x, int y) {
if (r < x || y < l) return T::e();
if (x <= l && r <= y) return node[rt];
int mid = l + r >> 1;
return query(rt << 1, l, mid, x, y) + query(rt << 1 | 1, mid + 1, r, x, y);
} public:
SegmentTree(int _n = 0) { init(_n); } void init(int _n) {
n = _n;
node.assign(n << 2, T::e());
} void update(int x, F f) { update(1, 1, n, x, f); } T query(int x, int y) { return query(1, 1, n, x, y); }
}; int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int m, d;
cin >> m >> d;
SegmentTree<T, F> sgt(m);
int cnt = 0;
ll t = 0;
while (m--) {
char op;
int x;
cin >> op >> x;
if (op == 'A') sgt.update(++cnt, { ((t + x) % d + d) % d });
else cout << (t = sgt.query(cnt - x + 1, cnt).mx) << '\n';
}
return 0;
}

方法二

#include <bits/stdc++.h>
using namespace std;
using ll = long long; ll node[21][200007];//! 这里是往前走的,标准st表是往后走的 int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int m, d;
cin >> m >> d;
int cnt = 0;
ll t = 0;
while (m--) {
char op;
int x;
cin >> op >> x;
if (op == 'A') {
node[0][++cnt] = ((t + x) % d + d) % d;
for (int i = 1;i <= 20 && cnt - (1 << i) + 1 >= 1;i++)
node[i][cnt] = max(node[i - 1][cnt - (1 << i - 1)], node[i - 1][cnt]);
}
else {
int k = log2(x);
cout << (t = max(node[k][cnt - x + 1 + (1 << k) - 1], node[k][cnt])) << '\n';
}
}
return 0;
}

NC20164 [JSOI2008]最大数MAXNUMBER的更多相关文章

  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. 【bzoj1012】[JSOI2008]最大数maxnumber

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

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

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

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

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

  6. [JSOI2008]最大数maxnumber

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

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

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

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

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

  9. 大视野 1012: [JSOI2008]最大数maxnumber(线段树/ 树状数组/ 单调队列/ 单调栈/ rmq)

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

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

    题目链接: 1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MB Description 现在请求你维护一个数列,要 ...

随机推荐

  1. @Import 源码解析

    转发请注明出处: @Import通过快速导入的方式实现把实例加入spring的IOC容器中:一般@EnableXXX注解是通过@Import实现具体的功能(@EnableXXX注解上加个@Import ...

  2. Vue- 绑定的图片不显示

    需要通过 require包裹 <template> <div> {{user.username}}: <img :src="user.avatar" ...

  3. HttpClient获取不到最新的系统代理

    默认情况下,HttpClient是默认采用系统代理,但是,如果你在程序运行过程中,手动修改系统代理,对于HttpClient是无效的,它依然会用老的代理去访问. 解决方法 使用下面的代码,你可以自己实 ...

  4. mysql-字符函数-拼接-长度-切片-替换

  5. Oracle的awr的学习与整理

    Oracle的awr的学习与整理 背景 本来想上周末进行一下总结和汇总 因为周末两天进行了一次长时间的培训.所以没有成行. 只能在工作之余找时间进行总结. 数据库部分自己一个不是很强. 其实也比较抗拒 ...

  6. Windows 修改时间提示: 某些设置已隐藏或由你的组织管理 的解决方案

    最近公司的一台生产服务器时间不对. 因为机器有域控的需求, 所以加入了域, 想改时间时有这样的提示信息: 某些设置已隐藏或由你的组织管理 百度了很久发现没有解决方法.. 但是突然发现可以使用 运行-& ...

  7. [转帖]LTP使用和分析

    一.安装及编译流程 1.下载LTP LTP 项目目前位于 GitHub,项目地址:https://github.com/linux-test-project/ltp . 获取最新版可以执行以下命令: ...

  8. [转帖] Linux命令拾遗-入门篇

    https://www.cnblogs.com/codelogs/p/16060394.html 原创:打码日记(微信公众号ID:codelogs),欢迎分享,转载请保留出处. 简介# 之前出过很多和 ...

  9. 银河麒麟安装nmon以及rpc.rstatd的方法

    背景说明 随着公司业务的发展,需要在ARM环境上面进行性能测试. 为了进行ARM环境的验证,需要一些组件进行资料收集. 比较好的方式是使用nmon或者是rstatd进行性能参数收集. 为了方便部署,想 ...

  10. 【解决了一个小问题】在某个linux基础镜像中安装python特定的版本

    作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢! cnblogs博客 zhihu Github 公众号:一本正经的瞎扯 在某个基础镜像中,安装了python3.6.但是一个测试需 ...