题目链接:

http://codeforces.com/problemset/problem/444/C

J. DZY Loves Colors

time limit per test:2 seconds
memory limit per test:256 megabytes
#### 问题描述
> 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:
>
> Paint all the units with numbers between l and r (both inclusive) with color x.
> Ask the sum of colorfulness of the units between l and r (both inclusive).
> Can you help DZY?
#### 输入
> 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.
#### 输出
> For each operation 2, print a line containing the answer — sum of colorfulness.
#### 样例
> **sample input**
> 3 3
> 1 1 2 4
> 1 2 3 5
> 2 1 3
>
> **sample output**
> 8

题意

初始的时候第i个位子的颜色是i,每个位子的值是0,现在用一把刷子,能把一段区间刷成颜色y,对于每个位子的值会增加abs(y-x)(x代表原先的颜色)。 并且给你区间(l,r),要你输出当前区间的值的和是多少

题解

线段树。

和普通的区间更新有点不一样,因为你刷一个区间,由于区间内有可能不止一种颜色,那你就没办法马上算出贡献值了。

所以我们增加一个Clear()函数,当我们找到需要更新的子区间的时候,在打标标记之前,先Clear()一下,把子区间下面的区间的标记统统清除掉,同时把更新的值维护上来。(我们只有当clear()到一段颜色相同的区间的时候,才能更新,否则就Clear()递归下去。)

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define lson (o<<1)
#define rson ((o<<1)|1)
#define M l+(r-l)/2
using namespace std; const int maxn=1e5+10;
typedef __int64 LL; //mark标记区间的颜色
//sumv计算区间的贡献值的和
//addv计算区间的增量
LL sumv[maxn<<2],addv[maxn<<2];
LL mark[maxn<<2];
int n,m; void pushdown(int o){
if(mark[o]>0){
mark[lson]=mark[rson]=mark[o];
mark[o]=0;
}
} void build(int o,int l,int r){
if(l==r){
mark[o]=l;
}else{
build(lson,l,M);
build(rson,M+1,r);
mark[o]=0;
}
} void maintain(int o,int l,int r){
sumv[o]=sumv[lson]+sumv[rson]+addv[o]*(r-l+1);
} int ql,qr,_v;
//Clear()到一段颜色相同的区间才能计算贡献值
void Clear(int o,int l,int r){
if(mark[o]>0){
addv[o]+=abs(mark[o]-_v);
sumv[o]+=abs(mark[o]-_v)*(r-l+1);
}else{
Clear(lson,l,M);
Clear(rson,M+1,r);
maintain(o,l,r);
}
mark[o]=0;
} void update(int o,int l,int r){
if(ql<=l&&r<=qr){
Clear(o,l,r);
mark[o]=_v;
}else{
pushdown(o);
if(ql<=M) update(lson,l,M);
if(qr>M) update(rson,M+1,r);
maintain(o,l,r);
}
} LL _sum;
void query(int o,int l,int r,LL add){
if(ql<=l&&r<=qr){
_sum+=sumv[o]+add*(r-l+1);
}else{
if(ql<=M) query(lson,l,M,add+addv[o]);
if(qr>M) query(rson,M+1,r,add+addv[o]);
}
} void init(){
memset(sumv,0,sizeof(sumv));
memset(addv,0,sizeof(addv));
memset(mark,0,sizeof(mark));
} int main(){
init();
scanf("%d%d",&n,&m);
build(1,1,n);
while(m--){
int cmd;
scanf("%d%d%d",&cmd,&ql,&qr);
if(cmd==1){
scanf("%d",&_v);
update(1,1,n);
}else{
_sum=0;
query(1,1,n,0);
printf("%I64d\n",_sum);
}
}
return 0;
}

