Test Instructions: give you an interval, and then the initial value of each point tells you that there are two kinds of operations, one is to give you a small interval of the left and right end, then the interval of all the values are open square root, the other is the interval evaluation.
Method: The tree array maintains the sum and subtly opens the square root. (Line segment tree)
parsing: This is a question of a test exam--. At that time also did not think of fast open radical method, violent open root like 70 points. The first thing to be clear: the number of open radical is the largest < Span class= "Mrow" id= "mathjax-span-1095" > 10 9 Span style= "Display:inline-block; width:0px; Height:2.291em; " > , while < Span class= "Mrow" id= "mathjax-span-1100" > 10 9 Span style= "Display:inline-block; width:0px; Height:2.291em; " > How many times will it open to 1? With a calculator to find out 5 times will be the largest number open to 1, and 1 open square root How to open are 1, so if you open the square root of the operation, it is only useless waste of time. So how do you maintain this problem? We take an array f a [ i Represent >=i The number of the first number that is not 1 can save us a lot of time. How does it work? F oR(INT I=F INd (L);I<= R;I=F INd (I+1)) Use the For loop as above to open the square root, if the current is open to 1 below, you need to change his FA array, that is, in the for loop to fill in a sentence: < Span class= "Mi" id= "mathjax-span-1806" style= "Font-family:mathjax_math; Font-style:italic; " >i f ( p [ i <= 1)F a[I]=F INd (I+1);
PostScript: This idea has been applied in many problems, and the way of optimization is also a way to keep in mind.
Code:
#include <stdio.h>#include <math.h>#include <algorithm>using namespace STD;typedef Long Longll, LL c[100001]; ll p[100001]; ll fa[100001] ;intn; ll Lowbit (ll x) {returnX & (-X);} ll Getsum (ll x) {ll sum =0; while(x>0) {sum + = c[x]; X-= Lowbit (x); }returnsum;}voidUpdata (intX, ll T) { while(x <= N) {C[x] + = t; x + = Lowbit (x); }}intFindintx) {if(Fa[x] = = x)returnx;Else{Fa[x] = find (fa[x]);returnFA[X]; }}intMain () {scanf("%d", &n); for(inti =1; I <= N; i++) {scanf("%d", &p[i]); Updata (i, p[i]); Fa[i] = i; } fa[n+1] = n+1;intQ;scanf("%d", &q); for(inti =1; I <= Q; i++) {intX, L, R;scanf("%d%d%d", &x, &l, &r);if(x==1) {printf("%lld\n", Getsum (R)-Getsum (l1)) ; }Else{ for(inti = Find (l); I<=r; i = Find (i+1)) {intT = (int)(sqrt(P[i])) ; Updata (i, t-p[i]); P[i] = t;if(P[i] <=1) Fa[i] = Find (i+1) ; } } }}
Bzoj 3211 Flower Gods travel All Countries tree Array (line segment tree) + optimization