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 secondsmemory 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 线段树的更多相关文章
- Codeforces Round #254 (Div. 1) C. DZY Loves Colors 分块
C. DZY Loves Colors 题目连接: http://codeforces.com/contest/444/problem/C Description DZY loves colors, ...
- 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内 ...
- Codeforces Round #254 (Div. 1) D - DZY Loves Strings
D - DZY Loves Strings 思路:感觉这种把询问按大小分成两类解决的问题都很不好想.. https://codeforces.com/blog/entry/12959 题解说得很清楚啦 ...
- 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 ...
- Codeforces Round #254 (Div. 1) A. DZY Loves Physics 智力题
A. DZY Loves Physics 题目连接: http://codeforces.com/contest/444/problem/A Description DZY loves Physics ...
- Codeforces Round #254 (Div. 2) A. DZY Loves Chessboard —— dfs
题目链接: http://codeforces.com/problemset/problem/445/A 题解: 这道题是在现场赛的最后一分钟通过的,相当惊险,而且做的过程也很曲折. 先是用递推,结果 ...
- 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 ...
- [题解]Codeforces Round #254 (Div. 2) B - DZY Loves Chemistry
链接:http://codeforces.com/contest/445/problem/B 描述:n种药品,m个反应关系,按照一定顺序放进试管中.如果当前放入的药品与试管中的药品要反应,危险系数变为 ...
- [题解]Codeforces Round #254 (Div. 2) A - DZY Loves Chessboard
链接:http://codeforces.com/contest/445/problem/A 描述:一个n*m的棋盘,有一些格子不能放棋子.现在把黑白棋子往上放,要求放满且相邻格子的棋子颜色不同.输出 ...
随机推荐
- c#中判断对象为空的几种方式(字符串等)
(1)先了解几个与空类型相关的关键字和对象 Null : 关键字表示不引用任何对象的空引用,它是所有引用类型变量的默认值,在2.0版本之前也就只有引用变量类型可以为null,如(string a=n ...
- linux iostat命令详解 磁盘操作监控工具
Linux系统中的 iostat是I/O statistics(输入/输出统计)的缩写,iostat工具将对系统的磁盘操作活动进行监视. 它的特点是汇报磁盘活动统计情况,同时也会汇报出CPU使用情况. ...
- php对mysql简单读取的实例
读取mysql数据库 例. <?php $link=mysql_connect("localhost","root","之前的管理员密码& ...
- Java排序
给出10个数,使用某种排序方法,按照从小到大的顺序输出个个数. 根据要求,首先得给出这10个数,这里的算法需要一个循环,数据结构需要一个长度为10的整型数组.首先用BufferedReader in= ...
- 通过bind实现activity与service的交互
先点bind按钮实现onCreate,在点击start给service传值(get方法) xml: <RelativeLayout xmlns:android="http://sche ...
- Moses 里的参数(未完成)
老师要求看看Moses里都有什么参数,调整了参数又会对翻译结果有什么影响,先将找到的参数列出来 首先是权重: [weight] WordPenalty0= LM= Distortion0= Phras ...
- WPF:将HTML RGB颜色值转化为Color对象的两种方式
(1)方式一: Color color1 = (Color)System.Windows.Media.ColorConverter.ConvertFromString("#E0E0E0&qu ...
- sqlserver中查找长时间未提交事务
无论是有意无意,如果事务在数据库中保持打开,则它会阻塞其他进程对修改后的数据进行操作.同样,对事务日志进行备份也只会截断不活动事务的那部分事务日志,所以打开的事务会导致日志变多(甚至达到物理限制),直 ...
- hdu 2680 Choose the best route
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Description One day , Kiki ...
- azure 云服务证书下载方式
打开地址自动下载证书,vs中项目-右键-发布-导入证书. https://manage.windowsazure.cn/publishsettings/index 在 Visual Studio 中打 ...