线段树_HDU_1698

来源:互联网 发布:淘宝网男士假发 编辑:程序博客网 时间:2024/06/16 02:55
/*一段区间内连续修改,最后求和*/#include<iostream>const int maxn = 100010;using namespace std;int lazy[maxn<<2];struct node{    int l, r,d;};node tree[maxn<<2];int ans;void work(int rt){    tree[rt].d = tree[rt<<1].d+tree[rt<<1|1].d;}void workhard(int rt){    if(lazy[rt])    {        lazy[rt<<1] = lazy[rt<<1|1] = lazy[rt];        tree[rt<<1].d = (tree[rt<<1].r-tree[rt<<1].l+1)*lazy[rt];        tree[rt<<1|1].d = (tree[rt<<1|1].r-tree[rt<<1|1].l+1)*lazy[rt];        lazy[rt] = 0;    }}void build(int l, int r,int rt){    tree[rt].l=l;    tree[rt].r=r;    lazy[rt] = 0;    if( l== r)    {        tree[rt].d = 1;        return;    }    int mid = (l+r)>>1;    build(l,mid,rt<<1);    build(mid+1,r,rt<<1|1);    work(rt);}void updata(int x,int y,int z,int l,int r,int rt){    if(x<=l&&r<=y)    {        lazy[rt] = z;        tree[rt].d = (r-l+1)*z;        return;    }    int mid = (l+r)>>1;    workhard(rt);    if(mid>=x)        updata(x,y,z,l,mid,rt<<1);    if(y>mid)        updata(x,y,z,mid+1,r,rt<<1|1);    work(rt);}void query(int x, int y,int l,int r,int rt){    if(x<=l&&r<=y)    {        ans += tree[rt].d;        return;    }    int mid = (l+r)>>1;    workhard(rt);    if(mid>=x)        query(x,y,l,mid,rt<<1);    if(y>mid)        query(x,y,mid+1,r,rt<<1|1);}int main(){    int t;    int n,q,x,y,z;    scanf("%d",&t);    for(int cas = 1; cas <= t; cas++)    {        scanf("%d",&n);        build(1,n,1);        scanf("%d",&q);        while(q--)        {            scanf("%d%d%d",&x,&y,&z);            updata(x,y,z,1,n,1);        }        ans = 0;        query(1,n,1,n,1);        printf("Case %d: The total value of the hook is %d.\n",cas,ans);    }    return 0;}
0 0
原创粉丝点击