当前位置:网站首页>NC20164 :最大数MAXNUMBER [线段树]
NC20164 :最大数MAXNUMBER [线段树]
2022-08-05 08:11:00 【9ack!?】
题目大意
维护一个数列,要求提供以下两种操作:
- 查询操作。语法:Q L 功能:查询当前数列中末尾L 个数中的最大的数,并输出这个数的值。限制:L不超过当前数列的长度。
- 插入操作。语法:A n 功能:将n加 上t,其中t是最近一次查询操作的答案(如果还未执行过查询操作,则t=0),并将所得结果对一个固定的常数D取模,将所得答案插入到数列的末尾。限制:n是非负整数并且在长整范围内。
注意:初始时数列是空的,没有一个数。
输入描述:
第一行两个整数,M和D,其中M表示操作的个数(M ≤ 200,000),D如上文中所述,满足D在longint内。接下来M行,查询操作或者插入操作。
输出描述:
对于每一个询问操作,输出一行。该行只有一个数,即序列中最后L个数的最大数。
示例
输入
5 100
A 96
Q 1
A 97
Q 1
Q 2
输出
96
93
96
思路
其实就是线段树的模版题,因为数据量比较小,所以都用不到带标记的线段树模版,就注意一下每次插入的位置和查询时左右边界的问题吧。
代码
#include <cstdio>
#include <algorithm>
using namespace std;
#define LC k<<1
#define RC k<<1|1
typedef long long ll;
const int maxn = 2e5+5;
ll f[maxn<<2];
ll t = 0;
ll last = 0;
ll M, D;
inline void buildtree(int k, int l, int r) {
f[k] = 0;
if(l == r) {
return; }
int m = (l+r) >> 1;
buildtree(LC, l, m);
buildtree(RC, m+1, r);
}
inline void insert(int k, int l, int r, int x, int y, int z) {
if(l == x && r == y) {
f[k] = z;
return;
}
int m = (l+r) >> 1;
if(y <= m) {
insert(LC, l, m, x, y, z);
} else if(x > m) {
insert(RC, m+1, r, x, y, z);
} else {
insert(LC, l, m, x, m, z);
insert(RC, m+1, r, m+1, y, z);
}
f[k] = max(f[LC], f[RC]);
}
inline ll query(int k, int l, int r, int x, int y) {
if(l == x && r == y) {
return f[k]; }
int m = (l+r) >> 1;
if(y <= m) {
return query(LC, l, m, x, y);
} else if(x > m) {
return query(RC, m+1, r, x, y);
} else {
return max(query(LC, l, m, x, m), query(RC, m+1, r, m+1, y));
}
}
int main(void)
{
/* freopen("in.txt", "r", stdin); */
scanf("%lld%lld", &M, &D);
buildtree(1, 1, M);
char op; ll x;
for(int q = 1; q <= M; ++q) {
scanf(" %c%lld", &op, &x);
if(op == 'A') {
++last;
insert(1, 1, M, last, last, (x+t)%D);
} else {
t = query(1, 1, M, last-x+1, last);
/* printf("%d %d\n", last-x+1, last); */
printf("%lld\n", t);
}
}
return 0;
}
边栏推荐
- MongoDB 语法大全
- SVG big fish eat small fish animation js special effects
- 软件系统测试和验收测试有什么联系与区别?专业软件测试方案推荐
- unity urp 渲染管线顶点偏移的实现
- Codeforce 8.1-8.7做题记录
- MySQL database error The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid)
- 随机码的生成
- pnpm 是凭什么对 npm 和 yarn 降维打击的
- D2--FPGA SPI接口通信2022-08-03
- SVG星球大战样式Toggle切换开关按钮
猜你喜欢
随机推荐
Chapter 12 贝叶斯网络
The Coolest Kubernetes Network Solution Cilium Getting Started Tutorial
v-if/v-else determines whether to display according to the calculation
数据源对象管理Druid和c3p0
Fiddler tool explanation
The color of life divine
导出SQLServer数据到Excel中
漂亮MM和普通MM的区别
Detailed explanation of DNS query principle
egg框架中解决跨域的三种方案
ps怎么拼图,自学ps软件photoshop2022,PS制作拼图效果
程序设计中的感悟
【每日一题】1403. 非递增顺序的最小子序列
The magic weapon for small entrepreneurs!
力扣每日一题
JVM运行流程,运行时数据区,类加载,垃圾回收,JMM解析
在ASP控制数字及字母输入
Access Denied: "microsoft.web.ui.webcontrols" workaround
php fails to write data to mysql
love is a sad song