DZY Loves Colors CodeForces - 444C(线段树)

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.
忘记之前有没有写过这个题的博客了。。
我记得之前做过这个题,但是没有记录,本地也没有代码,于是又写了一遍,果然还是错误百出。
两种操作,我们只有在这个区间内颜色都一样的时候,才能去更新。否则就不能更新。lazy标记直接标记差值的绝对值就好了。
代码如下:

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

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

void pushup(int cur)
{
	p[cur].v=p[cur<<1].v+p[cur<<1|1].v;
	p[cur].cor=(p[cur<<1].cor==p[cur<<1|1].cor)?p[cur<<1].cor:0;
}
void pushdown(int cur)
{
	if(p[cur].cor&&p[cur].lazy)
	{
		p[cur<<1].lazy+=p[cur].lazy;
		p[cur<<1|1].lazy+=p[cur].lazy;
		p[cur<<1].v+=abs(p[cur].lazy)*(ll)(p[cur<<1].r-p[cur<<1].l+1);
		p[cur<<1|1].v+=abs(p[cur].lazy)*(ll)(p[cur<<1|1].r-p[cur<<1|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].v=p[cur].lazy=0;
	if(l==r)
	{
		p[cur].cor=(ll)l;
		return ;
	}
	int mid=l+r>>1;
	build(l,mid,cur<<1);
	build(mid+1,r,cur<<1|1);
	pushup(cur);
}
void update(int cur,int l,int r,ll c)
{
	int L=p[cur].l;
	int R=p[cur].r;
	if(p[cur].cor==c) return ;
	if(l<=L&&R<=r&&p[cur].cor)
	{
		p[cur].v+=abs(p[cur].cor-c)*(ll)(R-L+1);
		p[cur].lazy+=abs(c-p[cur].cor);
		p[cur].cor=c;
		return ;
	}
	pushdown(cur);
	int mid=L+R>>1;
	if(r<=mid) update(cur<<1,l,r,c);
	else if(l>mid) update(cur<<1|1,l,r,c);
	else 
	{
		update(cur<<1,l,mid,c);
		update(cur<<1|1,mid+1,r,c);
	}
	pushup(cur);
}
ll query(int cur,int l,int r)
{
	int L=p[cur].l;
	int R=p[cur].r;
	if(l<=L&&R<=r)
	{
		return p[cur].v;
	}
	pushdown(cur);
	int mid=L+R>>1;
	ll ans=0;
	if(r<=mid) ans=query(cur<<1,l,r);
	else if(l>mid) ans=query(cur<<1|1,l,r);
	else ans=query(cur<<1,l,mid)+query(cur<<1|1,mid+1,r);
	pushup(cur);
	return ans;
}
int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		build(1,n,1);
		int op,l,r;
		ll c;
		while(m--)
		{
			scanf("%d%d%d",&op,&l,&r);
			if(op==1)
			{
				scanf("%lld",&c);
				update(1,l,r,c);
			}
			else if(op==2) printf("%lld\n",query(1,l,r));
		}
	}
	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、付费专栏及课程。

余额充值