【bzoj 1012】[JSOI2008]最大数maxnumber(线段树||st表)
来源:互联网 发布:微信抢号软件 编辑:程序博客网 时间:2024/06/04 19:50
1012: [JSOI2008]最大数maxnumber
Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 8663 Solved: 3798
[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
A 96
Q 1
A 97
Q 1
Q 2
Sample Output
96
93
96
93
96
HINT
数据如下http://pan.baidu.com/s/1i4JxCH3
Source
线段树码:水题一道
#include<cstdio>#include<cstring>#include<algorithm>#define ll long longusing namespace std;struct node{ll x;char opt;}que[200010];ll ans,maxn[1000010];int m,mod,n,last;inline void updata(int now){maxn[now]=max(maxn[now<<1],maxn[now<<1|1]);}void change(int now,int l,int r,int x,int val){if(l==r) { maxn[now]=val; return; }int mid=(l+r)>>1;if(x<=mid) change((now<<1),l,mid,x,val); else change((now<<1)|1,mid+1,r,x,val);updata(now);}ll ask(int now,int l,int r,int al,int ar){if(al<=l&&r<=ar) return maxn[now];int mid=(l+r)>>1;ll sum=0;if(al<=mid) sum=max(sum,ask((now<<1),l,mid,al,ar));if(ar>mid) sum=max(sum,ask((now<<1|1),mid+1,r,al,ar));return sum;}int main(){freopen("maxnumber.in","r",stdin);freopen("maxnumber.out","w",stdout);int i;scanf("%d%d\n",&m,&mod);for(i=1;i<=m;++i) { scanf("%c%lld\n",&que[i].opt,&que[i].x); if(que[i].opt=='A') n++; }for(i=1;i<=m;++i) if(que[i].opt=='A') change(1,1,n,last+1,(que[i].x+ans)%mod),last++; else { ans=ask(1,1,n,(last-(int)que[i].x+1),last); printf("%lld\n",ans); }return 0;}
【题解】【st表】
【用st[i][j]表示从j位置到向前2的i次方的位置的区间中的最大值。因为插入元素时,是在末尾,所以只要每次加长区间即可】
【插入元素:st[n][i]=max(st[n][i-1],st[n-(1<<(i-1))][i-1]) 】
【询问:ans=max(st[n][mi[x]],st[n-x+(1<<mi[x])][mi[x]]);】
st表:#include<cstdio>#include<cstring>#include<algorithm>#define ll long longusing namespace std;ll st[200010][20],mi[200010],ans;int m,mod,n;int main(){freopen("maxnumber.in","r",stdin);freopen("maxnumber.out","w",stdout);int i,t=0;scanf("%d%d\n",&m,&mod);for(i=2;i<=m;++i) { while((1<<t)<=i) t++; mi[i]=t-1; }while(m--) { char c; int x; scanf("%c%d\n",&c,&x); if(c=='A') { st[++n][0]=((ll)x+ans)%mod; for(i=1;n-(1<<i)>=0;++i) st[n][i]=max(st[n][i-1],st[n-(1<<(i-1))][i-1]); } else { ans=max(st[n][mi[x]],st[n-x+(1<<mi[x])][mi[x]]); printf("%lld\n",ans); } } return 0;}
0 0
- 【bzoj 1012】[JSOI2008]最大数maxnumber(线段树||st表)
- BZOJ 题目1012: [JSOI2008]最大数maxnumber(线段树)
- bzoj 1012: [JSOI2008]最大数maxnumber(线段树)
- BZOJ 1012[JSOI2008]最大数maxnumber (线段树解法)
- |BZOJ 1012|线段树|[JSOI2008]最大数maxnumber
- 【BZOJ 1012】 [JSOI2008]最大数maxnumber(线段树做法)
- BZOJ 1012: [JSOI2008]最大数maxnumber 单调队列/线段树
- BZOJ 1012: [JSOI2008]最大数maxnumber 线段树入门题
- [BZOJ 1012][JSOI2008]最大数maxnumber:线段树|单调栈
- BZOJ 1012 [JSOI2008] 最大数maxnumber 题解与分析 (ZKW线段树实现)
- BZOJ 1012[jsoi2008] 最大数maxnumber 线段树(或者是单调队列+二分)
- bzoj 1012 [JSOI2008]最大数maxnumber(ST表最值查询——RMQ算法)
- 【bzoj1012】[JSOI2008]最大数maxnumber st表
- 【bzoj 1012】[JSOI2008]最大数maxnumber 线段树裸题
- BZOJ 1012 JSOI2008 最大数maxnumber 单调栈+二分 / 线段树
- BZOJ-1012[JSOI2008]最大数maxnumber 线段树区间最值
- BZOJ 1012 [JSOI2008]最大数maxnumber=听说是线段树
- BZOJ 1012: [JSOI2008]最大数maxnumber
- 图中奇点数量不可能有奇数个的证明
- TCP同步与异步及阻塞模式,多线程+阻塞模式,非阻塞模式简单介绍
- XCode8.1控制台不打印东西
- Aerospike数据库配置
- python sklearn包——cross validation笔记
- 【bzoj 1012】[JSOI2008]最大数maxnumber(线段树||st表)
- #392 – 使用SnapsToDevicePixels 属性消除抗锯齿效果(Use SnapsToDevicePixels Property to Prevent Anti-Aliasing)
- Spring@Autowired注解与自动装配
- C++文件长度
- Android设计模式系列(2)--SDK源码之观察者模式
- MySQL索引
- 服务器的瞬时 diffiehellman 公共秘钥过弱
- angularjs-表单验证事件
- Swift 3 新变化