BZOJ 1012 洛谷1198 最大数 maxnumber

用线段数维护即可

 1 #include<cstdio>
 2 #include<algorithm>
 3 #define ls (cur<<1)
 4 #define rs (cur<<1|1)
 5 #define mid ((a[cur].l+a[cur].r)>>1)
 6 using namespace std;
 7 const int maxn=800010;
 8 long long n,m,x,y,z,k,d;
 9 struct tree{
10     int l,r;
11     long long max;
12 }a[maxn];
13 void read(long long &k){
14     k=0; int f=1; char c=getchar();
15     while (c<'0'||c>'9')c=='-'&&(f=-1),c=getchar();
16     while ('0'<=c&&c<='9')k=k*10+c-'0',c=getchar();
17     k*=f;
18 }
19 void build(int cur,int l,int r){
20     a[cur].l=l; a[cur].r=r;
21     if (l<r){
22         build(ls,l,mid);
23         build(rs,mid+1,r);
24     }
25 }
26 void add(int cur,long long pos,long long del){
27     if (a[cur].l==pos&&a[cur].r==pos) a[cur].max=(a[cur].max+del)%d;
28     else{
29         if (pos<=mid) add(ls,pos,del);
30         else add(rs,pos,del);
31         a[cur].max=max(a[ls].max,a[rs].max);
32     }
33 }
34 long long query(int cur,int l,int r){
35     if (l<=a[cur].l&&a[cur].r<=r) return a[cur].max;
36     else{
37         long long ret=-0X7f7f7f7f;
38         if (l<=mid) ret=max(ret,query(ls,l,r));
39         if (r>mid) ret=max(ret,query(rs,l,r));
40         return ret;
41     }
42 }
43 int main(){
44     read(m);
45     build(1,1,m);
46     read(d);
47     long long t=0;
48     for (int i=1;i<=m;i++){
49         char c=getchar();
50         while(c!='A'&&c!='Q') c=getchar();
51         if (c=='A'){
52             n++;
53             read(x);
54             add(1,n,x+t);
55         }
56         else{
57             read(x);
58             t=query(1,n-x+1,n);
59             printf("%lld\n",t);
60         }
61     }
62     return 0;
63 }
View Code

 

posted @ 2017-10-19 07:19  Driver_Lao  阅读(118)  评论(0编辑  收藏  举报