Codeforces 444C(线段树)

区间颜色不一致就更新到底,否则lazy标记

#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
#define lc l,m,index<<1
#define rc m+1,r,index<<1|1
#define N 100005
#define ll __int64

struct node
{
	bool same;
	ll col,sum,add;
}seg[N<<2];

ll n,m;

void color(ll x,ll add,ll l,ll r,ll index)
{
	seg[index].same=true;
	seg[index].sum+=add*(r-l+1);
	seg[index].col=x;
	seg[index].add+=add;
}

void pushup(ll l,ll r,ll index)
{
	seg[index].sum=seg[index<<1].sum+seg[index<<1|1].sum;
	if(seg[index<<1].same&&seg[index<<1|1].same&&seg[index<<1].col==seg[index<<1|1].col)
	{
		seg[index].same=true;
		seg[index].col=seg[index<<1].col;
	}
	else seg[index].same=false;
}

void pushdown(ll l,ll r,ll index)
{
	ll m=(l+r)>>1;
	if(seg[index].add)
	{
		color(seg[index].col,seg[index].add,lc);
		color(seg[index].col,seg[index].add,rc);
		seg[index].add=0;
	}
}

void build(ll l,ll r,ll index)
{
	ll m=(l+r)>>1;
	if(l==r)
	{
		seg[index].same=true;
		seg[index].col=l;
		seg[index].sum=0;
		return;
	}
	build(lc);
	build(rc);
	pushup(l,r,index);
}

void update(ll L,ll R,ll x,ll l,ll r,ll index)
{
	ll m=(l+r)>>1;
	if(L==l&&R==r&&seg[index].same)
	{
		color(x,abs(seg[index].col-x),l,r,index);
		return;
	}
	pushdown(l,r,index);
	if(R<=m)update(L,R,x,lc);
	else if(L>m)update(L,R,x,rc);
	else
	{
		update(L,m,x,lc);
		update(m+1,R,x,rc);
	}
	pushup(l,r,index);
}

ll query(ll L,ll R,ll l,ll r,ll index)
{
	ll m=(l+r)>>1;
	if(L==l&&R==r)return seg[index].sum;
	pushdown(l,r,index);
	if(R<=m)return query(L,R,lc);
	else if(L>m)return query(L,R,rc);
	else return query(L,m,lc)+query(m+1,R,rc);
}

int main()
{
	scanf("%I64d%I64d",&n,&m);
	build(1,n,1);
	while(m--)
	{
		ll op,a,b,c;
		scanf("%I64d",&op);
		if(op==1)
		{
			scanf("%I64d%I64d%I64d",&a,&b,&c);
			update(a,b,c,1,n,1);
		}
		else
		{
			scanf("%I64d%I64d",&a,&b);
			printf("%I64d\n",query(a,b,1,n,1));
		}
	}
	return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子。为了使cnt_white - cnt_black尽可能大,可以使用两次形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的链所代表的子。在第二次遍历中,需要维护一个sum变量,用于存储链所代表的子的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是链所代表的子的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的链所代表的子。在第二次遍历中,需要维护一个sum变量,用于存储链所代表的子的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值