Line Segment tree + Interval Update: HDU 4893 wow! Such sequence!

Source: Internet
Author: User
Wow! Such sequence!

Time Limit: 10000/5000 MS (Java/others) memory limit: 65536/65536 K (Java/Others)
Total submission (s): 2234 accepted submission (s): 657


Problem descriptionrecently, Doge got a funny birthday present from his new friend, protein tiger from st. Beeze college. No, not cactuses. It's a mysterious blackbox.

After some research, Doge found that the box is maintaining a sequence an of N numbers internally, initially all numbers are zero, and there are three "operations ":

1. Add D to the k-th number of the sequence.
2. query the sum of AI where L ≤ I ≤ r.
3. Change AI to the nearest Fibonacci number, where L ≤ I ≤ r.
4. Play Sound "Chee-Rio! ", A bit useless.

Let f0 = 1, F1 = 1, Maid number FN is defined as fn = FN-1 + FN-2 for n ≥ 2.

Nearest Maid number of number x means the smallest FN where | fn-x | is also smallest.

Doge doesn' t believe the machine cocould respond each request in less than 10 ms. Help Doge figure out the reason.

 

Inputinput contains several test cases, please process till EOF.
For each test case, there will be one line containing two integers n, m.
Next m lines, each line indicates a query:

1 K d-"add"
2 l r-"query sum"
3 l r-"change to nearest maid"

1 ≤ n ≤ 100000, 1 ≤ m ≤ 100000, | d | <231, all queries will be valid.

 

Outputfor each type 2 ("query sum") operation, output one line containing an integer represent the answer of this query.

 

Sample input1 12 1 15 41 1 71 3 173 2 42 1 5

 

Sample output022

 

Authorfudan University

 

Source2014 multi-university training contest 3

 

[Question analysis]

This question is more difficult than the bare line segment tree Interval Update.
We set a fib in each node to indicate the number of fiber nacci closest to sum. When each interval is updated, the sum value is updated to fib. The FIB value is changed only in the single-point update process. That is to say, when the sum value changes, the FIB changes because after the sum value changes to fib, the number of Fiber-nacci closest to sum is also the FIB value.

Lazy ---- record whether the child nodes below this point need to be updated.

 

I used long, and then used % LLD input. I don't know if hangdian does not support % LLD. After debugging for half a day, TLE has been running for more than 20 times, later, I changed % LLD to % i64d. Then I cried and killed Qaq...

 

//Memory   Time//  K      MS#include<algorithm>#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<vector>#include<queue>#include<stack>#include<iomanip>#include<string>#include<climits>#include<cmath>#define MAX 100010#define LL long longusing namespace std;LL n,m;LL ans;LL f[60];struct Tree{    LL l,r;    LL sum,fib;    bool lazy;};Tree tree[MAX*3];LL Find(LL x){    if(x<=0)return 1;    LL ldis,rdis;    for(int i=0;i<59;++i)    {        if(f[i]<=x&&f[i+1]>=x)        {            ldis=x-f[i];            rdis=f[i+1]-x;            return ldis<=rdis?f[i]:f[i+1];        }    }}void pushup(LL x){    LL tmp=x<<1;    tree[x].sum=tree[tmp].sum+tree[tmp+1].sum;    tree[x].fib=tree[tmp].fib+tree[tmp+1].fib;}void pushdown(LL x){    if(!tree[x].lazy)return;    tree[x].lazy=0;    if(tree[x].l==tree[x].r)return;    LL tmp=x<<1;    tree[tmp].lazy=tree[tmp+1].lazy=1;    tree[tmp].sum=tree[tmp].fib;    tree[tmp+1].sum=tree[tmp+1].fib;}void build(LL l,LL r,LL x){    tree[x].l=l,tree[x].r=r;    tree[x].sum=0,tree[x].fib=1,tree[x].lazy=0;    if(l==r)return;    LL tmp=x<<1;    LL mid=(l+r)>>1;    build(l,mid,tmp);    build(mid+1,r,tmp+1);    pushup(x);}void add(LL x,LL k,LL num){    if(tree[x].l==tree[x].r)    {        tree[x].sum+=num;        tree[x].fib=Find(tree[x].sum);        return;    }    if(tree[x].lazy)        pushdown(x);    LL tmp=x<<1;    LL mid=(tree[x].l+tree[x].r)>>1;    if(k<=mid)        add(tmp,k,num);    else if(k>mid)        add(tmp+1,k,num);    pushup(x);}void change(LL l,LL r,LL x){    if(r<tree[x].l||l>tree[x].r)return;    if(l<=tree[x].l&&r>=tree[x].r)    {        tree[x].sum=tree[x].fib;        tree[x].lazy=1;        return;    }    if(tree[x].lazy)pushdown(x);    LL tmp=x<<1;    LL mid=(tree[x].l+tree[x].r)>>1;    if(r<=mid)        change(l,r,tmp);    else if(l>mid)        change(l,r,tmp+1);    else    {        change(l,mid,tmp);        change(mid+1,r,tmp+1);    }    pushup(x);}void query(LL l,LL r,LL x){    if(r<tree[x].l||l>tree[x].r)return;    if(l<=tree[x].l&&r>=tree[x].r)    {        ans+=tree[x].sum;        return;    }    if(tree[x].lazy)        pushdown(x);    LL tmp=x<<1;    LL mid=(tree[x].l+tree[x].r)>>1;    if(r<=mid)        query(l,r,tmp);    else if(l>mid)        query(l,r,tmp+1);    else    {        query(l,mid,tmp);        query(mid+1,r,tmp+1);    }    pushup(x);}int main(){//    freopen("cin.txt","r",stdin);//    freopen("cout.txt","w",stdout);    f[0]=f[1]=1;    for(int i=2;i<60;++i) f[i]=f[i-1]+f[i-2];    while(scanf("%I64d %I64d",&n,&m)!=EOF)    {        build(1,n,1);        LL a,b,c;        while(m--)        {            scanf("%I64d %I64d %I64d",&a,&b,&c);            if(a==1)                add(1,b,c);            else if(a==2)            {                ans=0;                query(b,c,1);                printf("%I64d\n",ans);            }            else                change(b,c,1);        }    }    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.