POJ-1698 Just a Hook(线段树)

线段树

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

typedef long long ll;
const int N = 100005;


struct node
{
    ll l,r;
    ll add,sum;//add是懒惰标记
}tree[N<<2];

void pushup(int root)
{
	tree[root].sum = tree[root << 1].sum + tree[root << 1 | 1].sum;
}

void pushdown(int root, int m)
{
	if (tree[root].add)
	{
		tree[root << 1].add = tree[root].add;
		tree[root << 1 | 1].add = tree[root].add;
		tree[root << 1].sum = tree[root].add * (m - (m >> 1));
		tree[root << 1 | 1].sum = tree[root].add * (m >> 1);
		tree[root].add = 0;
	}
}

void build(int l, int r, int root)
{
	tree[root].l = l;
	tree[root].r = r;
	tree[root].add = 0;
	tree[root].sum = 1;
	if (l == r)
	{
		tree[root].sum = 1;
		return;
	}
	int mid = (tree[root].l + tree[root].r) / 2;
	build(l, mid, root << 1);
	build(mid + 1, r, root << 1 | 1);
	pushup(root);
}

void update(int c, int l, int r, int root)
{
	if (tree[root].l == l && tree[root].r == r)
	{
		tree[root].add = c;
		tree[root].sum = (ll)c * (r - l + 1);
		return;
	}
	if (tree[root].l == tree[root].r)
		return;

	pushdown(root, tree[root].r - tree[root].l + 1);

	int mid = (tree[root].l + tree[root].r) >> 1;
	if (r <= mid)
		update(c, l, r, root << 1);
	else if (l > mid)
		update(c, l, r, root << 1 | 1);
	else
	{
		update(c, l, mid, root << 1);
		update(c, mid + 1, r, root << 1 | 1);
	}

	pushup(root);
}


int main()
{
    int t,n,q;
    cin >> t;
    for(int ca = 1;ca <= t;ca++)
    {
        scanf("%d%d",&n,&q);
        build(1,n,1);
        ll x,y,val;
        //char str[5];
        while(q--)
        {
            scanf("%lld%lld%lld",&x,&y,&val);
            update(val,x,y,1);
        }
        printf("Case %d: The total value of the hook is %lld.\n",ca,tree[1].sum);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值