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. Java Eclipse进行断点调试

    如何调试Java程序? 大家最开始学习Java,都会觉得IDE调试好高端有木有,其实很简单了. 下文会尽量简单直观的教会你在Eclipse中调试,其他的IDE调试步骤也是类似的. 1.在你觉得有错的地 ...

  2. jQuery函数attr()和prop()的区别

    在jQuery中,attr()函数和prop()函数都用于设置或获取指定的属性,它们的参数和用法也几乎完全相同. 但不得不说的是,这两个函数的用处却并不相同.下面我们来详细介绍这两个函数之间的区别. ...

  3. flume-ng 集群搭脚本

    #!/bin/bash # author: xirong # date : -- ##### 搭建 flume 集群的脚本 # 注意: # . 需要 jdk7 环境,如果没有 Java 环境,请配置 ...

  4. Oracle ASM

    一 Oracle ASM簡介 Oracle 10g推出的管理磁盤的新方式,用於取代LVM技術.主要用于RAC環境 二 Oracle ASM的配置安裝 1.安裝asm包 RedHat Linux5.x ...

  5. Oracle数据库 控制文件

    一.概念控制文件的主要任务是管理数据库的状态以及描述数据库的物理结构 二.所含有的信息1.数据库名2.数据库标识符(DBID)3.数据库创建时间戳4.数据库字符集5.数据文件信息6.临时文件信息7.在 ...

  6. hdu 3236 二维背包

    明天来一发 hdu 4501  算是这题的简化版吧

  7. 信号通讯编程,王明学learn

    信号通讯编程 在Linux系统中,信号(signal)同样也是最为古老的进程间通信机制. 一.信号类型 Linux系统支持的所有信号均定义在/usr/include/asm/signal.h(展示), ...

  8. 使用SOUI开发的界面集锦

    仿QQ管家界面

  9. Arduino101学习笔记(十一)—— 蓝牙BLE

    一.BLE技术简介 第四代蓝牙既包括传统的蓝牙,现在标有"蓝牙经典",和新的低功耗蓝牙(Bluetooth LE,或BLE).低数据速率,低功耗优化. 蓝牙LE广播就像一个社区公告 ...

  10. c++ shared_ptr 使用注意事项. 1

    条款1:不要把一个原生指针给多个shared_ptr管理 int* ptr = new int; shared_ptr<int> p1(ptr); shared_ptr<int> ...