【bzoj 1012】 Maximum number maxnumber 【JSOI2008】

Description

  Now I ask you to maintain an array and provide the following two operations: 1. Query operation. Syntax: QL Function: Query
the largest number among the last L numbers in the current sequence, and output the value of this number. Restriction: L does not exceed the length of the current sequence. 2. Insert operation. Syntax: A n Function:
Add t, where t is the answer of the most recent query operation (if the query operation has not been performed, then t=0), and the result is
modulo a fixed constant D, the The resulting answer is inserted at the end of the sequence. Restrictions: n is a non-negative integer and is in the range of long integers. Note: Initially, the number column is empty, without a
number.

Input

  The first line contains two integers, M and D, where M represents the number of operations (M <= 200,000), and D, as described above, satisfies D within longint. Next
M lines, query operation or insert operation.

Output

  For each query operation, output one line. The row has only one number, the maximum number of the last L numbers in the sequence.

Sample Input

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

Sample Output

96
93
96

This problem can use line segment tree or monotonic queue, the following is the program:

Segment tree:

[cpp]  view plain copy  
  1. #include<stdio.h>  
  2. #include<iostream>  
  3. usingnamespace std;   
  4. constint N=800005;   
  5. struct tree{  
  6.     int lc,rc,key;  
  7. }t[N];  
  8. int k;  
  9. void build(int rt,int l,int r){  
  10.     if(l==r){  
  11.         t[rt].key=t[rt].lc=t[rt].rc=0;  
  12.         return;  
  13.     }  
  14.     int m=(l+r)>>1;  
  15.     t[rt].lc=++k;  
  16.     t[rt].rc=++k;  
  17.     build(t[rt].lc,l,m);  
  18.     build(t[rt].rc,m+1,r);  
  19.     t[rt].key=max(t[t[rt].lc].key,t[t[rt].rc].key);  
  20. }  
  21. void add(int rt,int l,int r,int k,int w){  
  22.     if(l==r&&l==k){  
  23.         t[rt].key=w;  
  24.         return;  
  25.     }  
  26.     int m=(l+r)>>1;  
  27.     if(k<=m){  
  28.         add(t[rt].lc,l,m,k,w);  
  29.     }  
  30.     else{  
  31.         add(t[rt].rc,m+1,r,k,w);  
  32.     }  
  33.     t[rt].key=max(t[t[rt].lc].key,t[t[rt].rc].key);  
  34. }  
  35. int ask(int rt,int l,int r,int a,int b){  
  36.     if(l==a&&r==b){  
  37.         return t[rt].key;  
  38.     }  
  39.     int m=(l+r)>>1;  
  40.     if(b<=m){  
  41.         return ask(t[rt].lc,l,m,a,b);  
  42.     }  
  43.     if(a>m){  
  44.         return ask(t[rt].rc,m+1,r,a,b);  
  45.     }  
  46.     return max(ask(t[rt].lc,l,m,a,m),ask(t[rt].rc,m+1,r,m+1,b));  
  47. }  
  48. void read(int &s){  
  49.     s=0;  
  50.     int f=1;  
  51.     char c=getchar();  
  52.     while((c<'0'||c>'9')&&c!='-'){  
  53.         c=getchar();  
  54.     }  
  55.     if(c=='-'){  
  56.         f=-1;  
  57.         c=getchar();  
  58.     }  
  59.     while(c>='0'&&c<='9'){  
  60.         s*=10;  
  61.         s+=c-'0';  
  62.         c=getchar();  
  63.     }  
  64.     s*=f;  
  65. }  
  66. void ch(char &c){  
  67.     c=getchar();  
  68.     while(c!='A'&&c!='Q'){  
  69.         c=getchar();  
  70.     }  
  71. }  
  72. int main(){  
  73.     int n,mod,q=0,x,l=0,i;  
  74.     char c;  
  75.     read(n);  
  76.     read(mod);  
  77.     build(0,1,n);  
  78.     for(i=0;i<n;i++){  
  79.         ch(c);  
  80.         read(x);  
  81.         if(c=='A'){  
  82.             add(0,1,n,++l,(q+x)%mod);  
  83.         }  
  84.         else{  
  85.             printf("%d\n",q=ask(0,1,n,l-x+1,l));  
  86.         }  
  87.     }  
  88.     return 0;  
  89. }  

portal

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324667829&siteId=291194637