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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Process: com.example.dzy, PID: 26008 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dzy/com.example.dzy.NavigationActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference at com.example.dzy.Fragment_1.<init>(Fragment_1.java:44) at com.example.dzy.NavigationActivity.initTab(NavigationActivity.java:39) at com.example.dzy.NavigationActivity.onCreate(NavigationActivity.java:27) at android.app.Activity.performCreate(Activity.java:7802) at android.app.Activity.performCreate(Activity.java:7791) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
06-01

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值