hdu 1698 Just a Hook 线段树成段更新

来源:互联网 发布:ubuntu怎么配置ip 编辑:程序博客网 时间:2024/06/05 14:10
裸的成段更新。。。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;#define maxn 110000#define lson rt<<1,l,m#define rson rt<<1|1,m+1,rint tree[maxn<<2],lazy[maxn<<2];void pushup(int rt){    tree[rt]=tree[rt<<1]+tree[rt<<1|1];}void pushdown(int rt,int mid){    if(lazy[rt])    {        lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];        tree[rt<<1]=lazy[rt]*(mid-(mid>>1));        tree[rt<<1|1]=lazy[rt]*(mid>>1);        lazy[rt]=0;    }}void build(int rt,int l,int r){    int m=(l+r)>>1;    lazy[rt]=0;    if(l==r)    {        tree[rt]=1;        return;    }    build(lson);    build(rson);    pushup(rt);}void update(int rt,int l,int r,int x,int y,int val){    if(x<=l&&r<=y)    {        lazy[rt]=val;        tree[rt]=val*(r-l+1);        return;    }    pushdown(rt,r-l+1);    int m=(l+r)>>1;    if(x<=m)    {        update(lson,x,y,val);    }    if(y>m)    {        update(rson,x,y,val);    }    pushup(rt);}int query(int rt,int l,int r,int x,int y){    if(x==l&&y==r)    {        return tree[rt];    }    pushdown(rt,r-l+1);    int m=(l+r)>>1;    int tmp=0;    if(y<=m)    {        return query(lson,x,y);    }    else if(x>m)    {        return query(rson,x,y);    }    else    {        return query(lson,x,m)+query(rson,m+1,y);    }}int main(){    int cas=0,n,m,t;    int a,b,c;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        build(1,1,n);        scanf("%d",&m);        for(int i=1;i<=m;i++)        {            scanf("%d%d%d",&a,&b,&c);            update(1,1,n,a,b,c);        }        int ans=query(1,1,n,1,n);        cas++;        printf("Case %d: The total value of the hook is %d.\n",cas,ans);    }    return 0;}

0 0