[JSOI2008]最大數 --線段樹

來源:互聯網
上載者:User

標籤:負數   closed   修改   超過   name   using   code   else   mod   

      [JSOI2008]最大數 --線段樹題目描述

現在請求你維護一個數列,要求提供以下兩種操作:

1、 查詢操作。

文法:Q L

功能:查詢當前數列中末尾L個數中的最大的數,並輸出這個數的值。

限制:L不超過當前數列的長度。

2、 插入操作。

文法:A n

功能:將n加上t,其中t是最近一次查詢操作的答案(如果還未執行過查詢操作,則t=0),並將所得結果對一個固定的常數D模數,將所得答案插入到數列的末尾。

限制:n是整數(可能為負數)並且在長整範圍內。

注意:初始時數列是空的,沒有一個數。

輸入輸出格式

輸入格式:

 

第一行兩個整數,M和D,其中M表示操作的個數(M <= 200,000),D如上文中所述,滿足(0<D<2,000,000,000)

接下來的M行,每行一個字串,描述一個具體的操作。文法如上文所述。

 

輸出格式:

 

對於每一個查詢操作,你應該按照順序依次輸出結果,每個結果佔一行。

 

輸入輸出範例輸入範例#1:
5 100A 96Q 1A 97Q 1Q 2
輸出範例#1:
969396
說明

[JSOI2008]

 

 1 /* 2     資料略水  3     有一個比較神奇的處理 4     就是把所有加點的處理分離出來 處理要加多少點 5     然後就是單點修改+區間查詢  6 */ 7 #include <cstdio> 8 #include <ctype.h> 9 #include <iostream>10 11 using namespace std;12 13 typedef long long LL;14 15 const int MAXN=200010;16 17 int m,d,n;18 19 char s[MAXN];20 21 LL a[MAXN];22 23 struct node {24     int l,r;25     LL sum;26 };27 node t[MAXN<<2];28 29 inline void read(LL&x) {30     int f=1;register char c=getchar();31     for(x=0;!isdigit(c);c==‘-‘&&(f=-1),c=getchar());32     for(;isdigit(c);x=x*10+c-48,c=getchar());33 }34 35 inline LL max(LL a,LL b) {36     return a<b?b:a;37 }38 39 inline void build_tree(int now,int l,int r) {40     t[now].l=l,t[now].r=r;41     if(l==r) return;42     int mid=(l+r)>>1;43     build_tree(now<<1,l,mid);44     build_tree(now<<1|1,mid+1,r);45 }46 47 inline void modify(int now,int pos,LL v) {48     if(t[now].l==t[now].r) {49         t[now].sum+=v;50         return;51     }52     int mid=(t[now].l+t[now].r)>>1;53     if(pos<=mid) modify(now<<1,pos,v);54     else modify(now<<1|1,pos,v);55     t[now].sum=max(t[now<<1].sum,t[now<<1|1].sum);56 }57 58 inline LL query(int now,int l,int r) {59     LL tot=0;60     if(l<=t[now].l&&r>=t[now].r) return t[now].sum;61     int mid=(t[now].l+t[now].r)>>1;62     if(l<=mid) tot=max(tot,query(now<<1,l,r));63     if(r>mid) tot=max(tot,query(now<<1|1,l,r));64     return tot;65 }66 67 int hh() {68     char c;69     scanf("%d%d",&m,&d);70     for(int i=1;i<=m;++i) {71         cin>>c;s[i]=c;72         if(c==‘A‘) ++n;73         read(a[i]);74     }75     build_tree(1,1,n);76     int k=0;77     LL t=0;78     for(int i=1;i<=m;++i) {79         if(s[i]==‘A‘) {80             ++k;81             modify(1,k,(a[i]+t)%d);82         }83         else {84             t=query(1,k-a[i]+1,k);85             printf("%lld\n",t);86         }87     }88     return 0;89 }90 91 int sb=hh();92 int main() {;}
代碼

 

[JSOI2008]最大數 --線段樹

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.