線段樹之區間賦值-帕吉的肉鉤

#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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章