HDU 4027 Can You answer these queries? Segment tree, Interval modification

Source: Internet
Author: User

Can You answer these queries?




problem Descriptiona lot of battleships of evil is arranged in A line before the battle. Our commander decides-to-use we secret weapon to eliminate the battleships. Each of the battleships can is marked a value of endurance. For every attack of We secret weapon, it could 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 is asked to answer the queries, the sum of the endurance of a consecutive part of the battleship line.

Notice that square root operation should is 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 is n battleships of evil in a line. (1 <= N <= 100000)
The second line contains N integers Ei, indicating the endurance value of all battleship from the beginning of the Line to the end. You can assume, the sum of all endurance value are 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 would decrease the endurance value of the battleships between the X -th and y-th battleship, inclusive. The t=1 denoting the commander which ask for the sum of the endurance value of the battleship between a D y-th, inclusive. 

Outputfor each test case, print the case number at the first line. Then print one to each query. And remember follow a blank line after each test case. 

Sample InputTen1 2 3 4 5 6 7 8 950 1 101 1Ten1 1 50 5 81 4 8 

Sample OutputCase #1:7 6Test instructions: gives an array of n that supports 2 operations:1.0 a B: Square the number between A and B2.1 A B: the sum of the number of a~b between SanyNote: The square root of the problem is to be rounded down. then, there is a blank line after each sample. There is a trick, written in a note.
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <cmath>5 6 using namespacestd;7 8 #defineLson l,m,rt<<19 #defineRson m+1,r,rt<<1|1Ten #defineLL Long Long One Const intmaxn=100000+5; A  -LL sum[maxn<<2]; -  the //because 2^63 sums up 2^63 - //If there are only 1 numbers, it will open up to 7 radicals. - //that's 1 on the open. - //So, when the sum of the interval is equal to the interval length (r-l) +1, + //the value of this interval is no longer updated -  + //1 indicates that the interval does not need to be updated. A  at voidPushup (intRT) - { -sum[rt]=sum[rt<<1]+sum[rt<<1|1]; -  - } -  in voidBuildintLintRintRT) - { to     if(l==R) +     { -scanf"%lld",&Sum[rt]); the         return ; *     } $ Panax Notoginseng     intM= (l+r) >>1; -  the build (Lson); + build (Rson); A pushup (RT); the } +  - voidUpdateintLintRintLintRintRT) $ { $     if(sum[rt]==r-l+1) -         return ; -  the     if(l==R) -     {WuyiSum[rt]= (LL) (sqrt (Long Double) (Sum[rt] )); the         //printf ("%lld", Sum[rt]); -         return ; Wu     } -  About     intM= (l+r) >>1; $  -     if(l<=m) - Update (L,r,lson); -     if(r>m) A Update (L,r,rson); +  the pushup (RT); - } $  theLL Query (intLintRintLintRintRT) the { the     if(l<=l&&r>=R) the     { -         returnSum[rt]; in     } the  the     intM= (l+r) >>1; About  theLL ret=0; the  the     if(l<=m) +ret+=query (L,r,lson); -     if(r>m) theret+=query (L,r,rson);Bayi  the     returnret; the } -  - intMain () the { the     intcas=1; the  the     intN; -  the      while(SCANF ("%d", &n)! =EOF) the     { the         //memset (Cal,0,sizeof (CAL));94  theBuild1N1); the  the         intm;98scanf"%d",&m); About  -         intt,x,y;101 102printf"Case #%d:\n", cas++);103 104          for(intI=1; i<=m;i++) the         {106scanf" %d%d%d",&t,&x,&y);107 108             if(x>y)109 swap (x, y); the 111             if(t==0) the             {113Update (x, Y,1N1); the             } the             Else the             {117printf"%lld\n", query (x, Y,1N1));118             }119         } - 121printf"\ n");122     }123     return 0;124}
View Code

HDU 4027 Can You answer these queries? Segment tree, Interval modification

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.