hdu 1698 Just a Hook(成段更新)

题意:有t组测试数据,n为钩子长度(1<=n<=100000),m为操作的次数。初始时,每个钩子的价值为1,操作由三个数字组成x,y,z表示把区间[x,y]的钩子变成的价值变成z(1代表铜,2银,3金)。

       使用了延迟标记type,表示当前区间的左右子儿子的价值为type,在更新到当前区间时,同时更新当前区间的valu,即type*当前区间的长度。

/*代码风格更新后*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

#define LL(x) (x<<1)
#define RR(x) (x<<1|1)
#define MID(a,b) (a+((b-a)>>1))
const int N=100005;

struct node
{
	int lft,rht;
	int valu,type;
	int mid(){return MID(lft,rht);}
	void fun(int tmp)
	{
		type=tmp;
		valu=(rht-lft+1)*type;
	}
};
struct Segtree
{
	node tree[N*4];
	void build(int lft,int rht,int ind)
	{
		tree[ind].lft=lft;	tree[ind].rht=rht;
		tree[ind].valu=rht-lft+1;
		tree[ind].type=1;
		if(lft!=rht)
		{
			int mid=tree[ind].mid();
			build(lft,mid,LL(ind));
			build(mid+1,rht,RR(ind));
		}
	}
	void updata(int st,int ed,int ind,int type)
	{
		int lft=tree[ind].lft,rht=tree[ind].rht;
		if(st<=lft&&rht<=ed) tree[ind].fun(type);
		else 
		{
			if(tree[ind].type)
			{
				tree[LL(ind)].fun(tree[ind].type);
				tree[RR(ind)].fun(tree[ind].type);
				tree[ind].type=0;
			}
			int mid=tree[ind].mid();
			if(st<=mid) updata(st,ed,LL(ind),type);
			if(ed>mid)  updata(st,ed,RR(ind),type);
			tree[ind].valu=tree[LL(ind)].valu+tree[RR(ind)].valu;
		}
	}
}seg;
int main()
{
	int t,t_cnt=0;
	scanf("%d",&t);
	while(t--)
	{
		int n,m,a,b,c;
		scanf("%d%d",&n,&m);
		seg.build(1,n,1);
		while(m--)
		{
			scanf("%d%d%d",&a,&b,&c);
			seg.updata(a,b,1,c);
		}
		printf("Case %d: The total value of the hook is %d.\n",++t_cnt,seg.tree[1].valu);
	}
	return 0;
}

/*代码风格更新前*/
#include <iostream>
#include <cstdio>
using namespace std;
const int N=100005;
struct node
{
    int left,right,co,sum;
    int mid(){return left+(right-left)/2;}
    int dis(){return right-left+1;}
    void change(int a)
    {
        co=a; sum=co*dis();
    }
};
struct Segtree
{
    node tree[N*4];
    void build(int left,int right,int r)
    {
        tree[r].left=left;  tree[r].right=right;
        tree[r].co=1;      tree[r].sum=tree[r].dis();
        if(left<right)
        {
            int mid=tree[r].mid();
            build(left,mid,r*2);
            build(mid+1,right,r*2+1);
        }
    }
    void updata(int be,int end,int r,int co)
    {
        if(be<=tree[r].left&&tree[r].right<=end)
        {
            tree[r].change(co);
        }
        else
        {
            if(tree[r].co!=0)
            {
                tree[r*2].change(tree[r].co);
                tree[r*2+1].change(tree[r].co);
                tree[r].co=0;
            }
            int mid=tree[r].mid();
            if(be<=mid) updata(be,end,r*2,co);
            if(end>mid) updata(be,end,r*2+1,co);
            tree[r].sum=tree[r*2].sum+tree[r*2+1].sum;
        }
    }
}seg;
int main()
{
    int t,t_cnt=0;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        seg.build(1,n,1);
        for(int i=0;i<m;i++)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            seg.updata(x,y,1,z);
        }
        printf("Case %d: The total value of the hook is %d.\n",++t_cnt,seg.tree[1].sum);
    }
    return 0;
}

你可能感兴趣的:(struct,tree,测试,Build,hook)