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

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

Sample Output

96
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 题解的更多相关文章

  1. BZOJ 1012

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

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

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

  3. bzoj一句话题解

    发现好多人都在搞这个...本人也想来试试(Solved刚到70就搞这个靠不靠谱啊喂).会更新的.嗯. 1000-1029 1000 A+B problem (这个还需要一句话吗?). 1001 狼抓兔 ...

  4. BZOJ 一句话题解

    菜鸡刷题记录 [题号:题解] 1008:简单排列组合 #include <bits/stdc++.h> using namespace std; #define ll long long ...

  5. Luogu P1198 BZOJ 1012 最大数 (线段树)

    手动博客搬家: 本文发表于20170821 14:32:05, 原地址https://blog.csdn.net/suncongbo/article/details/77449455 URL: (Lu ...

  6. 【BZOJ 1012】 [JSOI2008]最大数maxnumber(单调队列做法)

    [题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1012 [题意] [题解] 后加入的元素,如果比之前的元素大, 那么之前的元素比它小的元 ...

  7. 【BZOJ 1012】 [JSOI2008]最大数maxnumber(线段树做法)

    [题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1012 [题意] [题解] 预开一个20W长度的线段树; 这里a[1..20W]={0} ...

  8. BZOJ 3732 题解

    3732: Network Description 给你N个点的无向图 (1 <= N <= 15,000),记为:1…N. 图中有M条边 (1 <= M <= 30,000) ...

  9. bzoj 1012 [JSOI2008]最大数maxnumber

    原题链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1012 线段树,单点更新.. #include<algorithm> #incl ...

随机推荐

  1. HTTP中302与301的区别以及在ASP.NET中如何实现

    一.官方说法301,302 都是HTTP状态的编码,都代表着某个URL发生了转移,不同之处在于: 301 redirect: 301 代表永久性转移(Permanently Moved).302 re ...

  2. Gradle使用指南

    Gradle Plugin User Guide - Android Studio Project Sitehttp://tools.android.com/tech-docs/new-build-s ...

  3. Web框架之Tornado

    概述 Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py 或者 Google 的 webapp,不过为了 ...

  4. djcelery的细节篇

    http://blog.csdn.net/samed/article/details/50598371 随时撸一撸,要点记心间.. 1. 下面讲解一下celery.py文件的配置内容,为何要这么配置. ...

  5. js注入

    1.JavaScript注入就是在浏览器地址栏中输入一段js代码,用来改变页面js变量.页面标签的内容. 使用Javascript注入,用户不需要关闭或保存网页就可以改变其内容,这是在浏览器的地址栏上 ...

  6. go sample - hello world

    入门级别的hello world package mainimport "fmt"func main() { fmt.Println("blibliblbibl!&quo ...

  7. POJ 1625 Censored!(AC自动机+DP+高精度)

    Censored! Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 6956   Accepted: 1887 Descrip ...

  8. loj 1337

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1337 思路:对于搜过的区域进行标记,如果要求的点落在已经搜过的区域,那么直接取出来即可 ...

  9. Font Awesome符号字体

    http://www.fontawesome.com.cn/ 引用CSS包之后根据图标库找到所需的图标代码 使用i标签或者a标签皆可,符号为文字性质,可以直接通过修改text颜色从而修改符号颜色

  10. jsp网站环境搭建

    工具:tomcat7(exe安装版).jre7.javaxcms(安装版.非源码).mysql 1.先安装jre7,或者安装java7(自带了jre7) 2.安装tomcat7,期间要选择jre7安装 ...