博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CF444C. DZY Loves Colors[线段树 区间]
阅读量:5776 次
发布时间:2019-06-18

本文共 3485 字,大约阅读时间需要 11 分钟。

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 
#include
#include
#include
#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|1using namespace std;typedef long long ll;const int N=5e5+5,INF=2e9+5;inline int read(){ char c=getchar();int x=0,f=1; while(c<'0'||c>'9'){
if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} return x*f;}int n,q,op,ql,qr,x;struct node{ ll sum,lazy,col;}t[N<<1];void merge(int o){ t[o].col=t[lc].col!=t[rc].col?0: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>>1)); t[rc].sum+=t[o].lazy*(len>>1); t[o].col=t[o].lazy=0; }}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+1)*abs(v-t[o].col); t[o].lazy+=abs(v-t[o].col); t[o].col=v; }else{ pushDown(o,r-l+1); if(ql<=m) update(lson,ql,qr,v); if(m

 

转载地址:http://erhux.baihongyu.com/

你可能感兴趣的文章
java里调用构造器会不会一定产生新对象
查看>>
Elasticsearch
查看>>
引用计数算法
查看>>
Java 复习 —— 守护线程以及线程监测工具
查看>>
MAVEN私服搭建
查看>>
GIT win cmd 中文乱码
查看>>
const在c和c++中的区别,案例说明
查看>>
x86/x86_64的一些基本概念
查看>>
给 Android 开发者的 RxJava 详解
查看>>
jdk11 HttpClient 爬虫
查看>>
数据结构与算法_Index
查看>>
12.13 angular 指令与控制器交互
查看>>
关键字 using ....for ...
查看>>
一些小技巧。
查看>>
mac 下安装imagemagick和PHP扩展Rmagick
查看>>
PHP利用公钥私钥进行高强度加密
查看>>
SpringMVC4+hibernate遇到 readOnly问题FlushMode.MANUAL
查看>>
深入浅出OOP(三): 多态和继承(动态绑定/运行时多态)
查看>>
学习spring必须java基础知识-注解(annotation)
查看>>
怎么给ChemDraw反应式添加分数系数
查看>>