2016 Summer vacation Multi-school Union---Rikka with Sequence (segment tree)

Source: Internet
Author: User
Tags square root

2016 Summer vacation Multi-school Union---Rikka with Sequence (segment tree)

Problem Descriptionas We know, Rikka is poor at math. Yuta is worrying on this situation, so he gives Rikka some math tasks to practice. There is one of the them:

Yuta have an array A with n numbers. Then he makes M operations on it.

There is three type of operations:

1 L r x:for each i in [l,r], change a[i] to A[i]+x
2 L r:for each i in [l,r], change a[i] to⌊a−−√[i]⌋
3 L R:yuta wants Rikka to sum up a[i] for all I in [l,r]

It is too difficult for Rikka. Can you help her?Inputthe First line contains a number T (1<=t<=100), the number of the testcases. And there is no more than 5 testcases with n>1000.

For each testcase, the first line contains the numbers n,m (1<=n,m<=100000). The second line contains n numbers a[1]~a[n]. Then M. lines follow, each line describe a operation.

It's guaranteed that 1<=a[i],x<=100000. Outputfor each operation of type 3, print a lines contains one number-the answer of the query. Sample Input15 2 3 4 5 1 3 2 3 sample Output56 author Learning Army Middle School Source2016 multi-university Training Conte   St 8 Recommendwange2014 | We have carefully selected several similar problems for you:5831 5830 5829 5827 5826

Test instructions: Three kinds of operation, 1, the interval plus a number;
2, the interval of all the number of open square root downward rounding;
3, the interval sum;

Train of thought: for the record interval maximum and the minimum value, if equal, then only need to a number open square root, calculates the difference value before and after the open radical, so that the interval open square root becomes the interval minus a number;
Because it is open root, so there are two of the beginning of the difference of 1, plus a number of the root is still the difference of 1, so maintenance of the same number of intervals is useless
For example (2,3) +6--> (8,9) open root--and (2,3) if all such operations, even if the same number of maintenance, each open root complexity is O (N), not T is Strange
This only needs to maintain the maximum minimum value of the interval, when the use of 1, to see if the root is still 1 or worse, if the difference is 1, then the interval is equivalent to the whole interval minus the same number,
So that we can change the root to reduce the

#include <iostream>#include<algorithm>#include<cstdio>#include<cmath>using namespaceStd;typedefLong LongLL;Const intBuffersize=1<< -;Charbuffer[buffersize],*head,*Tail;inlineCharGetchar () {if(head==tail) {        intL=fread (Buffer,1, Buffersize,stdin); Tail= (Head=buffer) +l; }    return*head++;} InlineintRead () {intx=0, f=1;CharC=Getchar ();  for(;! IsDigit (c); C=getchar ())if(c=='-') f=-1;  for(; IsDigit (c); C=getchar ()) x=x*Ten+c-'0'; returnx*F;}///----------------------------------------------------------------------Const intn=1e5+5; LL Sum[n<<2],lz[n<<2],mx[n<<2],mn[n<<2];voidUpintRT) {Sum[rt]=sum[rt<<1]+sum[rt<<1|1]; MX[RT]=max (mx[rt<<1],mx[rt<<1|1]); MN[RT]=min (mn[rt<<1],mn[rt<<1|1]);}voidBuildintRtintLintR) {Lz[rt]=0; if(L==R) {Sum[rt]=read (); mn[rt]=mx[rt]=sum[rt];return;} intMid=l+r>>1; Build (Rt<<1, l,mid); Build (rt<<1|1, mid+1, R); Up (RT);}voidDownintRtintLintR) {  if(lz[rt]!=0)  {    intMid=l+r>>1; Lz[rt<<1]+=Lz[rt]; Lz[rt<<1|1]+=Lz[rt]; Mn[rt<<1]+=Lz[rt]; Mx[rt<<1]+=Lz[rt]; Mx[rt<<1|1]+=Lz[rt]; Mn[rt<<1|1]+=Lz[rt]; Sum[rt<<1]+=lz[rt]* (mid-l+1); Sum[rt<<1|1]+=lz[rt]* (Rmid); LZ[RT]=0; }}intx,y,t,t,n,m;voidKaigen (intRtintLintR) {  if(x<=l&&r<=y) {if(mx[rt]==Mn[rt]) {Lz[rt]-=Mx[rt]; MX[RT]=sqrt (mx[rt]); MN[RT]=Mx[rt]; LZ[RT]+=Mx[rt]; SUM[RT]=mx[rt]* (r-l+1); return; }     Else if(mx[rt]==mn[rt]+1) {LL x1=sqrt (mx[rt]); LL X2=sqrt (mn[rt]); if(x1==x2+1) {Lz[rt]-= (mx[rt]-x1); SUM[RT]-= (mx[rt]-x1) * (r-l+1); MX[RT]=x1;mn[rt]=x2; return; }    }  }  intMid=l+r>>1;d Own (rt,l,r); if(X<=mid) Kaigen (rt<<1, L,mid); if(Y>mid) Kaigen (rt<<1|1, mid+1, R); Up (RT);}voidAddintRtintLintR) {  if(x<=l&&r<=y) {Lz[rt]+=T; SUM[RT]+=(Long Long) (r-l+1)*T; MX[RT]+=t;mn[rt]+=T; return ; }  intMid=l+r>>1;d Own (rt,l,r); if(x<=mid) Add (rt<<1, L,mid); if(y>mid) Add (rt<<1|1, mid+1, R); Up (RT);} LLGet(intRtintLintR) {  if(x<=l&&r<=y)returnSum[rt]; intMid=l+r>>1;d Own (rt,l,r); LL ret=0; if(X<=mid) ret+=Get(rt<<1, L,mid); if(Y>mid) ret+=Get(rt<<1|1, mid+1, R); returnret;}intMain () {T=read ();  while(t--) {n=read (); m=read (); Build (1,1, N);  while(m--)    {      intop; Op=read (); X=read (); y=read (); if(op==1) {T=read (); Add (1,1, N); }      Else if(op==2) Kaigen (1,1, N); Elseprintf"%i64d\n",Get(1,1, N)); }  }  return 0;}

2016 Summer vacation Multi-school Union---Rikka with Sequence (segment tree)

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.