[BZOJ1012][JSOI2008]最大数maxnumber 线段树

没什么好说的……线段树维护区间就行了。第一次居然写错了,真丢人。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 typedef long long ll;
 6 ll inline readll(){
 7     ll Num;char ch;
 8     while((ch=getchar())<‘0‘||ch>‘9‘);Num=ch-‘0‘;
 9     while((ch=getchar())>=‘0‘&&ch<=‘9‘) Num=Num*10+ch-‘0‘;
10     return Num;
11 }
12 void outll(ll x){
13     if(x>=10) outll(x/10);
14     putchar(x%10+‘0‘);
15 }
16 int mx[200010<<2],len=0;
17 #define lson l,mid,rt<<1
18 #define rson mid+1,r,rt<<1|1
19 void Pushup(int &rt){
20     mx[rt]=max(mx[rt<<1],mx[rt<<1|1]);
21 }
22 void Add(int l,int r,int rt,int pos,int x){
23     if(l==r){
24         mx[rt]=x;
25         return;
26     }
27     int mid=l+r>>1;
28     if(pos<=mid) Add(lson,pos,x);
29     else Add(rson,pos,x);
30     Pushup(rt);
31 }
32 int Qry(int l,int r,int rt,int L,int R){
33     if(l>=L&&R>=r) return mx[rt];
34     int mid=l+r>>1,ret=0;
35     if(L<=mid) ret=max(ret,Qry(lson,L,R));
36     if(R>mid) ret=max(ret,Qry(rson,L,R));
37     return ret;
38 }
39 int main(){
40     int m=readll(),
41         d=readll();
42     char opt[5];
43     ll t=0,num;
44     memset(mx,0,sizeof(mx));
45     for(int i=1;i<=m;i++){
46         scanf("%s",opt);
47         num=readll();
48         if(opt[0]==‘A‘) Add(1,m,1,++len,(num%d+t%d)%d);
49         else{
50             t=Qry(1,m,1,len-num+1,len);
51             outll(t);
52             putchar(‘\n‘);
53         }
54     }
55     return 0;
56 }
时间: 2024-08-06 11:54:22

[BZOJ1012][JSOI2008]最大数maxnumber 线段树的相关文章

BZOJ1012 JSOI2008 最大数maxnumber 线段树/栈+二分法

题意:给定一个数列,要求维护:1.求倒数L个数中的最大值  2.在数列末尾插入(最近的1询问的答案+x)%D.其中初始序列为空. 法一:因为询问最多200000个,所以直接建一棵大小为M的线段树维护即可 #include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> using namespace std; struc

bzoj-1012 1012: [JSOI2008]最大数maxnumber(线段树)

题目链接: 1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MB Description 现在请求你维护一个数列,要求提供以下两种操作:1. 查询操作.语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度.2. 插入操作.语法:A n 功能:将n加上t,其中t是最近一次查询操作的答案(如果还未执行过查询操作,则t=0),并将所得结果对一个固定的常数D取模,将所得

BZOJ 1012: [JSOI2008]最大数maxnumber(线段树)

裸的线段树...因为数组开小了而一直RE..浪费了好多时间.. -------------------------------------------------------------------------- #include<cstdio> #include<algorithm> #include<cstring> #include<cctype> #include<iostream> #define rep(i,n) for(int i=

1012: [JSOI2008]最大数maxnumber 线段树

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

【BZOJ】1012: [JSOI2008]最大数maxnumber(树状数组+区间最值)

http://www.lydsy.com/JudgeOnline/problem.php?id=1012 树状数组原来我只懂得sum和add的操作,今天才知道可以有求区间最值的操作,我学习了一下写了个,1a了. 区间最值其实和区间求和差不多,就是将sum数组的含义转移到max,然后通过特定的区间更新max. 在区间求和中,当我们维护max[i]的时候,要找到它前面所有的max[j]来更新,在这里因为是树状数组,所以可以降成一个log级,画图可知,max[i]需要的max只有max[i-2^0],

单调栈 BZOJ1012 [JSOI2008]最大数maxnumber

1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 10440  Solved: 4571[Submit][Status][Discuss] Description 现在请求你维护一个数列,要求提供以下两种操作:1. 查询操作.语法:Q L 功能:查询当前数列中末尾L 个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度.2. 插入操作.语法:A n 功能:将n加 上t,其中t是最近一

【听说是线段树】bzoj1012 [JSOI2008]最大数maxnumber

一眼看题目吓了一跳:这TM不就是单调队列吗,200000又怎样,大不了我二分嘛 系统提示:成功开启 手残模式 开始瞎写: 1 #include <cstdio> 2 long long a[200001]; 3 int b[200001]; 4 int m,mod; 5 int find(int l,int r,long long x) 6 { 7 if(l>=r-1) 8 if(a[r]>=x) 9 return r; 10 else 11 return l; 12 int mi

[BZOJ1012] [JSOI2008] 最大数maxnumber (ST表)

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

bzoj1012 [JSOI2008]最大数maxnumber

这道题有很多的做法 裸的线段树 #include<cstdio> #define lc o*2 #define rc o*2+1 #define mid (l+r)/2 inline int max(int x,int y){ return x>y? x:y; } inline int read(){ char c; while(c=getchar(),c<'0' || c>'9'); int x=c-'0'; while(c=getchar(),c>='0' &