Hdu_4893 2014 multi-school tri-line tree

Source: Internet
Author: User

Given a sequence with an initial value of 0, there are three operations. The first two operations are normal. One is to add K to the number at a certain position, and the other is the query interval and. The third operation is troublesome. Change each value in a certain range to the number of Fibonacci closest to it. If there are two left and right close to it, take the value on the left first.

As soon as I saw the first two operations, I immediately wanted to get started with the tree array. Later I saw the third operation, which was a bit dumb. I thought about how to change the value quickly .. However, I ignore what the question requires. Only operation 2 is the output, that is, only the range and value are required. The other two are operations. In the reminder of cong Ge, knowing that two value records + a lazy tag are opened for the line segment tree, one value D records the normal range and value, and the other SF records the current fibanacci value and

The lazy flag records whether the current interval has been operated by 3. If it is marked, the next query returns SF; otherwise, the return value is D.

However, at the beginning, the idea was still a little unclear, and we had to make a few rounds. Obviously, the above conversion is a common single-point update + lazy tag + Range Query line segment tree.

The primary error lies in Pushdown function and 3 operation processing. Undistinguished Module

First 3, the operation is named "fix". After the fix finds the corresponding range, it is marked as "lazy". By the way, the current range D is changed to the SF value and then passed up.

Add operation. First, find the last single point to perform the update operation. Otherwise, Pushdown is used to pass the laziness mark. In the mark, the Operation d = SF is also required. Finally, the result is passed up.

Query is more obvious. If return SF or D is not found, Pushdown is performed first.

It was confused at first, and Pushdown was not pushed well, and the current value has not been modified.

It was a bit confused during the competition, and the line segment tree skills were not enough to continue to practice.

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define LL __int64using namespace std;const int N = 100010;LL f[100];LL d[N<<2],flag[N<<2],sf[N<<2];LL pref[N];int n,q;void init(){    LL tmp=1LL<<60;    f[0]=f[1]=1;    for (int i=2;i<=90;i++){        f[i]=f[i-1]+f[i-2];    }}void build(int rt ,int l,int r){    flag[rt]=d[rt]=0;    if (l>=r){        sf[rt]=1;        return;    }    int mid=(l+r)>>1;    build(lson);    build(rson);    sf[rt]=sf[rt<<1]+sf[rt<<1|1];}void change(int rt){    d[rt]=sf[rt];}void up(int rt,int l,int r){    d[rt]=d[rt<<1]+d[rt<<1|1];    sf[rt]=sf[rt<<1]+sf[rt<<1|1];}void pushdown(int rt,int l,int r){    int mid=(l+r)>>1;    if (flag[rt]==0 || l>=r) return;    flag[rt<<1]=flag[rt<<1|1]=flag[rt];    d[rt<<1]=sf[rt<<1];    d[rt<<1|1]=sf[rt<<1|1];    flag[rt]=0;}void fix(int L,int R,int rt,int l,int r){    if (flag[rt]==1) return;    if (L<=l && r<=R){        flag[rt]=1;        d[rt]=sf[rt];        return;    }    int mid=(l+r)>>1;    if (R<=mid) fix(L,R,lson);    else    if (L>mid) fix(L,R,rson);    else{        fix(L,R,lson);        fix(L,R,rson);    }    if (flag[rt<<1]==1 && flag[rt<<1|1]==1){        flag[rt]=1;    }    up(rt,l,r);}void add(int loc,LL val,int rt,int l,int r){    if (l>=r)    {        flag[rt]=0;        d[rt]+=val;        int loc=lower_bound(f,f+90,d[rt])-f;        sf[rt]=f[loc];        if (loc>0){            if (d[rt]-f[loc-1]<=f[loc]-d[rt]){            sf[rt]=f[loc-1];            }        }        return;    }    pushdown(rt,l,r);    int mid=(l+r)>>1;    if (loc<=mid) add(loc,val,lson);    else add(loc,val,rson);    up(rt,l,r);}LL query(int L,int R,int rt,int l,int r){    if (L<=l && r<=R){        if (flag[rt]) return sf[rt];        else return d[rt];    }    pushdown(rt,l,r);    int mid=(l+r)>>1;    if (R<=mid) return query(L,R,lson);    else    if (L>mid) return query(L,R,rson);    else    {        LL ret1=query(L,R,lson);        LL ret2=query(L,R,rson);        return ret1+ret2;    }}int main(){    init();    int op;    while (scanf("%d%d",&n,&q)!=EOF)    {        build(1,1,n);        while (q--)        {            scanf("%d",&op);            if (op==1){                int a;                LL b;                scanf("%d%I64d",&a,&b);                add(a,b,1,1,n);            }            else            if (op==2){                int a,b;                scanf("%d%d",&a,&b);                LL ans=query(a,b,1,1,n);                printf("%I64d\n",ans);            }            else{                int a,b;                scanf("%d%d",&a,&b);                fix(a,b,1,1,n);            }        }    }    return 0;}

 

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.