BZOJ 1012: [JSOI2008]最大数maxnumber【线段树单点更新求最值,单调队列,多解】
1012: [JSOI2008]最大数maxnumber
Time Limit: 3 Sec Memory Limit: 162 MB
Submit: 10374 Solved: 4535
[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
A 96
Q 1
A 97
Q 1
Q 2
Sample Output
93
96
HINT
数据如下http://pan.baidu.com/s/1i4JxCH3
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1012
分析:线段树,,,,,我可能又是智障了,调了俩个小时,一个update写错一个细节,WA了三发,然后一直Re,mmp,开80W数组都不够?开了个270W的数组才过,这题目有毒!这题其实也很简单,先建一棵树,结点都为空,然后记录下插入的数的个数为cnt,逐个插入,每次询问cnt-l+1到cnt的最大值即可!
下面给出AC代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=;
#define inf 0x7fffffff
inline int read()
{
int x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')
f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
inline void write(int x)
{
if(x<)
{
putchar('-');
x=-x;
}
if(x>)
write(x/);
putchar(x%+'');
}
struct Tree
{
int L,R,maxn;
}tree[N<<];
inline void buildtree(int l,int r,int pos)
{
tree[pos].L=l;
tree[pos].R=r;
tree[pos].maxn=-inf;
if(l==r)
return;
int mid=(l+r)/;
buildtree(l,mid,pos*);
buildtree(mid+,r,pos*+);
}
inline int Query(int l,int r,int pos)
{
if(tree[pos].L==l&&tree[pos].R==r)
return tree[pos].maxn;
int mid=(tree[pos].L+tree[pos].R)/;
if(r<=mid)
return Query(l,r,pos*);
else if(l>mid)
return Query(l,r,pos*+);
else return max(Query(l,mid,pos*),Query(mid+,r,pos*+));
}
inline void update(int l,int r,int pos)
{
if(tree[pos].L==tree[pos].R)
{
tree[pos].maxn=r;
return;
}
int mid=(tree[pos].L+tree[pos].R)/;
if(l<=mid)
update(l,r,pos*);
else update(l,r,pos*+);
tree[pos].maxn=max(tree[pos*].maxn,tree[pos*+].maxn);
}
int m,mod,last,cnt;
int main()
{
m=read();
mod=read();
buildtree(,m,);
for(int i=;i<=m;i++)
{
char ch[];
scanf("%s",ch);
int x;
if(ch[]=='A')
{
cnt++;
x=read();
x=(x+last)%mod;
update(cnt,x,);
}
else
{
x=read();
last=Query(cnt-x+,cnt,);
printf("%d\n",last);
}
}
return ;
}
似乎可以用单调队列写?
贴个AC代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn=;
int m,d,a[maxn<<],t,maxx[maxn<<],l,p;
char q[];
int main()
{
scanf("%d%d",&m,&d);
while(m--)
{
scanf("%s%d",q,&p);
if(q[]=='A')
{
a[++t]=(l+p)%d;
for(int i=t;i;i--)
{
if(maxx[i]<a[t])
maxx[i]=a[t];
else break;
}
}
else printf("%d\n",l=maxx[t-p+]);
}
return ;
}
BZOJ 1012: [JSOI2008]最大数maxnumber【线段树单点更新求最值,单调队列,多解】的更多相关文章
- bzoj 1012: [JSOI2008]最大数maxnumber (线段树)
1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 13081 Solved: 5654[Subm ...
- BZOJ 1012: [JSOI2008]最大数maxnumber 线段树
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1012 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作.语法:Q L 功能: ...
- bzoj-1012 1012: [JSOI2008]最大数maxnumber(线段树)
题目链接: 1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec Memory Limit: 162 MB Description 现在请求你维护一个数列,要 ...
- 1012: [JSOI2008]最大数maxnumber 线段树
https://www.lydsy.com/JudgeOnline/problem.php?id=1012 现在请求你维护一个数列,要求提供以下两种操作:1. 查询操作.语法:Q L 功能:查询当前数 ...
- HDU 1754 I Hate It 线段树单点更新求最大值
题目链接 线段树入门题,线段树单点更新求最大值问题. #include <iostream> #include <cstdio> #include <cmath> ...
- BZOJ 1012: [JSOI2008]最大数maxnumber 单调队列/线段树/树状数组/乱搞
1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 4750 Solved: 2145[Submi ...
- BZOJ-1012[JSOI2008]最大数maxnumber 线段树区间最值
这道题相对简单下面是题目: 1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec Memory Limit: 162 MB Submit: 6542 Solve ...
- BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]
1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 8748 Solved: 3835[Submi ...
- 【HDU】1754 I hate it ——线段树 单点更新 区间最值
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
随机推荐
- Struts2学习---简单的数据校验、访问Web元素
1.简单的数据校验 在action里面我们已经给出了一个数据校验: public String execute() { if(user.getUsername().equals("usern ...
- iOS 进阶—— iOS 内存管理
1 似乎每个人在学习 iOS 过程中都考虑过的问题 alloc retain release delloc 做了什么? autoreleasepool 是怎样实现的? __unsafe_unretai ...
- go defer (go延迟函数)
go defer (go延迟函数) Go语言的defer算是一个语言的新特性,至少对比当今主流编程语言如此.根据GO LANGUAGE SPEC的说法: A "defer" sta ...
- Memcached的简介和使用
缘起: 在数据驱动的web开发中,经常要重复从数据库中取出相同的数据,这种重复极大的增加了数据库负载.缓存是解决这个问题的好办法.但是ASP.NET中的虽然已经可以实现对页面局部进行缓存,但还是不够灵 ...
- nova创建虚拟机源码分析系列之四 nova代码模拟
在前面的三篇博文中,介绍了restful和SWGI的实现.结合restful和WSGI配置就能够简单的实现nova服务模型的最简单的操作. 如下的内容是借鉴网上博文,因为写的很巧妙,将nova管理虚拟 ...
- Python列表list对象方法总结
- Siamese Network理解
提起siamese network一般都会引用这两篇文章: <Learning a similarity metric discriminatively, with application to ...
- golang社区
a development list for Go Programming Language https://groups.google.com/forum/#!forum/golang-dev a ...
- SecureCRT连接本地的Vmware虚拟机(CentOS)时提示连接超时“Connection timed out”
测试了一下,直接在Vmware的VM里面可以ping通宿主机. 但是宿主机无法ping通VM. 后面发现是本地的网络设置里面的vmware的NAT的网卡设置了手工填写地址和DNS. 修改为自动获取.问 ...
- Linux下防火墙配置
查看防火墙的状态:/etc/init.d/iptables status 或 service iptables status 1) 临时生效,重启后复原 开启: service iptab ...