[bzoj] 1012: [JSOI2008]最大数maxnumber 发表于 Jan 1 2015 | 分类于 bzoj | 题意 树状数组维护区间最值 传送门 分析 可以好好画画图 代码123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657#include <cstdio>int CH , NEG ;inline void read(int& ret) { ret = NEG = 0 ; while (CH=getchar() , CH<'!') ; if (CH == '-') NEG = true , CH = getchar() ; while (ret = ret*10+CH-'0' , CH=getchar() , CH>'!') ; if (NEG) ret = -ret ;}inline void reads(int& ret) {truewhile (ret=getchar() , ret<'!') ;truewhile (CH=getchar() , CH>'!') ;}#define lb(x) (x&(-x))#define maxn 200010LL#define max(x,y) ((x)>(y)?(x):(y))int m , LAST = 0 , ansmod ;int A[maxn] , C[maxn] = {0} , tot = 0 ;inline int QueryMax(int L , int R) {trueint ret = A[R] ;truewhile (L <= R) {truetrueif (A[R] > ret) ret = A[R] ;truetruefor ( -- R ; R-L >= lb(R) ; R -= lb(R))truetruetrueif (C[R] > ret) ret = C[R] ;true}truereturn ret ;}inline void Insert(int x) {trueA[++tot] = (LAST+x)%ansmod ;trueC[tot] = max(QueryMax(tot-lb(tot)+1 , tot-1) , A[tot]) ;}int cmd , x ;int main() {// #define READtrue#ifdef READtruetruefreopen(".in" ,"r",stdin ) ;truetruefreopen(".out","w",stdout) ;true#endiftrueread(m) , read(ansmod) ;truewhile (m -- ) {truetruereads(cmd) , read(x) ;truetrueif (cmd == 'A') Insert(x) ;truetrueelse printf("%d\n", LAST=QueryMax(tot-x+1 , tot)) ;true}true#ifdef READtruetruefclose(stdin) ; fclose(stdout) ;true#elsetruetruegetchar() ; getchar() ;true#endiftruereturn 0 ;}