HDU 4027 can you answer these queries? (Line Segment tree)

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4027

Can you answer these queries?
Total submission (s): 8458 accepted submission (s): 1929
Time Limit: 4000/2000 MS (Java/others) memory limit: 65768/65768 K (Java/Others)



Problem descriptiona lot of battleships of edevil are arranged in a line before the battle. our commander decides to use our secret weapon to eliminate the battleships. each of the battleships can be marked a value of endurance. for every attack of our secret weapon, it cocould decrease the endurance of a consecutive part of battleships by make their endurance to the square root of it original value of endurance. during the series of attack of our secret weapon, the commander wants to evaluate the effect of the weapon, so he asks you for help.
You are asked to answer the queries that the sum of the endurance of a consecutive part of the battleship line.

Notice that the square root operation shocould be rounded down to integer.
Inputthe input contains several test cases, terminated by EOF.
For each test case, the first line contains a single integer N, denoting there are n battleships of edevil in a line. (1 <= n <= 100000)
The second line contains N integers EI, indicating the endurance value of each battleship from the beginning of the line to the end. You can assume that the sum of all endurance value is less than 263.
The next line contains an integer m, denoting the number of actions and queries. (1 <= m <= 100000)
For the following M lines, each line contains three integers t, X and Y. the T = 0 denoting the action of the secret weapon, which will decrease the endurance value of the battleships between the X-th and Y-th battleship, Volume Sive. the T = 1 denoting the query of the Commander which ask for the sum of the endurance value of the battleship between X-th and Y-th, inclusive.
 
Outputfor each test case, print the case number at the first line. Then print one line for each query. And remember follow a blank line after each test case.
Sample Input
101 2 3 4 5 6 7 8 9 1050 1 101 1 101 1 50 5 81 4 8
 
Sample output
Case #1:1976
 
Sourcethe 36th ACM/ICPC Asia Regional Shanghai site -- online contest

Question:

A row of enemy blood volume is given. Each attack can change the enemy blood volume within the range to the arithmetic square root of the original blood volume (rounded down), and ask about the enemy blood volume within the range and.

Analysis:

Obviously, it seems that lazy tags are not well designed. Let's think about the square root of arithmetic, SQRT (1) = 1, and the 64-bit integer ranges from 6 times to 1, as long as the number in the interval is 1, I don't need to update this interval. Therefore, each update is updated to the leaf node to maintain the interval and the sum. There is no 0 in the data, but is trick x> Y interesting?


/*** Author: fcbruce ** Date: 16:46:52 **/# include <cstdio> # include <iostream> # include <sstream> # include <cstdlib> # include <algorithm> # include <ctime> # include <cctype> # include <cmath> # include <string> # include <cstring> # include <stack> # include <queue> # include <list> # include <vector> # include <map> # include <set> # define sqr (X) (x) * (x) # define ll long # define ITN int # define INF 0x3f3f3 F # define PI 3.1415926535897932384626 # define EPS 1e-10 # ifdef _ Win32 # define LLD "% i64d" # else # define LLD "% LLD" # endif # define maxm # define maxn 100007 using namespace STD; ll sum [maxn <2]; inline void pushup (itn k) {sum [k] = sum [K * 2 + 1] + sum [K * 2 + 2];} void build (int K, int L, int R) {If (R-l = 1) {scanf (LLD, & sum [k]); return;} build (K * 2 + 1, l, L + r> 1); Build (K * 2 + 2, L + r> 1, R); pushup (k);} void Update (int, int B, int K, int L, Int R) {If (B <= L | r <= A) return; If (sum [k] = R-l) return; if (R-l = 1) {sum [k] = (LL) SQRT (sum [k]); return;} update (A, B, K * 2 + 1, l, l + r> 1); Update (a, B, K * 2 + 2, L + r> 1, R ); pushup (k);} ll query (itn a, int B, itn k, int L, int R) {If (B <= L | r <=) return 0; if (a <= L & R <= B) Return sum [k]; return query (a, B, K * 2 + 1, l, L + r> 1) + query (a, B, K * 2 + 2, L + r> 1, R);} int main () {# ifndef online_judgefreopen ("/home/fcbruce/documentation/code/T", "r", stdin ); # Endif // online_judgeint n, m ,__ = 0; while (~ Scanf ("% d", & N) {printf ("case # % d: \ n", ++ _); Build (0, 0, n); itn q, a, B; scanf ("% d", & M); While (M --) {scanf ("% d", & Q, &, & B); If (A> B) Swap (a, B); a --; If (q) printf (LLD "\ n", query (a, B, 0, 0, n); elseupdate (a, B, 0, 0, n);} putchar ('\ n');} return 0 ;}


HDU 4027 can you answer these queries? (Line 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.