BZOJ 3343: master magic, bzoj3343 master magic
3343: the magic of the instructor Question connection:
Http://www.lydsy.com/JudgeOnline/problem.php? Id = 3343
Description
The instructor recently learned a magic that can make people grow taller. So he is going to demonstrate it to every hero in the XMYZ Information Group. So N heroes gathered together again. This time they were arranged in a column numbered 1, 2 ,...... , N.
Each person's height is a positive integer up to 1000 at the beginning. The magic of the instructor adds an integer W to the height of the hero in the closed range [L, R] (1 ≤ L ≤ R ≤ N. (Although L = R does not conform to the writing rules of the interval, we can consider it to be a separate increase of the height of the hero (R)
CYZ, Guang Ge, ZJQ, and others do not believe in the evil spirits of the instructors, so they sometimes ask the WD to close the range [L, R] And how many heroes are taller than or equal to C, to verify whether the magic of the instructor is valid.
WD is very lazy, so he handed you the answer task.
Input
1st act as two integers N and Q. Q is the sum of the number of questions and the number of instructors.
Row 2nd has N positive integers, And the number I represents the height of the hero I.
Each row from row 3rd to row Q + 2 has an operation:
(1) If the first letter is "M", then there are three numbers, L, R, and W. Add W to the height of all heroes in the closed range [L, R.
(2) If the first letter is "A", then there are three numbers: L, R, and C. Ask how many heroes in the closed interval [L, R] are equal to or greater than C.
Output
Output A line for each "A" query, containing only one integer, indicating the number of heroes whose height is greater than or equal to C in the closed range [L, R.
Sample Input
5 3
1 2 3 4 5
A 1 5 4
M 3 5 1
A 1 5 4
Sample Output
2
3
Hint
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=1000000+5; 4 5 int block,num,l[maxn],r[maxn]; 6 int belong[maxn],n,m,a[maxn]; 7 int d[maxn],p[maxn]; 8 9 inline void bt() 10 { 11 block=sqrt(n); 12 num=n/block; 13 if(n%block) 14 num++; 15 for(int i=1;i<=num;i++) 16 l[i]=(i-1)*block+1,r[i]=i*block; 17 r[num]=n; 18 19 for(int i=1;i<=n;i++) 20 { 21 belong[i]=(i-1)/block+1; 22 d[i]=a[i]; 23 } 24 for(int i=1;i<=num;i++) 25 sort(d+l[i],d+r[i]+1); 26 } 27 28 inline void update(int ll,int rr,int w) 29 { 30 if(belong[ll]==belong[rr]) 31 { 32 for(int i=l[belong[ll]];i<=r[belong[rr]];i++) 33 { 34 a[i]+=p[belong[ll]]; 35 } 36 p[belong[ll]]=0; 37 for(int i=ll;i<=rr;i++) 38 a[i]+=w; 39 for(int i=l[belong[ll]];i<=r[belong[rr]];i++) 40 d[i]=a[i]; 41 sort(d+l[ll],d+r[rr]+1); 42 return ; 43 } 44 45 for(int i=l[belong[ll]];i<=r[belong[ll]];i++) 46 a[i]+=p[belong[ll]]; 47 p[belong[ll]]=0; 48 for(int i=ll;i<=r[belong[ll]];i++) 49 a[i]+=w; 50 for(int i=l[belong[ll]];i<=r[belong[ll]];i++) 51 d[i]=a[i]; 52 sort(d+l[belong[ll]],d+r[belong[ll]]+1); 53 54 for(int i=l[belong[rr]];i<=r[belong[rr]];i++) 55 a[i]+=p[belong[rr]]; 56 p[belong[rr]]=0; 57 for(int i=l[belong[rr]];i<=rr;i++) 58 a[i]+=w; 59 for(int i=l[belong[rr]];i<=r[belong[rr]];i++) 60 d[i]=a[i]; 61 sort(d+l[belong[rr]],d+r[belong[rr]]+1); 62 63 for(int i=belong[ll]+1;i<belong[rr];i++) 64 p[i]+=w; 65 } 66 67 inline int ask(int x,int y,int w) 68 { 69 int ans=0; 70 if(belong[x]==belong[y]) 71 { 72 for(int i=x;i<=y;i++) 73 if(a[i]+p[belong[i]]>=w) 74 ans++; 75 return ans; 76 } 77 for(int i=x;i<=r[belong[x]];i++) 78 if(a[i]+p[belong[i]]>=w) 79 ans++; 80 81 for(int i=l[belong[y]];i<=y;i++) 82 if(a[i]+p[belong[i]]>=w) 83 ans++; 84 85 for(int i=belong[x]+1;i<belong[y];i++) 86 { 87 int ll=l[i],rr=r[i],Ans=0; 88 while(ll<=rr) 89 { 90 int mid=(ll+rr)>>1; 91 if(d[mid]+p[i]>=w) 92 rr=mid-1,Ans=r[i]-mid+1; 93 else 94 ll=mid+1; 95 } 96 ans+=Ans; 97 } 98 return ans; 99 }100 101 int main()102 {103 scanf("%d%d",&n,&m);104 for(int i=1;i<=n;i++)105 {106 scanf("%d",&a[i]);107 }108 bt();109 for(int i=1;i<=m;i++)110 {111 char ch;112 int l,r,w,c;113 cin>>ch;114 if(ch=='M')115 {116 scanf("%d%d%d",&l,&r,&w);117 update(l,r,w);118 }119 else120 {121 scanf("%d%d%d",&l,&r,&c);122 printf("%d\n",ask(l,r,c));123 }124 }125 return 0;126 }