首页 > 代码库 > HDU-1698 Just a Hook

HDU-1698 Just a Hook

第一次AC的数据结构的题目呢,虽然是线段树水题,,,,

好了,贴代码了

# include <cstdio>
# include <iostream>
# include <algorithm>
# include <cstring>
using namespace std;

const int maxn=1e5+5;
int val[maxn*4],lazy[maxn*4];

void build(int l,int r,int rt){
    if(l==r){
        val[rt]=1;
        return ;
    }
    int mid=(l+r)>>1;
    build(l,mid,rt<<1);
    build(mid+1,r,rt<<1|1);
    val[rt]=val[rt<<1]+val[rt<<1|1];
}

void pushdown(int rt,int l,int r,int mid){
    lazy[rt<<1]=lazy[rt];
    val[rt<<1]=(mid-l+1)*lazy[rt];
    lazy[rt<<1|1]=lazy[rt];
    val[rt<<1|1]=(r-mid)*lazy[rt];
    lazy[rt]=0;
}

void update(int l,int r,int L,int R,int rt,int c){
    if(L<=l&&r<=R) {
        lazy[rt]=c;
        val[rt]=(r-l+1)*c;
        return ;
    }
    int mid=(l+r)>>1;
    if(lazy[rt]) pushdown(rt,l,r,mid);
    if(L<=mid)
        update(l,mid,L,R,rt<<1,c);
    if(mid<R)
        update(mid+1,r,L,R,rt<<1|1,c);
    val[rt]=val[rt<<1]+val[rt<<1|1];
}

void sum(int l,int r,int rt){
    if(l==r) return ;
    int mid=(l+r)>>1;
    if(lazy[rt]) pushdown(rt,l,r,mid);
    sum(l,mid,rt<<1);
    sum(mid+1,r,rt<<1|1);
    val[rt]=val[rt<<1]+val[rt<<1|1];
}

int main(){
    int t,l=0;
    scanf("%d",&t);
    while(l<t){
        int n,q;
        scanf("%d",&n);
        build(1,n,1);
        scanf("%d",&q);
        for(int i=0;i<q;i++){
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            update(1,n,a,b,1,c);
        }
        sum(1,n,1);
        printf("Case %d: The total value of the hook is %d.\n",++l,val[1]);
    }
    return 0;
}
似乎最后的sum不需要的,,,

然而影响也不大,区间更新,lazy标记,水题,,,,

HDU-1698 Just a Hook