Codeforces Round #254 (Div. 1) C. DZY Loves Colors 线段树的更多相关文章

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

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

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

    http://codeforces.com/contest/444/problem/C 题意:给出一个数组,初始时每个值从1--n分别是1--n.  然后两种操作. 1:操作 a.b内的数字是a,b内 ...

  3. Codeforces Round #254 (Div. 1) D - DZY Loves Strings

    D - DZY Loves Strings 思路:感觉这种把询问按大小分成两类解决的问题都很不好想.. https://codeforces.com/blog/entry/12959 题解说得很清楚啦 ...

  4. Codeforces Round #254 (Div. 1) D. DZY Loves Strings hash 暴力

    D. DZY Loves Strings 题目连接: http://codeforces.com/contest/444/problem/D Description DZY loves strings ...

  5. Codeforces Round #254 (Div. 1) A. DZY Loves Physics 智力题

    A. DZY Loves Physics 题目连接: http://codeforces.com/contest/444/problem/A Description DZY loves Physics ...

  6. Codeforces Round #254 (Div. 2) A. DZY Loves Chessboard —— dfs

    题目链接: http://codeforces.com/problemset/problem/445/A 题解: 这道题是在现场赛的最后一分钟通过的,相当惊险,而且做的过程也很曲折. 先是用递推,结果 ...

  7. Codeforces Round #254 (Div. 2)B. DZY Loves Chemistry

    B. DZY Loves Chemistry time limit per test 1 second memory limit per test 256 megabytes input standa ...

  8. [题解]Codeforces Round #254 (Div. 2) B - DZY Loves Chemistry

    链接:http://codeforces.com/contest/445/problem/B 描述:n种药品,m个反应关系,按照一定顺序放进试管中.如果当前放入的药品与试管中的药品要反应,危险系数变为 ...

  9. [题解]Codeforces Round #254 (Div. 2) A - DZY Loves Chessboard

    链接:http://codeforces.com/contest/445/problem/A 描述:一个n*m的棋盘,有一些格子不能放棋子.现在把黑白棋子往上放,要求放满且相邻格子的棋子颜色不同.输出 ...

随机推荐

  1. C#局域网桌面共享软件制作(二)

    链接C#局域网桌面共享软件制作(一) 如果你运行这个软件查看流量监控就会发现1~2M/s左右的上传下载,并且有时会报错“参数无效”,如果你将屏幕截图保存到本地的话每张图片大概4M(bmp).120KB ...

  2. POJ C++程序设计 编程题#4 字符串操作

    编程题#4: 字符串操作 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 给 ...

  3. nginx php 安装

    .选定源码目录选定目录 /data/klj/ cd /data/klj/ 2.安装PCRE库cd /data/klj/wget ftp://ftp.csx.cam.ac.uk/pub/software ...

  4. php输出utf-8格式

    header("Content-type:text/html;charset=utf-8"); 输出数据前插入以上代码,以utf-8格式输出,避免乱码

  5. Yii中使用PHPexcel获取excel中数据

    1.view中代码如下: <form name="frmBatchSettle" id="" action="" method=&qu ...

  6. PHPExcel导出导入excel、csv等格式数据

    <?php if(!defined('BASEPATH')) exit('No direct script access allowed'); //物资发料单明细 class Read_writ ...

  7. C++primer 阅读点滴记录(三)

    14章 操作符重载和转换 重载操作符是具有特殊名称的函数:保留字operator后接需要定义的操作符符号. 1.重载的操作符名: + – * / % ^ & | ~ ! , = <  & ...

  8. [转]SQLServer 2008以上误操作数据库恢复方法——日志尾部备份

    原文出处:http://blog.csdn.net/dba_huangzj/article/details/8491327 问题: 经常看到有人误删数据,或者误操作,特别是update和delete的 ...

  9. Android WIFI 启动流程(TIP^^)

    前几天因为解决一堆Bug,没时间写.我不会每天都写,就是为了存档一些资料. 内容来源:工作中接触到的+高手博客+文档(Books)=自己理解 仅限参考^^ 此博客是上一个<<Android ...

  10. ios 中怎么自定义(RGB)背景色

    1.定义RGB 色彩.随机颜色 我的抽为宏定义.便于各个文件中使用 // 1.获得RGB颜色 #define MTColor(r, g, b) [UIColor colorWithRed:(r)/25 ...