[BZOJ3343] Magic of the Bishop |

Source: Internet
Author: User

Description
The 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 the magic of the Guru is really effective. WD was lazy, so he gave you the task of answering.  
The chunking algorithm refers to dividing a sequence into sqrt (n) blocks, each of which has exactly sqrt (n) Elements
for this problem, we first divide each number into a block in order to maintain the monotonicity within each blockfor a M operation, we put the block in the middle of the l,r as a marker for addition, for the direct violence at both endsthe entire block is not more than sqrt (n), and neither of the two is more than sqrt (n), so the time complexity is O (sqrt (n) )For a operation, the violence of the part is easy to count, for the whole block because we have maintained a monotonic, direct binary search can bebut don't ignore the previous add tag.    
1 {2 Chunk.3 }4 5 Program bzoj3343;6 Constmaxn=1000010; maxm=1010;7 varN,q,m,block,x,y,z,i:longint;8Ch:Char;9a,b,pos:array[-1.. Maxn]of Longint;Tenadd:array[-1.. Maxm]of Longint; One  A function min (a,b:longint): Longint; - begin -     ifA<b then exit (a)Elseexit (b); the end; -  - procedure Qsort (l,r:longint); - varI,j,mid:longint; + begin -I:=l;j:=r;mid:=b[random (r-l+1)+L]; + Repeat A          while(I<r) and (B[i]>mid) DoInc (i); at          while(L<j) and (B[j]<mid) DoDec (j); -         ifi<=J Then - begin -b[0]:=b[i];b[i]:=b[j];b[j]:=b[0]; - Inc (I);d EC (j); - end; inUntil i>J; -     ifi<R then Qsort (i,r); to     ifl<J then Qsort (l,j); + end; -  theProcedureNew(p:longint); * varL,r,i:longint; $ beginPanax NotoginsengL:= (P-1) *block+1; R:=min (p*block,n); -      forI:=l to R Dob[i]:=A[i]; the qsort (l,r); + end; A  the Procedure Update (x,y,z:longint); + varI:longint; - begin $     ifpos[x]=Pos[y] Then $          forI:=x to Y DoInc (A[I],Z)Else - begin -          forI:=x to Pos[x]*block DoInc (A[I],Z); the          forI:= (pos[y]-1) *block+1To Y DoInc (A[I],Z); - end;Wuyi      fori:=pos[x]+1To pos[y]-1  DoInc (ADD[I],Z); the     New(Pos[x]);New(Pos[y]); - end; Wu  - function Find (x,y:longint): Longint; About varL,r,mid:longint; $ begin -find:= (x1)*Block; -l:= (x1) *block+1; r:=x*Block; -      whileL<=r Do A begin +mid:= (l+r) >>1; the         ifb[mid]>=y Then - begin $Find:=mid; L:=mid+1; theEndElser:=mid-1; the end; theDec (Find, (x1)*block); the end; -  in function Query (x,y,z:longint): Longint; the varTot,i:longint; the begin Abouttot:=0; the     ifpos[x]=Pos[y] Then theBegin forI:=x to Y Do ifA[i]>=z then Inc (TOT); endElse the begin +          forI:=x to Pos[x]*block Do ifa[i]>=Z then Inc (TOT); -          forI:= (pos[y]-1) *block+1To Y Do ifa[i]>=Z then Inc (TOT); the end;Bayi      fori:=pos[x]+1To pos[y]-1  DoInc (Tot,find (i,z-add[i])); the exit (TOT); the end; -  - begin the readln (n,q); theFillchar (Add,sizeof(add),0); theblock:=trunc (sqrt (n)); the      fori:=1to n Do - begin the read (a[i]); thepos[i]:= (I-1) Div block+1;//Pos[i] denotes the block where the number of I the end;94 Readln; the     ifN MoD block=0Then M:=n div blockElseM:=n Div Block +1; the      fori:=1to M Do New(i); the          fori:=1to Q Do98 begin About read (CH); -         ifCh='M' Then101 begin102 readln (x, y, z);103 Update (x, y, z);104EndElse the begin106 readln (x, y, z);107 writeln (query (x, Y, z));108 end;109 end; theEnd.

[BZOJ3343] Magic of the Bishop |

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.