bzoj1012: [JSOI2008]最大数maxnumber
题解:线段树入门,数据范围需要注意;
线段树的常数太大;
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 #include<cstdlib> 6 #include<ctime> 7 #include<vector> 8 #include<algorithm> 9 #include<queue> 10 #include<map> 11 #include<vector> 12 #include<cmath> 13 using namespace std; 14 #define LL long long 15 #define up(i,j,n) for(int i=j;i<=n;i++) 16 #define down(i,n,j) for(int i=n;i>=j;i--) 17 const int maxn=200020; 18 int m,n=200000; 19 unsigned LL mod,a[maxn],c[maxn<<2],last=0; 20 int x,y; 21 LL find(int left,int right,int root){ 22 if(left>y||right<x)return -1; 23 if(left>=x&&right<=y)return c[root]; 24 int mid=(left+right)>>1; 25 return max(find(left,mid,root<<1),find(mid+1,right,root<<1|1)); 26 } 27 void change(int left,int right,int root){ 28 if(left>x||right<x)return; 29 if(left==x&&right==x){c[root]=y;return;} 30 int mid=(left+right)>>1; 31 change(left,mid,root<<1); 32 change(mid+1,right,root<<1|1); 33 c[root]=c[root<<1]; 34 if(c[root<<1|1]>c[root])c[root]=c[root<<1|1]; 35 } 36 int main(){ 37 scanf("%d%lld",&m,&mod); 38 int top=0;char ch; 39 LL now,ans; 40 while(m--){ 41 scanf(" %c %lld",&ch,&now); 42 if(ch=='A'){ 43 top++;x=top;y=(now+last)%mod; 44 change(1,n,1); 45 } 46 if(ch=='Q'){ 47 y=top,x=top-now+1; 48 if(x>y){printf("0\n");continue;} 49 ans=find(1,n,1); 50 last=ans; 51 printf("%lld\n",last); 52 } 53 } 54 return 0; 55 }