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. php怎么获取input输入框中的值去数据库比较显示出来

    前端: <!--商品查询--> <input type="text" name="bianhao" value="" ma ...

  2. [修正] 移动平台曲线不平滑的问题(如:TRectangle, TPath...等)

    问题:从 XE4 以来,Firemonkey 曲线绘图在移动平台不平滑的问题一直令人诟病,提交到官方的 QC 也是族繁不及备载,官方似乎有意的避开这个问题,迟迟没有修正. 适用版本:XE4 ~ Ber ...

  3. junit4 assert类中的assert方法总结

    junit中的assert方法全部放在Assert类中,总结一下junit类中assert方法的分类. 1.assertTrue/False([String message,]boolean cond ...

  4. Ant_build.xml的最完整解释

    Ant的概念Make命令是一个项目管理工具,而Ant所实现功能与此类似.像make,gnumake和nmake这些编译工具都有一定的缺陷,但是Ant却克服了这些工具的缺陷.最初Ant开发者在开发跨平台 ...

  5. Combobox的使用

    第一次写博客,只是对自己在工作中遇到的问题进行一次总结回顾,为以后有同样的错误有一个参考: 由于最近空余时间很少,只是零零散散的把平时记录的笔记搬到博客园而已,博客中可能出现一些低级错误,希望互相学习 ...

  6. 微信小程序开发总结

    一.设计 无需开发者开发的 1.小程序加载动画: 2.页面下拉刷新加载样式: 3.微信控件(拥有完整的操作反馈):如弹出框.通知.模态框...   建议用微信自己的 1.加载.反馈样式(全局.局部) ...

  7. 网页中tab标签切换分别用jquery和javascript源码实现

    //HTML布局<ul id="tabTitle"> <li class="active">HTML5</li> <l ...

  8. 基本排序算法——冒泡排序java实现

    冒泡排序是原理最简单的一种排序算法,具体思想就不多说了,代码如下: eclipse4.3中编译通过 package sort.basic; import java.util.Arrays; publi ...

  9. ERP入门

    为什么想起写这个题目哪?其实这个问题很久就想写了,记得2005年时候,公司新招的二位刚毕业的大学生,一个专业是经济管理,一个是会计,东北大区培训后公司让我选择了一位带一带,我选择了一个会计专业的(因为 ...

  10. python的基础知识

    Python文件命名时不要有中文,不然在dos中不能执行 D:\Program Files\Py>Python hellyy.pyYear:2016Month(1-12):1Day(1-31): ...