首页 技术 正文
技术 2022年11月13日
0 收藏 546 点赞 3,436 浏览 2142 个字

1012: [JSOI2008]最大数maxnumber

Time Limit: 3 Sec  Memory Limit: 162 MB
Submit: 8748  Solved: 3835
[Submit][Status][Discuss]

Description

  现在请求你维护一个数列,要求提供以下两种操作:1、 查询操作。语法:Q L 功能:查询当前数列中末尾L
个数中的最大的数,并输出这个数的值。限制:L不超过当前数列的长度。2、 插入操作。语法:A n 功能:将n加
上t,其中t是最近一次查询操作的答案(如果还未执行过查询操作,则t=0),并将所得结果对一个固定的常数D取
模,将所得答案插入到数列的末尾。限制:n是非负整数并且在长整范围内。注意:初始时数列是空的,没有一个
数。

Input

  第一行两个整数,M和D,其中M表示操作的个数(M <= 200,000),D如上文中所述,满足D在longint内。接下来
M行,查询操作或者插入操作。

Output

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

Sample Input

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

Sample Output

96
93
96


[ 2016-11-15]按2e5建树,单点修改+区间最大值 一开始愚蠢的n+MOD防止n为负反而溢出了

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define m ((l+r)>>1)
#define lson o<<1,l,m
#define rson o<<1|1,m+1,r
#define lc o<<1
#define rc o<<1|1
using namespace std;
typedef long long ll;
const int N=2e5+,INF=2e9+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,nn,MOD;
char c[]; int x;
int t[N<<];
void build(int o,int l,int r){
if(l==r) t[o]=-INF;
else{
build(lson);
build(rson);
}
}
void change(int o,int l,int r,int p,int v){
if(l==r) t[o]=v;
else{
if(p<=m) change(lson,p,v);
if(m<p) change(rson,p,v);
t[o]=max(t[lc],t[rc]);
}
}
int query(int o,int l,int r,int ql,int qr){
if(ql<=l&&r<=qr) return t[o];
else{
int mx=-INF;
if(ql<=m) mx=max(mx,query(lson,ql,qr));
if(m<qr) mx=max(mx,query(rson,ql,qr));
return mx;
}
}
int main(){
n=read();MOD=read();
nn=2e5;
build(,,nn);
int last=,len=;
for(int i=;i<=n;i++){
scanf("%s",c); x=read();
if(c[]=='Q'){
last=query(,,nn,len-x+,len);
printf("%d\n",last);
}else{
x=(x+last)%MOD;
len++;
change(,,nn,len,x);
}
}
}

[2017-01-06]

还可以用单调栈+二分找栈中第一个>=p-L+1的元素

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int N=2e5+,INF=2e9+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int Q,MOD;
char s[];
int st[N],top,x,t,L,a[N],p;
int main(){
//freopen("in.txt","r",stdin);
Q=read();MOD=read();
while(Q--){
scanf("%s",s);
if(s[]=='A'){
x=read();
x=(x+t)%MOD;
a[++p]=x;
while(top&&a[st[top]]<=x) top--;
st[++top]=p;
}else{
L=read();
int pos=lower_bound(st+,st++top,p-L+)-st;
t=a[st[pos]];
printf("%d\n",t);
}
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,027
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,518
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,365
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,146
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,780
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,857