Blue Bridge Cup Log warrior (segment tree one-point interval update)

Source: Internet
Author: User

Title: Log Warrior

ATM took part in the calculator training course, after hard cultivation, the logarithm of the base of 2 is fast, called the log heroes.

One day, log hero's friend DRD has some integer sequence needs to transform, the log warrior just displays the mana ...

The rule of the transformation is that each integer of one of its subsequence becomes: [Log_2 (x) + 1] where [] means rounding down, which is the logarithm of 2 for each number, and then the whole.         For example, after the sequence 3 4 2 is manipulated once, the sequence becomes 2 3 2. DRD need to know how much the sequence will be after each such operation.

"Input Format" the first row of two positive integers n m. The second row n number, which represents the integer sequence, is positive. Next m line, two number L R per line indicates that the ATM operation is interval [L, R], sequence number starting from 1.

"Output format" output M line, which in turn represents the entire sequence of ATM after each operation.

For example, Input: 3 3 5 6 4 1 2 2 3 1 3

The program should output: 10 8 6

"Data range" for 30% data, N, M <= 10^3 for 100% data, N, M <= 10^5

Resource contract: Peak memory consumption < 256M CPU consumption < 1000ms

#include"Cstdio"#include"Cmath"#include"algorithm"using namespacestd;Const intmaxn=100005; typedefLong LongLL;structnode{intL,r; LL sum;} SEGTREE[MAXN*3];intCNT;voidBuildintRtintLintR) {SEGTREE[RT].L=l; SEGTREE[RT].R=R; if(l==r) {scanf ("%lld",&segtree[rt].sum); if(segtree[rt].sum==1) {CNT++;//count the number of 1 to facilitate optimization of the programsegtree[rt].sum++;//Change all 1 to 2 to prevent 1 interference program Optimization        }        return ; }    intMid= (l+r) >>1; Build (Rt<<1, L,mid); Build (Rt<<1)|1, mid+1, R); Segtree[rt].sum=segtree[rt<<1].sum+segtree[(rt<<1)|1].sum;}voidUpdateintRtintLintR) {    if(segtree[rt].l==l&&segtree[rt].r==r&&segtree[rt].sum==2* (r-l+1))return;//optimization: No more than 4 rounds, no less than 2 integers are changed to 2        if(segtree[rt].l==SEGTREE[RT].R) {Segtree[rt].sum= (LL) (Log2 (segtree[rt].sum*1.0)+1); return ; }        intMid= (SEGTREE[RT].L+SEGTREE[RT].R) >>1; if(r<=mid) Update (rt<<1, L,r); Else if(mid<l) Update ((rt<<1)|1, L,r); Else{update (RT<<1, L,mid); Update (RT<<1)|1, mid+1, R); } segtree[rt].sum=segtree[rt<<1].sum+segtree[(rt<<1)|1].sum;}intMain () {intn,m; scanf ("%d%d",&n,&m); Build (1,1, N);  while(m--)    {        intx, y; scanf ("%d%d",&x,&y); Update (1, x, y); printf ("%lld\n", segtree[1].sum-CNT); }}


Blue Bridge Cup Log warrior (segment tree one-point interval update)

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.