#include <cstdio> #include <cstring> #include <cstdlib> #include <climits> using namespace std; const int M=200001; const int L=INT_MAX; const int P=INT_MIN; int m,d,q[M][2],qa; struct SegmentTree { int l,r,mx; }tr[M<<2]; int lst; inline int read(void) { int s=0,f=1; char c=getchar(); for (;c<'0'||c>'9';c=getchar()) if (c=='-') f=-1; for (;'0'<=c&&c<='9';c=getchar()) s=(s<<1)+(s<<3)+c-'0'; return s*f; } void build(int now,int l,int r) { tr[now].l=l; tr[now].r=r; tr[now].mx=L; if (l^r) { int mid=l+r>>1; build(now<<1,l,mid); build(now<<1|1,mid+1,r); } } inline int max(int i,int j) { return i>j?i:j; } void ins(int now,int loc,int w) { if (tr[now].l==tr[now].r) {tr[now].mx=w;return;} int mid=tr[now].l+tr[now].r>>1; ins(now<<1|(loc<=mid?0:1),loc,w); tr[now].mx=max(tr[now<<1].mx,tr[now<<1|1].mx); } int query(int now,int l,int r) { if (l<=tr[now].l&&tr[now].r<=r) return tr[now].mx; int mx=P,mid=tr[now].l+tr[now].r>>1; if (l<=mid) mx=max(mx,query(now<<1,l,r)); if (mid<r) mx=max(mx,query(now<<1|1,l,r)); return mx; } int main(void) { char c; m=read(),d=read(); for (int i=1;i<=m;i++) { scanf("\n%c",&c); if (c=='A') qa++; else q[i][0]=1; q[i][1]=read(); } build(1,1,qa),qa=0; for (int i=1;i<=m;i++) if (!q[i][0]) ins(1,++qa,(lst+q[i][1])%d); else printf("%d\n",lst=query(1,qa-q[i][1]+1,qa)); return 0; }
#include <cstdio> #include <cstring> #include <cstdlib> using namespace std; const int M=200010; int m,d,lst; struct Q { int w,loc; }q[M]; int h,t,ca; inline int read(void) { int s=0,f=1; char c=getchar(); for (;c<'0'||c>'9';c=getchar()) if (c=='-') f=-1; for (;'0'<=c&&c<='9';c=getchar()) s=(s<<1)+(s<<3)+c-'0'; return s*f; } int main(void) { char c; int x; m=read(),d=read(),h=1; for (int i=1;i<=m;i++) { scanf("\n%c",&c),x=read(); if (c=='A') { x=(x+lst)%d,ca++; for (;h<=t&&x>=q[t].w;t--); q[++t].w=x,q[t].loc=ca; } else { int l=h,r=t,mid; for (;l<=r;) { mid=l+r>>1; ca-q[mid].loc+1<=x?r=mid-1:l=mid+1; } printf("%d\n",lst=q[l].w); } } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/u013598409/article/details/47010695