zoukankan      html  css  js  c++  java
  • 【qbxt day3】线段树

    先放代码

    线段树模板1

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define root 1,n,1
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define maxn 100010
    using namespace std;
    
    LL n,m,y[maxn<<2];
    char c;
    LL flag;
    
    void read(LL &x){
        x=0;c=getchar();flag=1;
        while(c<'0'||c>'9') flag=(c=='-'?-1:1),c=getchar();
        while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
        x*=flag;
    }
    
    struct rec{
        LL l,r;
        LL sum;
        LL col;
        rec(){
            sum=col=0;
        }
        
        rec(LL ll,LL rr,LL v){
            l=ll;r=rr;
            sum=v;
            col=0;
        }
        
        void color(LL v){
            col+=v;
            sum+=(r-l+1)*v;
        }
    }z[maxn<<2];
    
    void color(LL l,LL r,LL rt,LL v){
        z[rt].color(v);
    }
    
    void push_col(LL l,LL r,LL rt){
        if(z[rt].col!=0){
            LL m=(l+r)>>1;
            color(lson,z[rt].col);
            color(rson,z[rt].col);
            z[rt].col=0;
        }
    }
    
    rec operator+(rec a,rec b){
        rec c;
        c.l=a.l;c.r=b.r;
        c.sum=a.sum+b.sum;
        return c;
    }
    
    rec query(LL l,LL r,LL rt,LL nowl,LL nowr){
        if(nowl<=l&&r<=nowr) return z[rt];
        push_col(l,r,rt);
        int m=(l+r)>>1;
        if(nowl<=m){
            if(m<nowr) return query(lson,nowl,nowr)+query(rson,nowl,nowr);
            else return query(lson,nowl,nowr);
        }
        else return query(rson,nowl,nowr);
    }
    
    void update(LL rt){
        z[rt]=z[rt<<1]+z[rt<<1|1];
    }
    
    void build(LL l,LL r,LL rt){
        if(l==r){
            z[rt]=rec(l,r,y[l]);
            return ;
        }
        LL m=(l+r)>>1;
        build(lson);
        build(rson);
        update(rt);
    }
    
    void modify(LL l,LL r,LL rt,LL nowl,LL nowr,LL v){
        if(nowl<=l&&r<=nowr){
            color(l,r,rt,v);
            return ;
        }
        push_col(l,r,rt);
        LL m=(l+r)>>1;
        if(nowl<=m) modify(lson,nowl,nowr,v);
        if(m<nowr) modify(rson,nowl,nowr,v);
        update(rt);
    }
    
    int main(){
        //read(n);read(m);
        cin>>n>>m;
        for(int i=1;i<=n;i++) cin>>y[i];
        build(root);
        for(int i=1;i<=m;i++){
            int s;
            cin>>s;
            if(s==2){
                LL l,r;
                read(l);read(r);
                cout<<query(root,l,r).sum<<endl;
            }
            else{
                LL l,r,v;
                read(l);read(r);read(v);
                modify(root,l,r,v);
            }
        }
    }

    线段树2

    #include<iostream>
    
    using namespace std;
    
    #define root 1,n,1
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    
    const int maxn=100010;
    int y[maxn<<2];
    int mul,add;
    
    struct rec
    {
        int l,r;
        int mul,add;
        int sum;
        int col;
        rec()
        {
            sum=col=0;
        }
        rec(int ll,int rr,int v)
        {
            l=ll;r=rr;
            sum=v;
            col=0;
        }
        void color(int a,int b)
        {
            mul=mul*a;add=add*a+b;
            sum=sum*a+(r-l+1)*b;
        }
    }z[maxn<<2];
    
    rec operator+(rec a,rec b)
    {
        rec c;
        c.l=a.l;c.r=b.r;
        c.sum = a.sum + b.sum;
        return c;
    }
    
    void update(int rt)
    {
        z[rt] = z[rt<<1] + z[rt<<1|1];
    }
    
    void color(int l,int r,int rt,int mul,int add)
    {
        z[rt].color(mul,add);
    }
    
    void push_col(int l,int r,int rt)
    {
        if (z[rt].mul != 0||z[rt].add!=0)
        {
            int m=(l+r)>>1;
            color(lson,z[rt].mul,z[rt].add);
            color(rson,z[rt].mul,z[rt].add);
            z[rt].col=0;
        }
    }
    
    void build(int l,int r,int rt)
    {
        if (l==r)
        {
            z[rt] = rec(l,r,y[l]);
            return;
        }
        int m=(l+r)>>1;
        build(lson);
        build(rson);
        update(rt);
    }
    
    rec query(int l,int r,int rt,int nowl,int nowr)
    {
        if (nowl<=l && r<=nowr) return z[rt];
        push_col(l,r,rt);
        int m=(l+r)>>1;
        if (nowl<=m)
        {
            if (m<nowr) return query(lson,nowl,nowr)+query(rson,nowl,nowr);
            else return query(lson,nowl,nowr);
        }
        else return query(rson,nowl,nowr);
    }
    
    void modify(int l,int r,int rt,int nowl,int nowr,int mul,int add)
    {
        if (nowl<=l && r<=nowr)
        {
            color(l,r,rt,mul,add);
            return;
        }
        push_col(l,r,rt);
        int m=(l+r)>>1;
        if (nowl<=m) modify(lson,nowl,nowr,mul,add);
        if (m<nowr) modify(rson,nowl,nowr,mul,add);
        update(rt);
    }
    
    int main(){
        
    }
  • 相关阅读:
    CCF——分蛋糕(2017-3)
    CCF——公共钥匙盒(2017-9)
    CCF——打酱油(2017-9)
    CCF——游戏(2017-12)
    SDS-简单动态字符串
    Redis主从复制
    MySQL 知识点
    MySQL 死锁
    Java 类加载机制
    Java IO
  • 原文地址:https://www.cnblogs.com/civilization-ga/p/9495263.html
Copyright © 2011-2022 走看看