题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1012

题意:维护一个数列,开始时没有数值,之后会有两种操作,

Q L :查询数列末k位的最大值;

A n:上一次查询的结果加上n添加到数列的末尾;第一次添加时,认为t = 0;

Sample Input

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

Sample Output

96
93
96
思路:保留输入的原值和用树状数组处理过的mx[i];其中mx[i]所管辖的范围就是lowbit(i)的值,即(i - lowbit(i),i];这样在之后查找时,每次先和原值比较,之后和比较比较的区间就是lowbit(i),但里面要两重循环来模拟整个比较的个数。如区间长度为10,比较的长度为 1,1,2,4,1,1.
#include<bits/stdc++.h>
using namespace std;
#define MS0(a) memset(a,0,sizeof(a))
#define MS1(a) memset(a,-1,sizeof(a))
#define ok puts(" ok")
#define bug puts(" bug ")
#define INF 0x3f3f3f3f
#define esp 1e-8 typedef long long ll;
const int maxx=;
ll num[maxx],len=,mx[maxx];
int lowbit(int i){return i&(-i);}
void modify(int pos)//更改1~pos的max
{
mx[pos]=num[pos];
for(int j=;j<lowbit(pos);j<<=)
{
mx[pos]=max(mx[pos],mx[pos-j]);
}
}
int query(int l,int r)
{
ll ans=-INF;
//lowbit()会出现1 2 4,这里面出现的间隔,就需要要两层循环里避免中间值;
//并且lowbit的值代表的就是该值代表的数的个数;
while(l<=r)
{
ans=max(ans,num[r]);
for(--r;l+lowbit(r)<=r;r-=lowbit(r))
ans=max(ans,mx[r]);
}
return ans;
}
int main()
{
//freopen("in.txt","r",stdin);
ll m,mod;
scanf("%lld%lld",&m,&mod);
ll t=,x;char op[];
for(int i=;i<=m;i++)
{
scanf("%s%lld",op,&x);
if(op[]=='A')
{
num[++len]=(t+x)%mod;
modify(len);
}
else{
t=query(len-x+,len);
printf("%lld\n",t);
}
}
return ;
}

【BZOJ】1012: [JSOI2008]最大数maxnumber 树状数组求区间最值的更多相关文章

  1. 【BZOJ1012】【树状数组求区间最值】最大数maxnumber

    Description 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作.语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度. 2. ...

  2. BZOJ 1012: [JSOI2008]最大数maxnumber 单调队列/线段树/树状数组/乱搞

    1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 4750  Solved: 2145[Submi ...

  3. BZOJ 1012: [JSOI2008]最大数maxnumber【线段树单点更新求最值,单调队列,多解】

    1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 10374  Solved: 4535[Subm ...

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

    1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 13081  Solved: 5654[Subm ...

  5. bzoj 2819 Nim dfn序+树状数组维护区间异或值

    题目大意 著名游戏设计师vfleaking,最近迷上了Nim.普通的Nim游戏为:两个人进行游戏,N堆石子,每回合可以取其中某一堆的任意多个,可以取完,但不可以不取.谁不能取谁输.这个游戏是有必胜策略 ...

  6. 树状数组求区间和模板 区间可修改 参考题目:牛客小白月赛 I 区间

    从前有个东西叫树状数组,它可以轻易实现一些简单的序列操作,比如单点修改,区间求和;区间修改,单点求值等. 但是我们经常需要更高级的操作,比如区间修改区间查询.这时候树状数组就不起作用了,只能选择写一个 ...

  7. [Split The Tree][dfs序+树状数组求区间数的种数]

    Split The Tree 时间限制: 1 Sec  内存限制: 128 MB提交: 46  解决: 11[提交] [状态] [讨论版] [命题人:admin] 题目描述 You are given ...

  8. BZOJ 1012 [JSOI2008]最大数maxnumber【线段树】

    水题,每次记录一下当前有多少个数,然后按照题目所指示的那样模拟就行,每次向线段树末尾插入(其实是修改)题目中指定的数,然后询问当前的个数到前面Q个数中最大值是多少结果就是,好久不碰线段树了,用数组模拟 ...

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

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1012 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作.语法:Q L 功能: ...

随机推荐

  1. (原)linux 编译 lwqq

    1.安装工具 apt-get install automake apt-get install autoconf apt-get install libtool apt-get install lib ...

  2. What is a heap?--reference

    A heap is a partially sorted binary tree. Although a heap is not completely in order, it conforms to ...

  3. python--while循环

    1.最简单的while True循环 count = while True : : print('hello',count) break count += hello 2.利用while循环写一个猜年 ...

  4. Android_Intent_passValue(4)

    xml布局文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns ...

  5. 为Photoshop添加右键快捷

    打开注册表,开始--->运行--->regedit 找到  HKEY_CLASSES_ROOT  <----> *<---->shell 新建项,使用Photosh ...

  6. linux commond

    1  vi /etc/sysconfig/network-scripts/ifcfg-eth0     2  ifconfig    3  ping 172.22.14.59    4  ping 1 ...

  7. form表单按enter键自动提交的问题

    废话不多说.直接上代码. 1:form表单按enter键自动提交的情况 <!doctype html> <html lang="en"> <head& ...

  8. 【转】java.util.vector中的vector的详细用法

    [转]java.util.vector中的vector的详细用法 ArrayList会比Vector快,他是非同步的,如果设计涉及到多线程,还是用Vector比较好一些 import java.uti ...

  9. 利用CodeSmith生成抽象工厂步骤

    其实CodeSmith挺好的,帮我们主动生成不少代码,并且代码质量不错,下面就来介绍一下利用CodeSmith生成抽象工厂步骤 打开codesmith模板的buildall 注意path的设置,因为后 ...

  10. sqlserver 变量

    变量:分为全局变量和局部变量全部变量:以@@声明,为系统变量,所有实例都能访问,用户只能访问,不能赋值局部变量:生命周期只在一个批处理内有效, 局部变量经常使用的三种用途:1 在循环语句中记录循环的次 ...