1 #include<cstdio>
  2 #define MAXN 100010
  3 struct node
  4 {
  5     int left,right,res,add,x,y;
  6 };
  7 node tree[MAXN<<2];
  8 int n;
  9 inline int MAX(int x,int y)
 10 {
 11     return x>y?x:y;
 12 }
 13 inline int MIN(int x,int y)
 14 {
 15     return x>y?y:x;
 16 }
 17 inline void PushUp(int mid,int L,int R,int rt)
 18 {
 19     tree[rt].x=tree[rt<<1].x;
 20     tree[rt].y=tree[rt<<1|1].y;
 21     tree[rt].left=tree[rt<<1].left;
 22     tree[rt].right=tree[rt<<1|1].right;
 23     tree[rt].res=MAX(tree[rt<<1].res,tree[rt<<1|1].res);
 24     if(tree[rt<<1].y<tree[rt<<1|1].x)
 25     {
 26         if(tree[rt].left==mid-L+1)
 27             tree[rt].left+=tree[rt<<1|1].left;
 28         if(tree[rt].right==R-mid)
 29             tree[rt].right+=tree[rt<<1].right;
 30         tree[rt].res=MAX(tree[rt].res,tree[rt<<1].right+tree[rt<<1|1].left);
 31     }
 32 }
 33 inline void PushDown(int L,int R,int rt)
 34 {
 35     if(tree[rt].add)
 36     {
 37         tree[rt<<1].add+=tree[rt].add;
 38         tree[rt<<1|1].add+=tree[rt].add;
 39         tree[rt<<1].x+=tree[rt].add;
 40         tree[rt<<1].y+=tree[rt].add;
 41         tree[rt<<1|1].x+=tree[rt].add;
 42         tree[rt<<1|1].y+=tree[rt].add;
 43         tree[rt].add=0;
 44     }
 45 }
 46 void Build(int L,int R,int rt)
 47 {
 48     tree[rt].add=0;
 49     if(L==R)
 50     {
 51         scanf("%d",&tree[rt].x);
 52         tree[rt].y=tree[rt].x;
 53         tree[rt].left=tree[rt].right=tree[rt].res=1;
 54     }
 55     else
 56     {
 57         int mid=(L+R)>>1;
 58         Build(L,mid,rt<<1);
 59         Build(mid+1,R,rt<<1|1);
 60         PushUp(mid,L,R,rt);
 61     }
 62 }
 63 void Update(int x,int y,int val,int L,int R,int rt)
 64 {
 65     if(x<=L&&R<=y)
 66     {
 67         tree[rt].add+=val;
 68         tree[rt].x+=val;
 69         tree[rt].y+=val;
 70     }
 71     else
 72     {
 73         int mid=(L+R)>>1;
 74         PushDown(L,R,rt);
 75         if(mid>=x)
 76             Update(x,y,val,L,mid,rt<<1);
 77         if(y>mid)
 78             Update(x,y,val,mid+1,R,rt<<1|1);
 79         PushUp(mid,L,R,rt);
 80     }
 81 }
 82 int Query(int x,int y,int L,int R,int rt)
 83 {
 84     if(x<=L&&R<=y)
 85         return tree[rt].res;
 86     int mid=(L+R)>>1,ans=0;
 87     PushDown(L,R,rt);
 88     if(mid>=x)
 89         ans=MAX(ans,Query(x,y,L,mid,rt<<1));
 90     if(y>mid)
 91         ans=MAX(ans,Query(x,y,mid+1,R,rt<<1|1));
 92     if(tree[rt<<1].y<tree[rt<<1|1].x)
 93         ans=MAX(ans,MIN(mid-x+1,tree[rt<<1].right)+MIN(y-mid,tree[rt<<1|1].left));
 94     return ans;
 95 }
 96 int main()
 97 {
 98     char ch;
 99     int c,m,x,y,v,ca=1;
100     scanf("%d",&c);
101     while(c--)
102     {
103         scanf("%d%d",&n,&m);
104         Build(1,n,1);
105         printf("Case #%d:\n",ca++);
106         while(m--)
107         {
108             scanf(" %c",&ch);
109             if(ch=='a')
110             {
111                 scanf("%d%d%d",&x,&y,&v);
112                 Update(x,y,v,1,n,1);
113             }
114             else
115             {
116                 scanf("%d%d",&x,&y);
117                 printf("%d\n",Query(x,y,1,n,1));
118             }
119         }
120     }
121     return 0;
122 }
posted on 2012-05-24 17:04  DrunBee  阅读(596)  评论(1编辑  收藏  举报