DZY Loves Colors

Time Limit: 2000ms
Memory Limit: 262144KB

This problem will be judged on CodeForces. Original ID: 445E
64-bit integer IO format: %I64d      Java class name: (Any)

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.

 

Sample Input

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

Hint

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.

 

Source

 
解题:线段树
 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = ;
struct node{
int lt,rt,color;
LL sum,len,add;
}tree[maxn<<];
void pushup(int v){
tree[v].sum = tree[v<<].sum + tree[v<<|].sum;
if(tree[v<<].color == tree[v<<|].color)
tree[v].color = tree[v<<].color;
else tree[v].color = ;
}
void pushdown(int v){
if(tree[v].add) {
tree[v<<].add += tree[v].add;
tree[v<<|].add += tree[v].add;
tree[v<<].sum += tree[v].add*tree[v<<].len;
tree[v<<|].sum += tree[v].add*tree[v<<|].len;
tree[v].add = ;
}
if(tree[v].color){
tree[v<<].color = tree[v<<|].color = tree[v].color;
tree[v].color = ;
}
}
void build(int lt,int rt,int v){
tree[v].lt = lt;
tree[v].rt = rt;
tree[v].len = rt - lt + ;
tree[v].add = ;
tree[v].sum = ;
if(lt == rt){
tree[v].color = lt;
return;
}
int mid = (lt + rt)>>;
build(lt,mid,v<<);
build(mid+,rt,v<<|);
pushup(v);
}
void update(int lt,int rt,int color,int v){
if(lt <= tree[v].lt && rt >= tree[v].rt && tree[v].color){
tree[v].add += abs(tree[v].color - color);
tree[v].sum += abs(tree[v].color - color)*tree[v].len;
tree[v].color = color;
return;
}
pushdown(v);
if(lt <= tree[v<<].rt) update(lt,rt,color,v<<);
if(rt >= tree[v<<|].lt) update(lt,rt,color,v<<|);
pushup(v);
}
LL query(int lt,int rt,int v){
if(lt <= tree[v].lt && rt >= tree[v].rt) return tree[v].sum;
pushdown(v);
LL sum = ;
if(lt <= tree[v<<].rt) sum += query(lt,rt,v<<);
if(rt >= tree[v<<|].lt) sum += query(lt,rt,v<<|);
pushup(v);
return sum;
}
int main(){
int n,m,op,x,y,color;
scanf("%d%d",&n,&m);
build(,n,);
while(m--){
scanf("%d%d%d",&op,&x,&y);
if(op == ){
scanf("%d",&color);
update(x,y,color,);
}else printf("%I64d\n",query(x,y,));
}
return ;
}

CodeForces 445E 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. Codeforces Round #254 (Div. 1) C. DZY Loves Colors 分块

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

  4. Codeforces 444 C - DZY Loves Colors

    C - DZY Loves Colors 思路: 分块,复杂度有点玄学,和普通分块不同的是在这个块被一次染色的时候暴力染整个块. 代码: #pragma GCC optimize(2) #pragma ...

  5. Codeforces444C DZY Loves Colors(线段树)

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

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

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

  7. Cf 444C DZY Loves Colors(段树)

    DZY loves colors, and he enjoys painting. On a colorful day, DZY gets a colorful ribbon, which consi ...

  8. Codeforces 444A DZY Loves Physics(图论)

    题目链接:Codeforces 444A DZY Loves Physics 题目大意:给出一张图,图中的每一个节点,每条边都有一个权值.如今有从中挑出一张子图,要求子图联通,而且被选中的随意两点.假 ...

  9. CodeForces 445B DZY Loves Chemistry

    DZY Loves Chemistry Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64 ...

随机推荐

  1. C语言基本语法——变量

    1.变量作用域 2.局部变量 3.全局变量 4.变量生命周期 5.auto关键字 6.static关键字 1.变量作用域 • 变量作用域是指变量的有效范围 • 变量作用域是定义变量从何处被创建,到何处 ...

  2. QT_圆_直线_三角t

    MyImgTest.h: #ifndef MYIMGTEST_H#define MYIMGTEST_H #include <QWidget> class MyImgTest : publi ...

  3. axios统一拦截配置

    在vue项目中,和后台进行数据交互使用axios.要想统一处理所有的http请求和响应,就需要使用axios的拦截器.通过配置http response inteceptor 统一拦截后台的接口数据, ...

  4. 【BZOJ 1486】 [HNOI2009]最小圈

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 我们可以只想那个均值最小的环. 我们不知道那个环由哪些边构成 但我们可以把每条边都减掉mid 那个环受到的影响是什么呢? 如果这个均 ...

  5. HAVING使用子查询

    HAVING使用子查询     //查询各部门平均工资,显示平均工资大于   //公司整体平均工资的记录   select deptno,avg(sal)   from emp   group by ...

  6. 设计模式实例(Lua)笔记之七(Decorator模式)

    1.描写叙述 就说说"我"上小学的的糗事吧. 我上小学的时候学习成绩非常的差,班级上 40 多个同学,我基本上都是在排名 45 名以后,依照老师给我的定义就是"不是读书的 ...

  7. 接口測试-HAR

    參考文章 雪球的 HttpApi 接口測试框架设计 HAR(HTTP Archive)规范 神器--Chrome开发人员工具(一) HAR是什么 一句话:关于HTTP所有的信息的一种文件保存格式 HA ...

  8. wikioi 1306 机智Trie树

    题目描写叙述 Description 看广播操无聊得非常~你有认为吗?在看广播操一波又一波的人潮涌过再退去.认为非常没意思--于是,偶们的大神犇JHT发明了一个及其好玩的游戏~ 把每一班级的队形看成一 ...

  9. linux使用windows中编辑的文件,格式问题

    参考:https://blog.csdn.net/yongan1006/article/details/8142527 运行脚本时出现了这样一个错误,打开之后并没有找到所谓的^M,查了之后才知道原来是 ...

  10. 山东理工oj--1912--IP地址(水题)

     IP地址 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 2011年2月3日,国际互联网名称与数字地址分配机构(ICANN) ...