"XSY2111" Chef and Churus block tree array

Source: Internet
Author: User

Title Description

There is an array of length \ (n\) \ (a\ ) and \ (n\) intervals \ ([l_i,r_i]\), with \ (q\) operations:

   \ (1~x~y\): Change \ (a_x\) to \ (y\)

   \ (2~x~y\): The and of the interval to the l\ (r\) interval.

  \ (N,q\leq {10}^5,a_i\leq {10}^9\)

Exercises

Chunking.

Set \ (s_i\) is the interval of all intervals of the i\ block and, \ (d_{i,j}\) is the number of sections of the ( i\) block containing the location of \ (j\) .

Modify the tree array and the interval of each interval and. Set current \ (a_x=v\), then \ (s_i+= (y-v) \times d_{i,x}\)

Query the complete interval of the direct query interval and, incomplete interval on the violent query.

Set the block size to \ (m\), with a time complexity of
\[t (n) =o (\frac{n}{m}+m\log n) \]
When \ (M=\sqrt{\frac{n}{\log n}}\)
\[t (n) _{\min}=o (n\sqrt{n\log n}) \]

Code
#include <cstdio>#include <cstring>#include <algorithm>#include <cstdlib>#include <ctime>#include <utility>using namespaceStdtypedef Long Longlltypedef unsigned Long LongUlltypedefpair<int,int> Pii;ull c[100010];inta[100010];intNvoidAddintX,ull v) { for(; x<=n;x+=x&-x) c[x]+=v;} Ull sum (intx) {ull s=0; for(; x;x-=x&-x) s+=c[x];returns;}intBl;ull s[1010];intd[1010][100010];intl[100010];intr[100010];intblock[100010];intleft[100010];intright[100010];intMain () {memset (c,0,sizeofc);//Freopen ("Xsy2111.in", "R", stdin);//Freopen ("Xsy2111.out", "w", stdout);    intM scanf"%d", &n);intI for(i=1; i<=n;i++) {scanf ("%d", &a[i]);    Add (I,a[i]); } bl= -; M= (N+BL-1)/bl; for(i=1; i<=n;i++) block[i]= (I+BL-1)/bl; for(i=1; i<=m;i++) {left[i]= (i-1) *BL+1;    Right[i]=min (I*bl,n); } for(i=1; i<=n;i++) {scanf ("%d%d", &l[i],&r[i]); S[block[i]]+=sum (R[i])-sum (L[i]-1); d[block[i]][l[i]]++;if(r[i]<n) D[block[i]][r[i]+1]--; }intJ for(i=1; i<=m;i++) for(j=2; j<=n;j++) D[i][j]+=d[i][j-1];intQ scanf"%d", &q);intOp,x,y,k; for(i=1; i<=q;i++) {scanf ("%d%d%d", &op,&x,&y);if(op==1)        {intV=A[X]; for(j=1; j<=m;j++) S[j]+=ull (y-v) *d[j][x];            Add (X,Y-V);        A[x]=y; }Else{ull ans=0; for(j=block[x];j<=block[y];j++)if(left[j]>=x&&right[j]<=y) ans+=s[j];Else{intMi=max (LEFT[J],X);intMx=min (Right[j],y); for(k=mi;k<=mx;k++) Ans+=sum (R[k])-sum (L[k]-1); } printf ("%llu\n", ans); }    }return 0;}

"XSY2111" Chef and Churus block tree array

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.