BZOJ 1012: [JSOI2008]最大数maxnumber 线段树
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1012
现在请求你维护一个数列,要求提供以下两种操作: 1、 查询操作。语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值。限制:L不超过当前数列的长度。 2、 插入操作。语法:A n 功能:将n加上t,其中t是最近一次查询操作的答案(如果还未执行过查询操作,则t=0),并将所得结果对一个固定的常数D取模,将所得答案插入到数列的末尾。限制:n是非负整数并且在长整范围内。注意:初始时数列是空的,没有一个数。
Input
Output
算法分析:起初咋一看,可以用线段树做,于是就做了,迅速搞之,交之,WA...,为啥了,不应该呀,查代码,又交之,WA,,,无语中,后来突然想起了,C++提交得用lld呀,平常习惯了I64d了,买了个表的。
不过这道题可以用单调栈做,而且代码比线段树更简单。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define inf 0x7fffffff
using namespace std;
typedef long long LL;
const int maxn=+; LL M,D;
LL cnt,an[maxn],sum[maxn<<]; void PushUP(LL rt) {sum[rt]=max(sum[rt<<],sum[rt<<|]); } void build(LL l,LL r,LL rt)
{
sum[rt]=-inf;
if (l==r) return;
LL mid=(l+r)>>;
build(l,mid,rt<<);
build(mid+,r,rt<<|);
PushUP(rt);
} void update(LL l,LL r,LL rt,LL p,LL value)
{
if (l==r) {sum[rt]=value;return; }
LL mid=(l+r)>>;
if (p<=mid) update(l,mid,rt<<,p,value);
else update(mid+,r,rt<<|,p,value);
PushUP(rt);
} LL query(LL l,LL r,LL rt,LL x,LL y)
{
if (x<=l && r<=y) return sum[rt];
LL mid=(l+r)>>;
LL ret=-inf;
if (y<=mid) ret=max(ret,query(l,mid,rt<<,x,y));
else if (x>mid) ret=max(ret,query(mid+,r,rt<<|,x,y));
else
{
ret=max(ret,query(l,mid,rt<<,x,y));
ret=max(ret,query(mid+,r,rt<<|,x,y));
}
PushUP(rt);
return ret;
} int main()
{
while (scanf("%lld%lld",&M,&D)!=EOF)
{
memset(sum,,sizeof(sum));
LL t=,N=M;
char ch[];
LL l,v;
cnt=;
build(,M,);
for (LL i= ;i<M ;i++)
{
scanf("%s",ch);
if (ch[]=='A')
{
scanf("%lld",&v);
v=(v+t)%D;
update(,N,,++cnt,v);
}
else
{
scanf("%lld",&l);
LL ans=query(,N,,cnt-l+,cnt);
printf("%lld\n",ans);
t=ans;
}
}
}
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 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 功能:查询当前数 ...
- BZOJ 1012: [JSOI2008]最大数maxnumber【线段树单点更新求最值,单调队列,多解】
1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 10374 Solved: 4535[Subm ...
- 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 ...
- 【BZOJ 1012】 [JSOI2008]最大数maxnumber(线段树做法)
[题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1012 [题意] [题解] 预开一个20W长度的线段树; 这里a[1..20W]={0} ...
- BZOJ 1012 [JSOI2008]最大数maxnumber【线段树】
水题,每次记录一下当前有多少个数,然后按照题目所指示的那样模拟就行,每次向线段树末尾插入(其实是修改)题目中指定的数,然后询问当前的个数到前面Q个数中最大值是多少结果就是,好久不碰线段树了,用数组模拟 ...
随机推荐
- Permission Lists Assigned to a User
SQL that I find useful in many occasions. It will return a list of permissions that are assigned to ...
- Dynamic Prompt Table for Record Field on PeopleSoft Page
Sometimes a situation in project work arises to have a dynamic prompt table for record fields on Peo ...
- FileSystemWatcher用法详解
FileSystemWatcher控件主要功能: 监控指定文件或目录的文件的创建.删除.改动.重命名等活动.可以动态地定义需要监控的文件类型及文件属性改动的类型. 1.常用的几个基本属性: (1) P ...
- CSS中的content和attr的用法
问题缘起 在闲看别人网站时发现了这样的代码 <span class="hamburger icon" data-icon="C"> .icon:be ...
- WordPress 主题开发 - (七) 让主题更安全 待翻译
We're just about ready to start building our theme's template files. Before we do this, however, it' ...
- Nginx Location匹配举例
1.location / { if (!-f $request_filename){ rewrite ^/(.+)$ /uri.php last; }} ...
- 设计师眼中功能强大的Xcode
作为设计师,不仅要能创造出移动为先的新产品,更要了解能创造出优秀移动作品的工具.这个实现过程可以让我们的设计更加优秀. 过去两个月,我每天在 Xcode 上花费的时间大约有 10 个小时,我学到了很多 ...
- jQuery学习笔记(2)
val() 当鼠标放上去的时候,文本消失,鼠标拿开,文本恢复 效果图: code as below: <html xmlns="http://www.w3.org/1999/xhtml ...
- DoubanFm之设计模式(一)
前两版DoubanFm写的太戳,第一版可以忽略,当是熟悉WP手机的一些API.. 第二版用了比较多的依赖注入,熟悉了Messenger,过后越写越大,感觉不对,赶快打住..现在开始好好思考各模块了. ...
- hdu 4613 Points<计算几何>
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4613 题意: 判断一个集合中的点能不能由另一个集合中的点,通过平移,旋转,放缩得到~ 思路:先求出集合中的 ...