bzoj3343 the magic of the bishop

Source: Internet
Author: User
Tags sort time limit

3343: The magic of the guruTime Limit:10 Sec Memory limit:256 MB
submit:446 solved:198
[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 code of the interval, but we can be considered as a separate increase in the height of the hero of the L (R)) CYZ, light elder brother and Zjq and other people are not religious owners of evil, so they sometimes ask the WD closed interval [L, R] How many heroes height is greater than or equal to C, to verify that 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, and at this time [1, 5] There were 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"
For 30% of the data, n≤1000,q≤1000.
For 100% of the data, n≤1000000,q≤3000,1≤w≤1000,1≤c≤1,000,000,000.

Can be saved after the original sequence to save a structure, save the series and serial number, and then block in the structure of the mess ... In particular, in the modification of the ordinal sort modification, the query on the weight of the order modification, time complexity is O (q*sqrt (n) *log (sqrt (n)). However, this practice will have a lot of repetitive sort ... May be t ... There are better ways to sort the weights after each modification, rather than sorting them at the time of the lookup.

Code:

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath
> #include <algorithm> #define N 1000010 #define K 2001 using namespace Std; struct point{int xu,v;}
B[n];
int a[n],add[k];
int n,q,k;
	inline int in () {int x=0; char Ch=getchar ();
	while (ch< ' 0 ' | | ch> ' 9 ') Ch=getchar ();
	while (ch>= ' 0 ' && ch<= ' 9 ') x=x*10+ch-' 0 ', Ch=getchar ();
return x; } inline bool Cmpxu (point X,point y) {return x.xu<y.xu;}-inline bool Cmpv (point X,point y) {return x.v<y.v;} INL
	ine int Erfen (int i,int c) {int l= (i-1) *k+1,r=i*k,m,s;
	Sort (B+L,B+R+1,CMPV);
		while (l<=r) {m= (l+r) >>1;
		if (B[m].v==c-add[i]) break;
		else if (b[m].v<c-add[i]) l=m+1;
	else r=m-1;
	} if (B[m].v>=c-add[i]) s=i*k-m+1;
	else s=i*k-m;
return s;
	} int main () {freopen ("cin.in", "R", stdin);
	Freopen ("Cin.out", "w", stdout);
	N=in (), Q=in (), K=SQRT (n);
	for (int i=1; i<=n; i++) A[i]=in (), b[i].v=a[i],b[i].xu=i; while (q--) {CHar Opt=getchar ();
		int l,r,c,ll,rr,l,r;
		while (opt< ' A ' | | opt> ' Z ') Opt=getchar ();
		L=in (), R=in (), C=in (); Ll= (l/k) + (l%k!=0 1:0);
		Rr= (r/k) + (r%k!=0 1:0); l=ll+ (k* (LL-1) +1==l 0:1);
		R=rr+ (r%k==0 0:-1);
				if (opt== ' M ') {if (l<=r) {for (int i=l; i<=r; i++) add[i]+=c;
					if (ll<l) {sort (b + (LL-1) *k+1,b+ll*k+1,cmpxu);
				for (int i=l; i<=ll*k; i++) a[i]+=c,b[i].v+=c;
					} if (Rr>r) {sort (b + (rr-1) *k+1,b+rr*k+1,cmpxu);
				for (int i=r*k+1; i<=r; i++) a[i]+=c,b[i].v+=c;
				}} else {sort (b + (LL-1) *k+1,b+rr*k+1,cmpxu);
			for (int i=l; i<=r; i++) a[i]+=c,b[i].v+=c;
			}} else {int ans=0;
				if (l<=r) {for (int i=l; i<=r; i++) Ans+=erfen (I,C);
				if (ll<l) {for (int i=l; i<=ll*k; i++) if (A[i]>=c-add[ll]) ans++;
				} if (Rr>r) {for (int i=r*k+1; i<=r; i++) if (A[I]&GT;=C-ADD[RR]) ans++; }} else {if (LL==RR) {for (iNT I=l; i<=r;
				i++) if (A[i]>=c-add[ll]) ans++;
					} else {for (int i=l; i<=ll*k; i++) if (A[i]>=c-add[ll]) ans++;
				for (int i=ll*k+1; i<=r; i++) if (A[I]&GT;=C-ADD[RR]) ans++;
		}} printf ("%d\n", ans);
}} return 0; }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.