HDU-1698-Just a Hook-区间更新

来源:互联网 发布:授权回调域名 10003 编辑:程序博客网 时间:2024/05/17 02:59

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698

好吧,这个题目我起初想试试我不用延迟更新能不能过,嘿嘿,我还是太天真了。。。2000ms毫不客气的超时了,果然延迟更新大法好;这里我就不再详细的介绍延迟更新了,不懂的可以看我前面的博文:http://blog.csdn.net/wlxsq/article/details/46910485

#include<iostream>#include<string>#include<cstdio>#include<cstring>#include<queue>#include<map>#include<cmath>#include<stack>#include<set>#include<vector>#include<algorithm>#define LL long long#define inf 1<<30using namespace std;const int N=200005;int n,m,a,b,c;struct node{    int l,r,v;    int lazy;}node[N<<2];void PushUp(int rt)     //  向上更新;{    node[rt].v=node[rt<<1].v+node[rt<<1|1].v;}void PushDown(int rt)   //  向下更新;{    node[rt<<1].lazy=node[rt].lazy;    node[rt<<1|1].lazy=node[rt].lazy;    node[rt<<1].v=node[rt].lazy*(node[rt<<1].r-node[rt<<1].l+1);    node[rt<<1|1].v=node[rt].lazy*(node[rt<<1|1].r-node[rt<<1|1].l+1);    node[rt].lazy=0;    //  清零;}void build(int l,int r,int rt){    node[rt].l=l;    node[rt].r=r;    node[rt].lazy=0;    if(r==l){        node[rt].v=1;        return;    }    int mid=(l+r)>>1;    build(l,mid,rt<<1);    build(mid+1,r,rt<<1|1);    PushUp(rt);}void Insert(int l,int r,int v,int rt){    int ll=node[rt].l;    int rr=node[rt].r;    if(ll==l&&rr==r){        node[rt].v=v*(rr-ll+1);        node[rt].lazy=v;        return;    }    if(node[rt].lazy) PushDown(rt);    int mid=(ll+rr)>>1;    if(r<=mid) Insert(l,r,v,rt<<1);    else if(l>mid) Insert(l,r,v,rt<<1|1);    else {        Insert(l,mid,v,rt<<1);        Insert(mid+1,r,v,rt<<1|1);    }    PushUp(rt);}int main(){    int t,Case=1;    scanf("%d",&t);    while(t--){        scanf("%d",&n);        build(1,n,1);        scanf("%d",&m);        for(int i=0;i<m;i++){            scanf("%d%d%d",&a,&b,&c);            Insert(a,b,c,1);        }        printf("Case %d: The total value of the hook is %d.\n",Case++,node[1].v);    }    return 0;}


 

1 0