CF 254Div1 C.DZY Loves Colors

C. DZY Loves Colors
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 ton 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 is0 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 uniti currently is y. When it is painted by this paintbrush, the color of the unit becomesx, and the colorfulness of the unit increases by|x - y|.

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

  1. Paint all the units with numbers between l andr (both inclusive) with color x.
  2. Ask the sum of colorfulness of the units between l andr (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 integertype (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 operation1.

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

Output

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

Sample test(s)
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 is8.

解析

線段樹。

線段樹主要有3個重要的節點信息。

mark:節點[l,r]全部是顏色mark。雜色的mark==0

sum:節點[l,r]的和。

col(相當於laz):形如add(l,r,k)的所有k之和。節點[l,r]的sum=sum[左兒子]+sum[右兒子]+區間長度*col

如果你還是不明白sum和col的關係,可以看看下面的ppt

注意,cf的long long 坑死人啦。運算的時候一定要強轉long long

#include<cstdio>
#include<algorithm>
#include<cstdlib>
using namespace std;
#define MaxN 100100
typedef long long LL;

struct node
{
	int l,r,mark;
	LL sum,col;//col==lazy
} tree[MaxN*4];
int N,M;

void build(int p,int l,int r)
{
	tree[p].l=l; tree[p].r=r;
	if(l==r) {tree[p].mark=l;return;}
	int mid=(l+r)>>1;
	build(p<<1,l,mid);
	build((p<<1)+1,mid+1,r);
}

void clear(int p,int x)
{
	if(tree[p].mark)
	{
		tree[p].col+=abs(tree[p].mark-x);
		tree[p].sum+=((long long)tree[p].r-tree[p].l+1)*abs(tree[p].mark-x);//一定要強轉long long
		tree[p].mark=x;
	}
	else
	{
		if(tree[p].l==tree[p].r) return;
		clear(p<<1,x);
		clear(p*2+1,x);
		tree[p].sum=tree[p<<1].sum+tree[p*2+1].sum+((long long)tree[p].r-tree[p].l+1)*tree[p].col;//一定要強轉long long
	}
}

void modify(int p,int ql,int qr,int x)
{
	if(ql<=tree[p].l && tree[p].r<=qr)
	{
		clear(p,x);
		tree[p].mark=x;
		return;
	}
	int mid=(tree[p].l+tree[p].r)>>1;
	if(tree[p].mark)
	{tree[p<<1].mark=tree[p*2+1].mark=tree[p].mark;tree[p].mark=0;}

	if(ql<=mid) modify(p<<1,ql,qr,x);
	if(mid<qr) modify(p*2+1,ql,qr,x);
	tree[p].sum=tree[p<<1].sum+tree[p*2+1].sum+((long long)tree[p].r-tree[p].l+1)*(long long)tree[p].col;//一定要強轉long long
}

LL sum(int p,int ql,int qr)
{
	if(ql<=tree[p].l && tree[p].r<=qr) return tree[p].sum;
	int mid=(tree[p].l+tree[p].r)>>1;
	LL l=0,r=0;
	if(ql<=mid) l=sum(p<<1,ql,qr);
	if(mid<qr) r=sum(p*2+1,ql,qr);
	return l+r+max(0,min(tree[p].r,qr)-max(tree[p].l,ql)+1)*(long long)tree[p].col;//一定要強轉long long
}
int main()
{
	scanf("%d%d",&N,&M);
	build(1,1,N);
	for(int i=1;i<=M;i++)
	{
		int typ; scanf("%d",&typ);
		if(typ==1)
		{
			int l,r,x; scanf("%d%d%d",&l,&r,&x);
			modify(1,l,r,x);
		}
		if(typ==2)
		{
			int l,r; scanf("%d%d",&l,&r);
			printf("%I64d\n",sum(1,l,r));
		}
	}

	//while(1);
	return 0;
}


發佈了123 篇原創文章 · 獲贊 1 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章