Title Description
Now request you to maintain a number of columns, it requires the following two actions:
1, the query operation.
grammar:Q L
Function: the maximum number of queries in the end of the number of columns in the current number L, and outputs the value of this number.
Restrictions: L L does not exceed the length of the current sequence. (L> 0) ( L > 0 )
2, the insertion operation.
grammar:A n
Function: n- n-plus T T, where T T is the answer to the last query operation (if not already performed a query operation, then T = 0 T = 0), and the result of a fixed constant D D taken die, the resultant answer inserted at the end of the number of columns.
Restrictions: n- n-integer (possibly negative) and in the entire length range.
Note: The number of columns is initially empty, not a number.
Input and output formats
Input formats:
The first line of two integers, M M and D D, where M M represents the number of operations (M \ Le 200,000) ( M ≤ 2 0 0 , 0 0 0 ), D D described hereinabove, satisfying (0 < D <2,000,000,000) ( 0 < D < 2 , 0 0 0 , 0 0 0 , 0 0 0 )
The next M M rows, each row a string describing a specific operation. Syntax as described above.
Output formats:
For each query, you should turn the output in the order, and each result row.
Topics requirements too straight ball
#include <bits/stdc++.h> #define ll long long using namespace std; const int maxn=2e5+5; int n; ll a[maxn],d; struct node { int l,r; ll id; }t[maxn*4]; void build(int p,int l,int r) { t[p].l=l,t[p].r=r; if(l==r) { t[p].id=a[l]; return; } int mid=(l+r)>>1; build(p*2,l,mid); build(p*2+1,mid+1,r); t[p].id=max(t[p*2].id,t[p*2+1].id); } void change(int p,int x,ll v,int l,int r) { if(l==r) { t[p].id=v; return; } int mid=(l+r)>>1; if(x<=mid) change(p*2,x,v,l,mid); else change(p*2+1,x,v,mid+1,r); t[p].id=max(t[p*2].id,t[p*2+1].id); } ll ask(int l0,int r0,int p,int l,int r) { if(l<=l0&&r>=r0) { return t[p].id; } int mid=(l0+r0)>>1; ll val=-(1<<30); if(mid>=l) val=max(val,ask(l0,mid,p*2,l,r)); if(mid<r) val=max(val,ask(mid+1,r0,p*2+1,l,r)); return val; } int main() { scanf("%d %lld",&n,&d); int len=0; ll t=0; for(int i=1;i<=n;i++) { char ch; ll x; scanf(" %c %lld",&ch,&x); //cout<<ch<<" "<<x<<endl; if(ch=='A') { change(1,++len,(x+t)%d,1,n); } else { if(x==0) t=0; else t=ask(1,n,1,len-x+1,len); printf("%lld\n",t); } } return 0; }