Bzoj 3211 Flower Gods travel all over the world tree-like array + and search collection

Source: Internet
Author: User

Topic: Given a sequence, the following actions are provided:

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

2. Query the and of the [l,r] Interval

The radical is not support interval modification, so we choose a single point to modify the interval query tree array, but this is O (n^2), how to do?

We find a number x open LOGLOGX the root of the root will become 1 that is, a number in the range of int only 5 times to open the root of the root will be changed to 1 the total time complexity of the modification is O (NLOGLOGN)

But what about a one-time modification? We maintain a and check set, once a number of 1 or 0, we set the position of the father to the right of the position can be so that the O (n) time to find the number of the first >1 after a number

In addition to this question added read-in optimization can be a lot faster than the puzzling 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 Flower Gods travel all over the world tree-like array + and search 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.