hdu1698Just a Hook segment_tree

来源:互联网 发布:淘宝卖家寄寿衣怎么办 编辑:程序博客网 时间:2024/05/17 16:13
#include <iostream>#include <cstring>#include <cstdio>#define lson l,m,rt <<1#define rson m+1,r,rt << 1 |1using namespace std;const int N=100005;int sum[N << 2];int lazy[N<<2];void pushup(int rt){    sum[rt]=sum[rt<<1]+ sum[rt << 1 | 1];}void pushdown(int rt,int m){    if(lazy[rt]){        lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];        sum[rt<<1]=(m-(m>>1))*lazy[rt];//        sum[rt<<1|1]=(m>>1)*lazy[rt];        lazy[rt]=0;    }}void build(int l,int r,int rt){    lazy[rt]=0;    if(l==r){        sum[rt]=1;        return ;    }    int m= (l + r)>>1;    build(lson);    build(rson);    pushup(rt);}void update(int L,int R,int c,int l,int r,int rt){    if(L <= l && r <= R){        lazy[rt]=c;        sum[rt]=(r - l + 1)*c;        return ;    }    pushdown(rt,r-l+1);    int m=(l + r)>>1;    if(L <= m) update(L,R,c,lson);    if(R > m) update(L,R,c,rson);    pushup(rt);}int main(){    int t,n,Q,x,y,z;    cin>>t;    for(int cas=1;cas<=t;cas++){        scanf("%d%d",&n,&Q);        build(1,n,1);        while(Q--){            scanf("%d%d%d",&x,&y,&z);            update(x,y,z,1,n,1);        }        printf("Case %d: The total value of the hook is %d.\n",cas,sum[1]);    }    return 0;}

0 0
原创粉丝点击