zoukankan      html  css  js  c++  java
  • 【模板】线段树

    今天考试的时候很开心的打了一个线段树,考完一看,SMG

    #include<iostream> 
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    #define maxn 100001
    #define LL long long 
    using namespace std;
    struct node
    {LL l,r,sum;}tree[maxn*3];
    LL a[maxn],f[maxn],lazy[maxn*3];
    inline LL read()
    {
        char ch=getchar();
        LL f=1,x=0;
        while(!(ch>='0'&&ch<='9')) { if(ch=='-') f=-1;ch=getchar();}
        while((ch>='0'&&ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
        return f*x;
    }
    void build(LL x,LL l,LL r)
    {
        tree[x].l=l;tree[x].r=r;
        if(l==r){tree[x].sum=read();return;}
        build(x*2,l,(l+r)>>1);
        build(x*2+1,1+((l+r)>>1),r);
        tree[x].sum=tree[x*2+1].sum+tree[x*2].sum;
    }
    void pushdown(LL x)
    {
        LL mid=(tree[x].l+tree[x].r)/2;
        tree[x*2].sum+=(mid-tree[x].l+1)*lazy[x];
        tree[x*2+1].sum+=(tree[x].r-mid)*lazy[x];
        lazy[x*2]+=lazy[x];
        lazy[x*2+1]+=lazy[x];
        lazy[x]=0;
    }
    void update(LL x,LL l,LL r,LL k)
    {
        if(tree[x].l>=l&&tree[x].r<=r)
        {
            tree[x].sum+=(tree[x].r-tree[x].l+1)*k;
            lazy[x]+=k;
            return;
        }
        if(tree[x].l>r||tree[x].r<l) return;
        LL mid=(tree[x].r+tree[x].l)>>1;
        if(lazy[x]) pushdown(x);
        update(x*2,l,r,k);update(x*2+1,l,r,k);
        tree[x].sum=tree[x*2].sum+tree[x*2+1].sum;
    }
    LL query(LL x,LL l,LL r)
    {
        if(tree[x].l>=l&&tree[x].r<=r) return tree[x].sum;
        if(tree[x].l>r||tree[x].r<l) return 0;
        if(lazy[x]) pushdown(x);
        return query(x*2,l,r)+query(2*x+1,l,r);
    }
    int main()
    {
       LL n,m,x,y,z;
       n=read();m=read();
       build(1,1,n);
       for(LL i=1;i<=m;i++)
       {
            z=read();x=read();y=read();
            if(z==1) {z=read();update(1,x,y,z);}
            else cout<<query(1,x,y)<<endl;
       }
       return 0; 
    }
    View Code
  • 相关阅读:
    [UE4]创建多把枪,使用Class,参数的对象类型
    [UE4]换枪需要做的事,容器:数组、集合、Map
    [UE4]蓝图重构
    [UE4]为什么会有类型检查
    [UE4]ChildActor组件
    [UE4]Character,飞行模式。
    [UE4]Pawn和Controller,第一人称和第三人称切换
    [UE4]组件
    [UE4]Acotr
    [UE4]封装、继承、多态
  • 原文地址:https://www.cnblogs.com/LQ-double/p/6069585.html
Copyright © 2011-2022 走看看