BZOJ 1012 题解
1012: [JSOI2008]最大数maxnumber
Time Limit: 3 Sec Memory Limit: 162 MB
Submit: 8468 Solved: 3702
[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
Solution
/**************************************************************
Problem: 1012
User: shadowland
Language: C++
Result: Accepted
Time:844 ms
Memory:24732 kb
****************************************************************/ #include "bits/stdc++.h" using namespace std ;
const int maxN = ;
const int INF = ;
struct SegTree { int l , r , maxtr ; } ; SegTree tr[ maxN << ] ; int pos , cnt , last ; inline int gmax ( int x , int y ) { return x > y ? x : y ; }
inline void Push_up ( const int i ) { tr[ i ].maxtr = gmax ( tr[ i << | ].maxtr , tr[ i << ].maxtr ) ; } int INPUT ( ) {
int x = , f = ; char ch = getchar ( ) ;
while ( ch < '' || ch > '' ) { if ( ch == '-' ) f = - ; ch = getchar ( ) ; }
while ( ch >= '' && ch <= '' ) { x = ( x << ) + ( x << ) + ch - '' ; ch = getchar ( ) ;}
return x * f ;
} void Build_Tree ( const int x , const int y , const int i ) {
tr[ i ].l = x ; tr[ i ].r = y ;
tr[ i ].maxtr = -INF ;
if ( x == y ) return ;
else {
int mid = ( tr[ i ].l + tr[ i ].r ) >> ;
Build_Tree ( x , mid , i << ) ;
Build_Tree ( mid + , y , i << | ) ;
Push_up ( i ) ;
}
} void Insert ( const int q , const int val , const int i ) {
if ( tr[ i ].l == tr[ i ].r && tr[ i ].l == q ) {
tr[ i ].maxtr = val ;
return ;
}
else {
int mid = ( tr[ i ].l + tr[ i ].r ) >> ;
if ( q > mid ) Insert ( q , val , i << | ) ;
else if ( q <= mid ) Insert ( q , val , i << ) ;
Push_up ( i ) ;
}
} int Query_Tree ( const int q , const int w , const int i ){
if ( q <= tr[ i ].l && tr[ i ].r <= w ) {
return tr[ i ].maxtr ;
}
else {
int mid = ( tr[ i ].l + tr[ i ].r ) >> ;
if ( q > mid ) return Query_Tree ( q , w , i << | ) ;
else if ( w <= mid ) return Query_Tree ( q , w , i << ) ;
else {
return gmax ( Query_Tree ( q , w , i << | ) , Query_Tree ( q , w , i << ) ) ;
}
}
} int main ( ) {
int N = INPUT ( ) , MOD = INPUT ( ) ;
Build_Tree ( , N , ) ;
for ( int i= ; i<=N ; ++i ) {
char ch = getchar ( ) ;
if ( ch == 'A' ) {
++cnt ;
int tmp = ( INPUT ( ) + last ) % MOD ;
Insert ( cnt , tmp , ) ;
}
else if ( ch == 'Q' ) {
last = Query_Tree ( cnt - INPUT ( ) + , cnt , ) ;
printf ( "%d\n" , last ) ;
}
}
return ;
}
2016-10-14 23:31:51
BZOJ 1012 题解的更多相关文章
- BZOJ 1012
1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 7912 Solved: 3441[Submi ...
- BZOJ 1012: [JSOI2008]最大数maxnumber 单调队列/线段树/树状数组/乱搞
1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 4750 Solved: 2145[Submi ...
- bzoj一句话题解
发现好多人都在搞这个...本人也想来试试(Solved刚到70就搞这个靠不靠谱啊喂).会更新的.嗯. 1000-1029 1000 A+B problem (这个还需要一句话吗?). 1001 狼抓兔 ...
- BZOJ 一句话题解
菜鸡刷题记录 [题号:题解] 1008:简单排列组合 #include <bits/stdc++.h> using namespace std; #define ll long long ...
- Luogu P1198 BZOJ 1012 最大数 (线段树)
手动博客搬家: 本文发表于20170821 14:32:05, 原地址https://blog.csdn.net/suncongbo/article/details/77449455 URL: (Lu ...
- 【BZOJ 1012】 [JSOI2008]最大数maxnumber(单调队列做法)
[题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1012 [题意] [题解] 后加入的元素,如果比之前的元素大, 那么之前的元素比它小的元 ...
- 【BZOJ 1012】 [JSOI2008]最大数maxnumber(线段树做法)
[题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1012 [题意] [题解] 预开一个20W长度的线段树; 这里a[1..20W]={0} ...
- BZOJ 3732 题解
3732: Network Description 给你N个点的无向图 (1 <= N <= 15,000),记为:1…N. 图中有M条边 (1 <= M <= 30,000) ...
- bzoj 1012 [JSOI2008]最大数maxnumber
原题链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1012 线段树,单点更新.. #include<algorithm> #incl ...
随机推荐
- Android ANR分析(2)
转自:http://blog.csdn.net/ruingman/article/details/53118202 定义 主线程在特定的时间内没有做完特定的事情 常见的场景 A.input事件超过 ...
- 无废话ExtJs 入门教程十一[下拉列表:Combobox]
无废话ExtJs 入门教程十一[下拉列表:Combobox] extjs技术交流,欢迎加群(201926085) 继上一节内容,我们在表单里加了个一个下拉列表: 1.代码如下: 1 <!DOCT ...
- [LeetCode] Ugly Number
Ugly Number Total Accepted: 20760 Total Submissions: 63208 Difficulty: Easy Write a program to check ...
- SQL Server排序规则
在使用数据库的过程中,总会碰到一些特别的需求.有时候需要储存中文字符,区分大小写或者按照中文的比划顺序排序.这就涉及到了对数据库排列规则的选择. 我们一般可以选择数据库名称-->右键属性(Pro ...
- Linux/centos下安装riak
必备的组件: gccgcc-c++glibc-develmakepam-devel 使用yum安装相关组件 sudo yum install gcc gcc-c++ glibc-devel make ...
- COALESCE NVL NVL2 DECODE
1 COALESCE 語法:COALESCE(expr1, expr2, ..., exprn) n>=2 作用:COALESCE returns the first non-null expr ...
- PHP利用jquery生成各种验证码和Ajax验证
PHP生成验证码图片 PHP生成验证码的原理:使用PHP的GD库,生成一张带验证码的图片,并将验证码保存在Session中.PHP 生成验证码的大致流程有: .产生一张png的图片: .为图片设置背景 ...
- 用.NET开发通用Windows App
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:随着Windows 10的正式发布,作为.NET开发人员应该开始或多或少了解一下通用( ...
- 通过ajax访问Tomcat服务器web service接口时出现No 'Access-Control-Allow-Origin' header问题的解决办法
问题描述 通过ajax访问Web服务器(Tomcat7.0.42)中的json web service接口的时候,报以下跨域问题: XMLHttpRequest cannot load http:// ...
- cdh环境下,spark streaming与flume的集成问题总结
文章发自:http://www.cnblogs.com/hark0623/p/4170156.html 转发请注明 如何做集成,其实特别简单,网上其实就是教程. http://blog.csdn.n ...