Another LCIS
Time Limit: 1000 ms Memory Limit: 65536 kB Solved: 193 Tried: 2428
Description
For a sequence S1,S2,...,SN, and a pair of integers (i, j), if 1 <= i <= j <= N and Si < Si+1 < Si+2 <...< Sj-1 < Sj, then the sequence Si,Si+1,...,Sj is a CIS (Continuous Increasing Subsequence). The longest CIS of a sequence is called the LCIS (Longest Continuous Increasing Subsequence).In this problem, we will give you a sequence first, and then some “add” operations and some “query” operations. An add operation adds a value to each member in a specified interval. For a query operation, you should output the length of the LCIS of a specified interval.
Input
The first line of the input is an integer T, which stands for the number of test cases you need to solve.Every test case begins with two integers N, Q, where N is the size of the sequence, and Q is the number of queries. S1,S2,...,SN are specified on the next line, and then Q queries follow. Every query begins with a character ‘a’ or ‘q’. ‘a’ is followed by three integers L, R, V, meaning that add V to members in the interval [L, R] (including L, R), and ‘q’ is followed by two integers L, R, meaning that you should output the length of the LCIS of interval [L, R].
T <= 10;
1 <= N, Q <= 100000;
1 <= L <= R <= N;
-10000 <= S1,S2,...,SN, V <= 10000.
Output
For every test case, you should output "Case #k:" on a single line first, where k indicates the case number and starts at 1. Then for every ‘q’ query, output the answer on a single line. See sample for more details.
Sample Input
1
5 6
0 1 2 3 4
q 1 4
a 1 2 -10
a 1 1 -6
a 5 5 -4
q 2 3
q 4 4
Sample Output
Case #1:421
Source
The 9th UESTC Programming Contest Preliminary
1 #include<stdio.h> 2 #define HH 1 3 struct st 4 { 5 int l,r; 6 int lnum,rnum; 7 int max; 8 int lmax,rmax; 9 int color; 10 int num; 11 } f[100002*4]; 12 int date[100002]; 13 int max(int x,int y) 14 { 15 if(x>y) 16 return x; 17 else return y; 18 } 19 int min(int x,int y) 20 { 21 if(x<y) 22 return x; 23 else return y; 24 } 25 int Max(int x,int y,int z,int t,int b) 26 { 27 return max(max(max(x,y),z),max(t,b)); 28 } 29 void up(struct st *fa,struct st *lll,struct st *rrr) 30 { 31 fa->lnum=lll->lnum; 32 fa->rnum=rrr->rnum; 33 if(lll->rnum>=rrr->lnum) 34 { 35 fa->lmax=lll->lmax; 36 fa->rmax=rrr->rmax; 37 fa->max=max(lll->max,rrr->max); 38 } 39 else if(lll->rnum<rrr->lnum) 40 { 41 fa->lmax=(lll->lmax==(lll->r-lll->l+1))? lll->lmax+rrr->lmax:lll->lmax; 42 fa->rmax=(rrr->rmax==(rrr->r-rrr->l+1))? rrr->rmax+lll->rmax:rrr->rmax; 43 fa->max=Max(fa->lmax,fa->rmax,lll->max,rrr->max,lll->rmax+rrr->lmax); 44 } 45 } 46 void down(int n) 47 { 48 if(f[n*2].color==HH) 49 f[n*2].num+=f[n].num; 50 else f[n*2].num=f[n].num; 51 f[n*2].lnum+=f[n].num; 52 f[n*2].rnum+=f[n].num; 53 54 if(f[n*2+1].color==HH) 55 f[n*2+1].num+=f[n].num; 56 else f[n*2+1].num=f[n].num; 57 f[n*2+1].lnum+=f[n].num; 58 f[n*2+1].rnum+=f[n].num; 59 60 f[n*2].color=HH; 61 f[n*2+1].color=HH; 62 63 f[n].color=0; 64 f[n].num=0; 65 } 66 void build(int l,int r,int n) 67 { 68 int mid=(l+r)/2; 69 f[n].l=l; 70 f[n].r=r; 71 f[n].color=0; 72 f[n].num=0; 73 if(l==r) 74 { 75 f[n].lmax=1; 76 f[n].rmax=1; 77 f[n].max=1; 78 f[n].lnum=date[l]; 79 f[n].rnum=date[l]; 80 return ; 81 } 82 build(l,mid,n*2); 83 build(mid+1,r,n*2+1); 84 up(&f[n],&f[n*2],&f[n*2+1]); 85 } 86 void update(int l,int r,int num,int n) 87 { 88 int mid=(f[n].l+f[n].r)/2; 89 if(f[n].l==l&&f[n].r==r) 90 { 91 if(f[n].color==HH) 92 f[n].num=f[n].num+num; 93 else f[n].num=num; 94 f[n].color=HH; 95 f[n].lnum+=num; 96 f[n].rnum+=num; 97 return ; 98 } 99 if(f[n].color==HH) 100 down(n); 101 if(mid>=r) 102 update(l,r,num,n*2); 103 else if(mid<l) 104 update(l,r,num,n*2+1); 105 else 106 { 107 update(l,mid,num,n*2); 108 update(mid+1,r,num,n*2+1); 109 } 110 up(&f[n],&f[n*2],&f[n*2+1]); 111 } 112 int query(int l,int r,int n) 113 { 114 int mid=(f[n].l+f[n].r)/2; 115 int a=0,b=0,ans=0; 116 if(f[n].l==l&&f[n].r==r) 117 { 118 return f[n].max; 119 } 120 if(f[n].color==HH) 121 down(n); 122 if(mid>=r) 123 return query(l,r,n*2); 124 else if(mid<l) 125 return query(l,r,n*2+1); 126 a=query(l,mid,n*2); 127 b=query(mid+1,r,n*2+1); 128 if(f[n*2].rnum>=f[n*2+1].lnum) 129 ans=max(a,b); 130 else if(f[n*2].rnum<f[n*2+1].lnum) 131 { 132 ans=max(max(a,b),min(mid-l+1,f[n*2].rmax)+min(r-mid,f[n*2+1].lmax)); 133 } 134 return ans; 135 } 136 int main() 137 { 138 int i,j,k,n,m,l,r,num,t; 139 char c[5]; 140 while(scanf("%d",&t)>0) 141 { 142 for(i=1; i<=t; i++) 143 { 144 scanf("%d%d",&n,&m); 145 for(j=1; j<=n; j++) 146 scanf("%d",&date[j]); 147 build(1,n,1); 148 printf("Case #%d:\n",i); 149 getchar(); 150 for(j=1; j<=m; j++) 151 { 152 scanf("%s",c); 153 if(c[0]=='q') 154 { 155 scanf("%d%d",&l,&r); 156 k=query(l,r,1); 157 printf("%d\n",k); 158 } 159 else if(c[0]=='a') 160 { 161 scanf("%d%d%d",&l,&r,&num); 162 update(l,r,num,1); 163 } 164 } 165 } 166 } 167 return 0; 168 }