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

  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.

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.


题意:区间修改颜色,colorfulness+=颜色差的绝对值,区间查询colorfulness


线段树区间更新和区间查询

col记录颜色,0说明颜色不同;sum是colorfulness;lazy记录增量(colorfulness的增量)

更新的时候遇到相同颜色的被包含的区间直接更新,否则下传标记更新孩子然后合并

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define m ((l+r)>>1)
#define lson o<<1,l,m
#define rson o<<1|1,m+1,r
#define lc o<<1
#define rc o<<1|1
using namespace std;
typedef long long ll;
const int N=5e5+,INF=2e9+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,q,op,ql,qr,x;
struct node{
ll sum,lazy,col;
}t[N<<];
void merge(int o){
t[o].col=t[lc].col!=t[rc].col?:t[lc].col;
t[o].sum=t[lc].sum+t[rc].sum;
}
void build(int o,int l,int r){
if(l==r) t[o].col=l;
else{
build(lson);
build(rson);
}
}
void pushDown(int o,int len){
if(t[o].col){
t[lc].col=t[rc].col=t[o].col;
t[lc].lazy+=t[o].lazy;
t[rc].lazy+=t[o].lazy;
t[lc].sum+=t[o].lazy*(len-(len>>));
t[rc].sum+=t[o].lazy*(len>>); t[o].col=t[o].lazy=;
}
}
void update(int o,int l,int r,int ql,int qr,ll v){//printf("update %d %d %d\n",o,l,r);
if(ql<=l&&r<=qr&&t[o].col){
t[o].sum+=(r-l+)*abs(v-t[o].col);
t[o].lazy+=abs(v-t[o].col);
t[o].col=v;
}else{
pushDown(o,r-l+);
if(ql<=m) update(lson,ql,qr,v);
if(m<qr) update(rson,ql,qr,v);
merge(o);
}
}
ll query(int o,int l,int r,int ql,int qr){
if(ql<=l&&r<=qr) return t[o].sum;
else{
pushDown(o,r-l+);
ll ans=;
if(ql<=m) ans+=query(lson,ql,qr);
if(m<qr) ans+=query(rson,ql,qr);
return ans;
}
}
int main(){
n=read();q=read();
build(,,n);
for(int i=;i<=q;i++){
op=read();ql=read();qr=read();
if(op==){x=read();update(,,n,ql,qr,x);}
else printf("%I64d \n",query(,,n,ql,qr));
}
}

CF444C. DZY Loves Colors[线段树 区间]的更多相关文章

  1. 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 ...

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

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

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

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

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

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

  5. CF444C DZY Loves Colors

    考试完之后打的第一场CF,异常惨烈呀,又只做出了一题了.A题呆滞的看了很久,领悟到了出题者的暗示,应该就是两个点的时候最大吧,不然的话这题肯定特别难敲,YY一发交上去然后就过了.然后就在不停地YY B ...

  6. HDU5649 DZY Loves Sorting 线段树

    题意:BC 76 div1 1004 有中文题面 然后奉上官方题解: 这是一道良心的基础数据结构题. 我们二分a[k]的值,假设当前是mid,然后把大于mid的数字标为1,不大于mid的数字标为0.然 ...

  7. ZOJ1610 Count the Colors —— 线段树 区间染色

    题目链接:https://vjudge.net/problem/ZOJ-1610 Painting some colored segments on a line, some previously p ...

  8. ZOJ 1610 Count the Colors(线段树,区间覆盖,单点查询)

    Count the Colors Time Limit: 2 Seconds      Memory Limit: 65536 KB Painting some colored segments on ...

  9. zoj 1610 Count the Colors 线段树区间更新/暴力

    Count the Colors Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/show ...

随机推荐

  1. SQL Server:APPLY表运算符

    SQL Server 2005(含)以上版本,新增了APPLY表运算,为我们日常查询带来了极大的方便. 新增的APPLY表运算符把右表表达式应用到左表表达式中的每一行.它不像JOIN那样先计算那个表表 ...

  2. Asp.Net Core子应用由于配置中重复添加模块会引起IIS错误500.19

    ASP.NET Core已经从IIS中解耦,可以作为自宿主程序运行,不再依赖IIS. 但我们还是需要强大的IIS作为前置服务器,IIS利用httpPlatformHandler模块来对后台的一些web ...

  3. android studio 1.0 开发 ndk 调用 c++ so库

    一个没用过java和安卓的人使用android studio开发带c++ so库的安卓程序用例(以ndk的hello-jni为例),对于不熟悉java和安卓的人来说这个很花时间,希望通过这篇文章帮助跟 ...

  4. 如何在MFC界面开发中响应Button按钮的Down和Up事件

    通过尝试有两种方案可以解决这个问题,第一种方案是通过PreTranslateMessage函数在调度消息之前对消息类型进行筛选,第二种方案是重载CButton类,在重载后的类CForTestButto ...

  5. 一元多项式的乘法与加法运算(C语言)

    输入格式: 输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数).数字间以空格分隔. 输出格式: 输出分2行,分别以指数递降方 ...

  6. 电脑上不安装Oracle时,C# 调用oracle数据库,Oracle客户工具

    Oracle的安装包通常都比较大,安装又比较费时,而且如果安装过程中不幸出错,各种蛋疼,即便是安装过N遍的老手,有时候安装起来也觉得挺烦.而工作中,通常服务器上面安装oracle就可以了,我们本地电脑 ...

  7. autocomplete="off" 不起作用

    首先来了解一下 表单自动填充的原理,当我们登录的时候,如果选择的记住登录密码,那么后续界面中如果有 <input type="text" name="field1& ...

  8. git下载指定版本的代码

    1. git fetch https://github.com/angular/angular.js.git v1.5.8 或 2. git pull https://github.com/angul ...

  9. C++实现Ping

    这是一个老话题了,但是我刚学会... 我们的目的是实现这么个东西: 之所以用红框框一下是因为,从baidu.com到123.125.114.144的过程是DNS解析,我们暂时先实现ping的部分. 基 ...

  10. JavaScript的个人学习随手记(三)

    JavaScript Window - 浏览器对象模型 Window 对象 以下window对象时使用均可省略window 所有浏览器都支持 window 对象.它表示浏览器窗口. 所有 JavaSc ...