线段树之区间更新Wow! Such Sequence!

Description

Recently, 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 F 0 = 1,F 1 = 1,Fibonacci number Fn is defined as F n = F n - 1 + F n - 2 for n ≥ 2.

Nearest Fibonacci number of number x means the smallest Fn where |F n - x| is also smallest.

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

Input

Input 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 Fibonacci"

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

Output

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

Sample Input

1 1 2 1 1 5 4 1 1 7 1 3 17 3 2 4 2 1 5
 

Sample Output

0 22
 
 
注意:1.lower_bound的使用:upper_bound()函数是指定范围内查找大于目标值的第一个元素,
  2.开了仨数组,change记录情况三时改变成的斐波那契数字,并且给change也搞了个树
  3.a数组用于标记,情况一是换过斐波那契数了,没有pushdown,改过子节点,此时a为1;情况二是换过斐波那契,但是pushdown过了,或者没有换过斐波那契数字。
 
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
using namespace std;

const int N=1e5+5;
long long fin[75],f[N<<2],change[N<<2];
int a[N<<2];

long long fact(long long num){
    
    if(num<=0){
        return fin[1];
    } 
    
    long long ii=lower_bound(fin+1,fin+70+1,num)-fin;//lower_bound给出fin1到fin71之间第一个大于num 的元素,再找出其的编号 
    
    if(fin[ii]==num){
        return num;
    }
    
    if(abs(fin[ii]-num)<abs(fin[ii-1]-num)){
        return fin[ii];
    }
    
    return fin[ii-1];
}

void init(){
    
    fin[1]=1;
    fin[2]=1;
    
    for(long long i=3;i<=70;i++){
        fin[i]=fin[i-1]+fin[i-2];
    }
    
}

void pushup(long long root){
    int rt=root<<1;
    
    f[root]=f[rt]+f[rt+1];
    change[root] = change[rt] + change[rt+ 1];
}

void pushdown(long long root){
    int rt=root<<1;
    
    if(a[root]!=-1){
        a[rt]=a[rt+1]=a[root];
        
        f[rt]=change[rt];
        f[rt+1]=change[rt+1];
        a[root]=-1;
    }
}


void build(long long root,long long left,long long right){
    if(left==right){
        f[root]=0;
        change[root]=1;
        a[root]=-1;
    }else{
    long long mid=(left+right)>>1;
    long long rt=root<<1;
    
    build(rt,left,mid);
    build(rt+1,mid+1,right);
    
    a[root]=-1;
    pushup(root);}
    
}

void updata1(long long root,long long left,long long right,long long k,long long d){
    if(left==right){
        if(left==k){
            f[root]+=d;
        change[root]=fact(f[root]);    
        }
        return;
    }
    
    pushdown(root);
    
    long long mid=(left+right)>>1;
    long long rt=root<<1;
    
    if(k<=mid){
        updata1(rt,left,mid,k,d);
    }else{
        updata1(rt+1,mid+1,right,k,d);
    }
    pushup(root);
    
}

void updata3(long long root,long long left,long long right,long long cl,long long cr){
    if(cl>right||cr<left){
        return;
    }
    
    if(cl<=left&&cr>=right){
        a[root]=1;
        f[root]=change[root];
        return;
    }
    
    pushdown(root);
    
    long long mid=(left+right)>>1;
    long long rt=root<<1;
    
    if(cl<=mid){
        updata3(rt,left,mid,cl,cr);
    }
    if(cr>mid){
        updata3(rt+1,mid+1,right,cl,cr);
    }
    pushup(root);
}

long long query(long long root,long long left,long long right,long long cl,long long cr){
    if(cl>right||cr<left){
        return 0;
    }
    
    if(cl<=left&&cr>=right){
        return f[root];
    }
    
    if(left==right){
        return 0;
    }
    
    pushdown(root);
    
    long long rt=root<<1;
    long long mid=(left+right)>>1;
    
    long long ans=0;
    
    if(cl<=mid){
        ans+=query(rt,left,mid,cl,cr); 
    }
    
    if(cr>mid){
        ans+=query(rt+1,mid+1,right,cl,cr);
    }
    
    return ans;
}


int main(){
    long long n,m;
    init();
    
    while(~scanf("%I64d%I64d",&n,&m)){
        build(1,1,n);
        
        while(m--){
            long long op,a,b;
            scanf("%I64d%I64d%I64d",&op,&a,&b);
            
            if(op==2){
                printf("%I64d\n",query(1,1,n,a,b));
                
            }else if(op==1){
                updata1(1,1,n,a,b);
            }else{
                updata3(1,1,n,a,b);
            }
        }
        
    }
    return 0;
}



 

 

 
posted @ 2022-07-14 16:15  killjoyskr  阅读(27)  评论(0)    收藏  举报