3343: the master's magic time limit: 10 sec memory limit: 256 MB
Submit: 319 solved: 145
[Submit] [Status] The description 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. Therefore
NThe heroes once again gathered together. 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 instructor's magic can be closed every time [
L,
R] (1 ≤
L≤
R≤
N) And add an integer to the height of the hero.
W. (Although
L=
RIt does not conform to the writing rules of the interval, but we can consider it to be an additional
L(
R) Height of a hero) cyz, Guang Ge, zjq, and others do not believe in the teachings of the Lord, so they sometimes ask WD to close the interval [
L,
R] How many heroes are equal to or higher
CTo verify whether the magic of the instructor is valid. WD is very lazy, so he handed you the answer task. Input 1st two integers
N,
Q.
QThis is the sum of the number of questions and the number of lectures delivered by the instructors. 2nd rows have
NPositive integers
INumber indicates the nth
IHero height. 3rd
Q+ There is an operation for each row in two rows: (1) if the first letter is "M", then there are three numbers.
L,
R,
W. Indicates the closed interval [
L,
R] Height of all heroes plus
W. (2) If the first letter is "A", then there are three numbers.
L,
R,
C. Ask for a closed interval [
L,
R] How many heroes are equal to or higher in height?
C. Output asks each "a" to output a row, containing only one integer, indicating the closed interval [
L,
R] Height greater than or equal
CNumber of heroes. 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]
In the past, five heroes were 1, 2, 3, 4, and 5 in height. At this time, two heroes in height were greater than or equal to 4 in [1, 5. The instructor changes to 1, 2, 4, 5, and 6. At this time, the height of three heroes between [1, 5] is greater than or equal to 4.
[Data Scope]
30% of the data,
N≤ 1000,
Q≤ 1000.
100% of the data,
N≤ 1000000,
Q≤ 3000,1 ≤
W≤ 1000,1 ≤
C≤ 1,000,000,000.
Source
Question:
At the beginning, I saw a pleasant question with a code of 100 + sgt. When I wrote it to the last function, I suddenly found that the maintenance could not be done ..... What kind of mentality ......
It's better to use the multipart big method...
Large parts are directly added to the note, and small pieces of violence
Code:
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set>10 #include<queue>11 #include<string>12 #define inf 100000000013 #define maxn 1000000+10014 #define maxm 1000+10015 #define eps 1e-1016 #define ll long long17 #define pa pair<int,int>18 using namespace std;19 inline int read()20 {21 int x=0,f=1;char ch=getchar();22 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}23 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}24 return x*f;25 }26 int n,m,block,a[maxn],b[maxn],pos[maxn],add[maxm];27 inline void reset(int x)28 {29 int l=(x-1)*block+1,r=min(x*block,n);30 for(int i=l;i<=r;i++)b[i]=a[i];31 sort(b+l,b+r+1);32 }33 int find(int x,int z)34 {35 int l=(x-1)*block+1,r=x*block,mid;36 while(l<=r)37 {38 mid=(l+r)>>1;39 if(b[mid]>=z)r=mid-1;else l=mid+1;40 }41 return x*block-l+1;42 }43 void change(int x,int y,int z)44 {45 int bx=pos[x],by=pos[y];46 if(by-bx<=1){for(int i=x;i<=y;i++)a[i]+=z;}47 else48 {49 for(int i=x;i<=bx*block;i++)a[i]+=z;50 for(int i=bx+1;i<by;i++)add[i]+=z;51 for(int i=(by-1)*block+1;i<=y;i++)a[i]+=z;52 } 53 reset(bx);reset(by); 54 }55 int query(int x,int y,int z)56 {57 int bx=pos[x],by=pos[y],sum=0;58 if(by-bx<=1){for(int i=x;i<=y;i++)if(a[i]+add[pos[i]]>=z)sum++;}59 else60 {61 for(int i=x;i<=bx*block;i++)if(a[i]+add[bx]>=z)sum++;62 for(int i=(by-1)*block+1;i<=y;i++)if(a[i]+add[by]>=z)sum++;63 }64 for(int i=bx+1;i<by;i++)sum+=find(i,z-add[i]);65 return sum; 66 }67 int main()68 {69 freopen("input.txt","r",stdin);70 freopen("output.txt","w",stdout);71 n=read();m=read();72 block=floor(sqrt(n));73 for(int i=1;i<=n;i++)74 {75 a[i]=read();76 pos[i]=(i-1)/block+1;77 }78 for(int i=1;i<=pos[n];i++)reset(i);79 char ch;int x,y,z;80 while(m--)81 {82 ch=‘ ‘;83 while(ch!=‘A‘&&ch!=‘M‘)ch=getchar();x=read();y=read();z=read();84 if(ch==‘M‘)change(x,y,z);else printf("%d\n",query(x,y,z));85 } 86 return 0;87 }
View code
Bzoj3343: the magic of the instructor