HDU4893线段树

HDU4893线段树的简单应用:

//主要优化部分  :若某一段进行过3操作且没进行过1操作,那么再次对该段进行3操作时可以忽略此段 
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 100005
#define ll root*2
#define rr root*2+1
#define mid (a[root].l+a[root].r)/2


__int64 fi[100];


struct node{
    int l, r;
    __int64 sum;
    int flag3;
}a[N*4];


void build(int l,int r,int root){
    a[root].l=l;a[root].r=r;
    a[root].sum=0;a[root].flag3=0;
    if(l==r) return;
    build(l,mid,ll);
    build(mid+1,r,rr);
}


__int64 find(__int64 key,int n){
    int l=1, r=n;
    while(l<=r){
        int mm=(l+r)/2;
        if(fi[mm]<=key&&mm==n) return fi[mm];
        else if(fi[mm]>=key&&mm==1) return fi[mm];
        else if(fi[mm]<=key&&fi[mm+1]>=key) {
            if(abs(fi[mm]-key)<=abs(fi[mm+1]-key))
            return fi[mm];
            else return fi[mm+1];
        }
        else if(fi[mm]>=key&&fi[mm-1]<=key) {
                if(abs(fi[mm-1]-key)<=abs(fi[mm]-key))
            return fi[mm-1];
            else return fi[mm];
        }
        else if(fi[mm]<key){
            l=mm+1;
        }
        else if(fi[mm]>key){
            r=mm-1;
        }
    }
}


void init(){
    fi[0]=1;fi[1]=1;fi[2]=2;
    for(int i=3;i<=90;i++){
        fi[i]=fi[i-1]+fi[i-2];
    }
}


void down(int root){
    if(a[root].flag3==1)return;
    a[root].flag3=1;
    if(a[root].l==a[root].r){
        a[root].sum=find(a[root].sum,90);    
        return ;
    }
    down(ll);
    down(rr);
    a[root].sum=a[ll].sum+a[rr].sum;
}


void add(int p,__int64 val,int root){
    a[root].flag3=0;
    if(a[root].l==p&&a[root].r==p){
        a[root].sum+=val;
        return;
    }
    if(p<=mid) add(p,val,ll);
    else add(p,val,rr);
    a[root].sum=a[ll].sum+a[rr].sum;
    a[root].flag3=min(a[ll].flag3,a[rr].flag3);
}


void update(int l,int r,int root){


     if(a[root].flag3) return;
    if(a[root].l>=l&&a[root].r<=r){
        down(root);
        return;
    }
    if(l<=mid) update(l,r,ll);
    if(r>mid) update(l,r,rr);
        a[root].sum=a[ll].sum+a[rr].sum;
    a[root].flag3=min(a[ll].flag3,a[rr].flag3);
}


__int64 query(int l,int r,int root){
    if(a[root].l==l&&a[root].r==r)return a[root].sum;
    if(r<=mid) return query(l,r,ll);
    else if(l>mid) return query(l,r,rr);
    else return query(l,mid,ll)+query(mid+1,r,rr);
}


main(){
    int x, z, n, m;
    int y;
    __int64 yy;
    init();
    while(scanf("%d %d",&n,&m)==2){
        
        build(1,n,1);
        while(m--){
            scanf("%d%d",&z,&x);
            if(z==1){scanf("%I64d",&yy);add(x,yy,1);}
            else if(z==2){scanf("%d",&y);printf("%I64d\n",query(x,y,1));}
            else {scanf("%d",&y);update(x,y,1);}
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值