BZOJ1012: [JSOI2008]最大數maxnumber [線段樹]

來源:互聯網
上載者:User

標籤:getch   main   文法   blog   注意   turn   操作   include   size   

1012: [JSOI2008]最大數maxnumberTime Limit: 3 Sec  Memory Limit: 162 MB
Submit: 8748  Solved: 3835
[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 Input5 100
A 96
Q 1
A 97
Q 1
Q 2
Sample Output96
93
96
按2e5建樹,單點修改+區間最大值 一開始愚蠢的n+MOD防止n為負反而溢出了
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#define m ((l+r)>>1)#define lson o<<1,l,m#define rson o<<1|1,m+1,r#define lc o<<1#define rc o<<1|1using namespace std;typedef long long ll;const int N=2e5+5,INF=2e9+5;inline int read(){    char c=getchar();int x=0,f=1;    while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();}    while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();}    return x*f;}int n,nn,MOD;char c[2]; int x;int t[N<<2];void build(int o,int l,int r){    if(l==r) t[o]=-INF;    else{        build(lson);        build(rson);    }}void change(int o,int l,int r,int p,int v){    if(l==r) t[o]=v;    else{        if(p<=m) change(lson,p,v);        if(m<p) change(rson,p,v);        t[o]=max(t[lc],t[rc]);    }}int query(int o,int l,int r,int ql,int qr){    if(ql<=l&&r<=qr) return t[o];    else{        int mx=-INF;        if(ql<=m) mx=max(mx,query(lson,ql,qr));        if(m<qr) mx=max(mx,query(rson,ql,qr));         return mx;    }}int main(){    n=read();MOD=read();    nn=2e5;    build(1,1,nn);    int last=0,len=0;    for(int i=1;i<=n;i++){        scanf("%s",c); x=read();        if(c[0]==‘Q‘){            last=query(1,1,nn,len-x+1,len);            printf("%d\n",last);        }else{            x=(x+last)%MOD;            len++;            change(1,1,nn,len,x);        }    }}

 

 

BZOJ1012: [JSOI2008]最大數maxnumber [線段樹]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.