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 thei-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:

  1. Paint all the units with numbers between l and r (both
    inclusive) with color x.
  2. 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.

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 is 8.

线段树的操作,因为涉及到·类似染色的问题。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
typedef long long LL;
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
const int maxn=1e5+100;
LL col[maxn<<2],sum[maxn<<2],d[maxn<<2];//col[i]!=0代表区间颜色都为col[i];d[i]用于lazy操作
int n,m;
void pushup(int rs)
{
if(col[rs<<1]==col[rs<<1|1]) col[rs]=col[rs<<1];
else col[rs]=0;
sum[rs]=sum[rs<<1]+sum[rs<<1|1];
}
void pushdown(int rs,int m)
{
if(col[rs])
{
col[rs<<1]=col[rs<<1|1]=col[rs];
d[rs<<1]+=d[rs];d[rs<<1|1]+=d[rs];
sum[rs<<1]+=(LL)(m-(m>>1))*d[rs];
sum[rs<<1|1]+=(LL)(m>>1)*d[rs];
d[rs]=col[rs]=0;
}
}
void build(int rs,int l,int r)
{
if(l==r)
{
sum[rs]=0;
col[rs]=l;
return ;
}
col[rs]=d[rs]=0;
int mid=(l+r)>>1;
build(rs<<1,l,mid);
build(rs<<1|1,mid+1,r);
pushup(rs);
}
void update(int rs,int x,int y,int l,int r,int c)
{
if(l>=x&&r<=y&&col[rs])
{
sum[rs]+=abs(col[rs]-c)*(LL)(r-l+1);
d[rs]+=abs(col[rs]-c);
col[rs]=c;
return ;
}
pushdown(rs,r-l+1);
int mid=(l+r)>>1;
if(x<=mid) update(rs<<1,x,y,l,mid,c);
if(y>mid) update(rs<<1|1,x,y,mid+1,r,c);
pushup(rs);
}
LL query(int rs,int x,int y,int l,int r)
{
// cout<<"2333 "<<<<endl;
if(l>=x&&y>=r)
return sum[rs];
// if(l==r) return sum[rs];
int mid=(l+r)>>1;
pushdown(rs,r-l+1);
LL res=0;
if(x<=mid) res+=query(rs<<1,x,y,l,mid);
if(y>mid) res+=query(rs<<1|1,x,y,mid+1,r);
return res;
}
int main()
{
// std::ios::sync_with_stdio(false);
int l,r,x,op;
while(~scanf("%d%d",&n,&m))
{
build(1,1,n);
while(m--)
{
scanf("%d",&op);
if(op==1)
{
scanf("%d%d%d",&l,&r,&x);
update(1,l,r,1,n,x);
}
else
{
scanf("%d%d",&l,&r);
printf("%I64d\n",query(1,l,r,1,n));
}
}
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

Cf 444C DZY Loves Colors(段树)的更多相关文章

  1. Codeforces 444C DZY Loves Colors(线段树)

    题目大意:Codeforces 444C DZY Loves Colors 题目大意:两种操作,1是改动区间上l到r上面德值为x,2是询问l到r区间总的改动值. 解题思路:线段树模板题. #inclu ...

  2. Codeforces Round #254 (Div. 1) C. DZY Loves Colors 线段树

    题目链接: http://codeforces.com/problemset/problem/444/C J. DZY Loves Colors time limit per test:2 secon ...

  3. CF444C. DZY Loves Colors[线段树 区间]

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

  4. HDU 5649.DZY Loves Sorting-线段树+二分-当前第k个位置的数

    DZY Loves Sorting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Oth ...

  5. CF 444C DZY Loves Physics(图论结论题)

    题目链接: 传送门 DZY Loves Chemistry time limit per test1 second     memory limit per test256 megabytes Des ...

  6. Codeforces 444 C. DZY Loves Colors (线段树+剪枝)

    题目链接:http://codeforces.com/contest/444/problem/C 给定一个长度为n的序列,初始时ai=i,vali=0(1≤i≤n).有两种操作: 将区间[L,R]的值 ...

  7. codeforces 444 C. DZY Loves Colors(线段树)

    题目大意: 1 l r x操作 讲 [l,r]上的节点涂成x颜色,而且每一个节点的值都加上 |y-x| y为涂之前的颜色 2 l r  操作,求出[l,r]上的和. 思路分析: 假设一个区间为同样的颜 ...

  8. Codeforces444C DZY Loves Colors(线段树)

    题目 Source http://codeforces.com/problemset/problem/444/C Description DZY loves colors, and he enjoys ...

  9. Codeforces Round #254 (Div. 1) C. DZY Loves Colors 分块

    C. DZY Loves Colors 题目连接: http://codeforces.com/contest/444/problem/C Description DZY loves colors, ...

随机推荐

  1. 为大型数据文件每行只能产生id

    为大型数据文件每行只能产生id 4个主要思路: 1 单线程处理 2 普通多线程 3 hive 4 Hadoop 搜到一些參考资料 <Hadoop实战>的笔记-2.Hadoop输入与输出 h ...

  2. ACM字符串处理算法经典:字符串搜索

    语法:result=strfind(char str[],char key[]); 参数: str[]:在这个源字符串查找操作 key[]:搜索字符串.不能为空字符串 回报值:     假设查找成功. ...

  3. C#程序(含多个Dll)合并成一个Exe

    把C#程序(含多个Dll)合并成一个Exe的超简单方法   开发程序的时候经常会引用一些第三方的DLL,然后编译生成的exe文件就不能脱离这些DLL独立运行了. 但是,很多时候我们本想开发一款只需要一 ...

  4. asp.net使用post方式action到另一个页面,在另一个页面接受form表单的值!(报错,已解决!)

    原文:asp.net使用post方式action到另一个页面,在另一个页面接受form表单的值!(报错,已解决!) 我想用post的方式把一个页面表单的值,传到另一个页面.当我点击Default.as ...

  5. Benchmark与Profiler---性能调优得力助手

    转载请注明出处:http://blog.csdn.net/gaoyanjie55/article/details/34981077 性能优化.它是一种诊断性能瓶颈,能问题点进行优化的过程.前两天听完s ...

  6. 键盘enter事件时间页面绑定

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. socket套接字TCP API

    socket套接字TCP API socket概念 socket又称"套接字",是计算机网络中进程间通信数据通道的一个端点.或称之为句柄.IP地址+port号就能够唯一确定一个so ...

  8. Notification使用以及PendingIntent.getActivity() (转)

    public void sendNotification(Context ctx,String message) { //get the notification manager String ns ...

  9. hello nodejs

    文章1一步:下载.安装文件 打开nodejs官方网站http://www.nodejs.org/download/ .选择须要的版本号.直接打开.默认安装就可以 第二步:编写測试代码: var htt ...

  10. Learning JavaScript(0)_Concepts

    作用域,嵌套函数和闭包 <script type="text/javascript"> function foo(){ var a = 10; function bar ...