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 ...
随机推荐
- Delphi函数指针
参考:http://blog.chinaunix.net/uid-91034-id-2009700.html http://blog.csdn.net/procedure1984/article/de ...
- 【翻译三】java-并发之线程对象和实现
Thread Objects Each thread is associated with an instance of the class Thread. There are two basic s ...
- react.js基本操练
慢慢了解其它的JS前端框架... var data = [{ "when": "2 minutes ago", "who": "J ...
- hdu 1247:Hat’s Words(字典树,经典题)
Hat’s Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- php获取当前页面的完整url
javascript实现: top.location.href 顶级窗口的地址 this.location.href 当前窗口的地址 php实现: //测试网址: http://localhost/b ...
- hdu 4043 2011北京赛区网络赛D 概率+大数 **
推出公式为:P = A(2n,n)/(2^(2n)*n!) 但是不会大数,学完java再补
- [Oracle] PL/SQL学习笔记
-- 1. 使用一个变量 declare -- Local variables here v_name ); begin -- Test statements here select t.user_n ...
- maven web启动报错java.lang.ClassNotFoundException: org.springframework.web.util.Log4jConfigListener
问题描述 SEVERE: Error configuring application listener of class org.springframework.web.util.Log4jConfi ...
- 在Salesforce中用Data Loader去批量处理数据
Data Loader download file: Setup --> Administration Setup --> Data Loader --> Download the ...
- 关于移动端1px边框问题
<div class="z_nei_list"> <div class="z_name_left font-size3">身份证号:&l ...