Bzoj 3211 Flora to the National tree Array + and check the collection

Source: Internet
Author: User

Title effect: Given a sequence, it provides the following actions:

1. Change [L.R] Each number interval a[i] to sqrt (A[i])

2. query [L,r] interval and

Drastic changes are not supported by the interval, so we choose the single-point replacement interval query for the tree array, but this is O (n^2), what to do?

We find a number x open LOGLOGX the root of the root will become 1 that is, an int within the range of the number is only 6 times the root of the root will be changed to 1 then the total time of the change of Complexity O (NLOGLOGN)

But what about a single change? We maintain one and check the set. Once a number is 1 or 0, we can set the father of this position to the right of the position to be able to divide the O (n) time to find the number of the first >1 after a number

In addition, this topic increases the read-in optimization can be as fast as a very confusing 0.0

#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include < Algorithm> #define M 100100using namespace Std;typedef long long ll;int n,m,a[m],fa[m];ll c[m];inline int getc () {St    atic const int L = 1 << 15;    Static Char buf[l], *s = buf, *t = buf;        if (s = = t) {T = (s = buf) + fread (buf, 1, L, stdin);    if (S = = T) return EOF; } return *s++;}    inline int getint () {int C;    while (!isdigit (c = getc ()) && c! = '-');    BOOL sign = c = = '-'; int tmp = sign?

0:c-' 0 '; while (isdigit (c = getc ())) TMP = (tmp << 1) + (TMP << 3) + C-' 0 '; Return sign? -tmp:tmp;} int Find (int x) {if (!fa[x]| | FA[X]==X) return Fa[x]=x;return fa[x]=find (fa[x]);} void Update (int x,int y) {for (; x<=n;x+=x&-x) c[x]+=y;} ll Get_ans (int x) {ll re=0;for (; x;x-=x&-x) Re+=c[x];return re;} void Modify (int x,int y) {int i=x;for (i=x; i<=y; I=find (i+1)) {int temp= (int) sqrt (a[i]); Update (I,temp-a[i]); A[i]=temp;if (a[i]<=1) fa[i]=find (i+1);}} int main () {int i,p,x,y;cin>>n;for (i=1;i<=n;i++) {a[i]=getint (); if (a[i]==1| |! A[i]) fa[i]=i+1; Update (I,a[i]);} M=getint (); for (i=1;i<=m;i++) {p=getint (); X=getint (); Y=getint (); if (p==1) printf ("%lld\n", Get_ans (y)-get_ans ( x-1)); elsemodify (x, y);}}



Bzoj 3211 Flora to the National tree Array + and check the collection

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.