3343: The magic of the guruTime Limit:10 Sec Memory limit:256 MB
submit:631 solved:272
[Submit] [Status] [Discuss]
DescriptionThe leader has recently learned a magical magic that can make a person grow taller. So he was ready to demonstrate to each hero of the Xmyz Information Group. So the N heroes gathered together again, this time they were in a row numbered 1, 2 、......、 N. Each person's height begins with a positive integer that does not exceed 1000. The Master's magic can add an integer W to each hero's height in the closed interval [L, R] (1≤l≤r≤n). (Although l= R does not conform to the written specification of the interval, but we can think of it as a separate increase in the height of the hero of the L (R)) CYZ, light elder brother and Zjq and other people who are not religious, so they sometimes ask the WD closed interval [L, R] How many heroes height is greater than or equal to C, to verify WD was lazy, so he gave you the task of answering.
Input1th Act Two integers N, Q. Q is the sum of the number of questions and the number of spells cast. The 2nd line has n positive integers, and the number of I represents the height of the hero of the first I. 3rd to q+2 each line has an operation: (1) If the first letter is "M", then there are three numbers L, R, W. Represents the height of all heroes in the closed interval [L, R] plus W. (2) If the first letter is "A", then there are three numbers L, R, C. Ask how many heroes in the closed interval [L, R] are taller than or equal to C.
OutputFor each "A" query, the output line contains only one integer, representing the number of heroes in the closed interval [L, R] that are greater than or equal to C.
Sample Input5 3
1 2 3) 4 5
A 1 5 4
M 3 5 1
A 1 5 4
Sample Output2
3
HINT
Input and Output sample description
The original 5 heroes were 1, 2, 3, 4, 5, when [1, 5] had 2 heroes taller than or equal to 4. The master cast to 1, 2, 4, 5, 6, at this time [1, 5] There are 3 heroes taller than or equal to 4.
"data range"
30% data, n≤1000, q≤1000.
Data for 100%, n≤1000000, q≤3000,1≤w≤1000,1≤c≤1,000,000,000. Open an array b b in each block of elements ordered every time Updata: the whole block tag record, small block of violence modified to query: Small block of violence, large chunks of two.
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <cmath> 5 6 #define
MAXN 1000001 7 8 using namespace Std;
9 int n,a[maxn],team[maxn],b[maxn],str[maxn],end[maxn],tag[1001]; Each of the inline int in () is {X=0;char int ' () Ch=getchar (); while (ch< ' 0 ' | |
Ch> ' 9 ') Ch=getchar ();
(ch<= ' 9 ' &&ch>= ' 0 ') x=x*10+ch-' 0 ', Ch=getchar ();
return x; + void Pre () {25 int t=sqrt (n), CNT; i=1;i<=n;i++ (int.) team[i]= (i-1)/t+1;
Cnt=team[n];
(int i=1;i<=n;i++) end[team[i]]=i,b[i]=a[i];
Str[team[i]]=i for (int i=n;i>=1;i--);
(int i=1;i<=cnt;i++) (str[i]+b,b+end[i]+1); + void Add (int l,int r,int c) {a[i]+=c if (Team[l]==team[r]) {36 for (Int. i=l;i<=r;i++
for (int i=str[team[l]];i<=end[team[r]];i++) b[i]=a[i];
Panax Notoginseng Sort (str[team[l]]+b,end[team[l]]+b);
(a) return; 39 } for (int i=l;i<=end[team[l]];i++) a[i]+=c;
(int j=str[team[r]];j<=r;j++) a[j]+=c;
(int i=str[team[l]];i<=end[team[l]];i++) b[i]=a[i];
(int i=str[team[r]];i<=end[team[r]];i++) b[i]=a[i];
(str[team[l]]+b,end[team[l]]+b+1);
(str[team[r]]+b,end[team[r]]+b+1);
i=team[l]+1;i<=team[r]-1;i++ for (int) tag[i]+=c; binary_search Int (int pos,int c) (int.): L=str[pos],r=end[pos (b[r]<c) return r+1; 56 while (L<=r), {mid= int (l+r) >>1; (b[mid]<c) l=mid+1; or else r=mid-1; 61
} return L; (int l,int r,int c), (int.) int ans=0, (Team[l]==team[r]) {-(int i=l;i<=
r;i++) if (a[i]+tag[team[l]]>=c) ans++;
+--return ans;
The i=l;i<=end[team[l]];i++ (int) for (int.) A[i]+tag[team[l]]>=c ans++; J=str[team[r for (int]];j<=r;j++) if (a[j]+tag[team[r]]>=c) ans++;
(int i=team[l]+1;i<=team[r]-1;i++) Ans+=end[i]-binary_search (i,c-tag[i]) +1;
return ans; ---------Bayi int main ()---------------q,u,v,c;
In ();
The pre (); i=1;i<=q;i++ (int) (int.) sb=0 (sb!= ' A ' &&sb!= ' M ') Sb=getchar (); 92
U=in (), V=in (), C=in ();
if (sb== ' A ') printf ("%d\n", Query (U,v,c));
94 Else Add (U,V,C);
0; 97}
View Code