线段树之区间赋值-帕吉的肉钩

#include<iostream>
using namespace std;
/*调试了好久才找出问题所在*/
const int MAX_N=100010;
struct node{
    int l,r;
    int  detal;
    int sum;
}hook[MAX_N<<2];

void pushup(int p){
    hook[p].sum=hook[p<<1].sum+hook[p<<1|1].sum;
}

void build(int p,int l,int r){
    hook[p].l=l;
    hook[p].r=r;
    hook[p].detal=0;
    if(l==r){
        hook[p].sum=1;
        return;
    }
    int mid=(l+r)>>1;
    build(p<<1,l,mid);
    build(p<<1|1,mid+1,r);
    pushup(p);
}
void down(int p,int l,int mid,int r){
    if(hook[p].detal){
    hook[p<<1].sum=hook[p].detal*(mid-l+1);
    hook[p<<1|1].sum=hook[p].detal*(r-mid);
    hook[p<<1].detal=hook[p].detal;
    hook[p<<1|1].detal=hook[p].detal;
    hook[p].detal=0;
    }
}

void modify(int p,int l,int r,int c){
    if(l<=hook[p].l&&hook[p].r<=r){
       hook[p].detal=c;
       hook[p].sum=c*(hook[p].r-hook[p].l+1);
       return;
    }
    int mid=(hook[p].l+hook[p].r)>>1;
    down(p,hook[p].l,mid,hook[p].r);
    if(l<=mid){
        modify(p<<1,l,r,c);
    }
    if(mid<r){
        modify(p<<1|1,l,r,c);
    }
    pushup(p);
}

int main(){
    std::ios::sync_with_stdio(false);
    int n,q;
    int x,y,z;
    cin>>n>>q;
    build(1,1,n);
    // cout<<"The total value of the hook is "<<hook[1].sum<<"."<<endl;
    while (q--) {
       cin>>x>>y>>z;
       modify(1,x,y,z);
    }
    cout<<"The total value of the hook is "<<hook[1].sum<<"."<<endl;
    return 0;
}


相关文章:

  • 2022-02-03
  • 2021-06-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-28
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-01
  • 2022-12-23
  • 2021-04-05
  • 2022-12-23
相关资源
相似解决方案