DZY Loves Colors CodeForces - 445E(线段树)

DZY loves colors, and he enjoys painting.

On a colorful day, DZY gets a colorful ribbon, which consists of n units (they are numbered from 1 to n from left to right). The color of the i-th unit of the ribbon is i at first. It is colorful enough, but we still consider that the colorfulness of each unit is 0 at first.

DZY loves painting, we know. He takes up a paintbrush with color x and uses it to draw a line on the ribbon. In such a case some contiguous units are painted. Imagine that the color of unit i currently is y. When it is painted by this paintbrush, the color of the unit becomes x, and the colorfulness of the unit increases by |x - y|.

DZY wants to perform m operations, each operation can be one of the following:

Paint all the units with numbers between l and r (both inclusive) with color x.
Ask the sum of colorfulness of the units between l and r (both inclusive).
Can you help DZY?

Input
The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105).

Each of the next m lines begins with a integer type (1 ≤ type ≤ 2), which represents the type of this operation.

If type = 1, there will be 3 more integers l, r, x (1 ≤ l ≤ r ≤ n; 1 ≤ x ≤ 108) in this line, describing an operation 1.

If type = 2, there will be 2 more integers l, r (1 ≤ l ≤ r ≤ n) in this line, describing an operation 2.

Output
For each operation 2, print a line containing the answer — sum of colorfulness.

Examples
Input
3 3
1 1 2 4
1 2 3 5
2 1 3
Output
8
Input
3 4
1 1 3 4
2 1 1
2 2 2
2 3 3
Output
3
2
1
Input
10 6
1 1 5 3
1 2 7 9
1 10 10 11
1 3 8 12
1 1 10 3
2 1 10
Output
129
Note
In the first sample, the color of each unit is initially [1, 2, 3], and the colorfulness is [0, 0, 0].

After the first operation, colors become [4, 4, 3], colorfulness become [3, 2, 0].

After the second operation, colors become [4, 5, 5], colorfulness become [3, 3, 2].

So the answer to the only operation of type 2 is 8.

给定一段序列,一开始每个点的价值为0,颜色第i个为i。有两种操作,1 x y z 代表着将x到y染为z颜色。2 x y 代表着从x到y序列的价值。染色过后节点价值增加abs(z-u)(u为这个节点原来的颜色编号)。很明显用线段树去进行这一系列的操作。但是一个一个节点的去操作肯定会超时,但是一段序列中颜色不同的时候又不能进行区间更新。我们就设置一个数组去按着线段树更新的样子去记录每一段区间里颜色。只有序列颜色一样了才去更新这一区间。
代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#define ll long long
using namespace std;

const int maxx=1e5+100;
struct node{
	int l;
	int r;
	ll lazy;
	ll sum;
	ll cor;
}p[maxx<<2];
int n,m;

void pushup(int cur)
{
	p[cur].cor=(p[cur<<1].cor==p[cur<<1|1].cor?p[cur<<1].cor:0);//只有左右子节点颜色都一样才把父节点标记为有颜色,否则就标记为0
	p[cur].sum=p[cur<<1].sum+p[cur<<1|1].sum;
}
void pushdown(int cur)
{
	if(p[cur].lazy)
	{
		p[cur<<1].lazy+=p[cur].lazy;
		p[cur<<1|1].lazy+=p[cur].lazy;
		p[cur<<1|1].sum+=(p[cur].lazy*(p[cur<<1|1].r-p[cur<<1|1].l+1));
		p[cur<<1].sum+=(p[cur].lazy*(p[cur<<1].r-p[cur<<1].l+1));
		p[cur<<1].cor=p[cur<<1|1].cor=p[cur].cor;
		p[cur].lazy=0;
	}
}
void build(int l,int r,int cur)
{
	p[cur].l=l;
	p[cur].r=r;
	p[cur].sum=p[cur].lazy=p[cur].cor=0;
	if(l==r)
	{
		p[cur].cor=(ll)l;
		return ;
	}
	int mid=(l+r)/2;
	build(l,mid,cur<<1);
	build(mid+1,r,cur<<1|1);
	pushup(cur);
}
void update(int l,int r,int cur,ll add)
{
	int L=p[cur].l;
	int R=p[cur].r;
	if(l<=L&&R<=r&&p[cur].cor)
	{
		p[cur].sum+=(abs(add-p[cur].cor)*(R-L+1));
		p[cur].lazy+=(abs(add-p[cur].cor));
		p[cur].cor=add;
		//cout<<p[cur].sum<<endl;
		return ;
	}
	pushdown(cur);
	int mid=(L+R)/2;
	if(r<=mid) update(l,r,2*cur,add);
	else if(l>mid) update(l,r,2*cur+1,add);
	else 
	{
		update(l,mid,2*cur,add);
		update(mid+1,r,2*cur+1,add);
	}
	pushup(cur);
}
ll query(int l,int r,int cur)
{
	int L=p[cur].l;
	int R=p[cur].r;
	if(l<=L&&R<=r)
	{
		return p[cur].sum;
	}
	pushdown(cur);
	int mid=(L+R)/2;
	if(r<=mid) return query(l,r,2*cur);
	else if(l>mid) return query(l,r,2*cur+1);
	else return query(l,mid,2*cur)+query(mid+1,r,2*cur+1);
}

int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		build(1,n,1);
		int x,y;ll z;int k;
		while(m--)
		{
			scanf("%d",&k);
			if(k==1)
			{
				scanf("%d%d%lld",&x,&y,&z);
				update(x,y,1,z);
			}
			else if(k==2)
			{
				scanf("%d%d",&x,&y);
				printf("%lld\n",query(x,y,1));
			}
		}
	}
	return 0;
}

努力加油a啊,(o)/~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值