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的棋盘,有一些格子不能放棋子.现在把黑白棋子往上放,要求放满且相邻格子的棋子颜色不同.输出 ...
随机推荐
- SqlServer将表中数据复制到另一张表
insert into phone2(ph,attr,type,carrier) select top 1000 ph,attr,type,carrier from phone 将表phone的字段和 ...
- ADO.NET中ExcuteReader读取存储过程获取的多行数据
DLL层调用: List<BookInfo> tupleList = new List<BookInfo>(); using (IDataReader reader = thi ...
- c#学习之Socket网络编程
我是新手以前没写过博客 希望大家勿喷, 在编写Socket的时候需要导入System.Net.Socket 命名空间.利用该类我们可以直接编写Socket的客户端和服务的的程序了, 这里我们只讲tpc ...
- 实现QQ机器人报警
如题,废话不说,直接上代码.首先是登录QQ的小脚本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ...
- Ubuntu10.10的网络配置
有一阵子着实对Ubuntu的网络配置很迷惑,耐下心来仔细上网找了找,有点小心得,总结一下. 先说下大概的配置过程,再去细究一些情况. 一.配置大概分三类:通过配置文件配置.通过命令配置.通过图形化的网 ...
- sql server备份还原数据时的问题记录
1.关于“因为数据库正在使用,所以无法获得对数据库的独占访问权”的最终解决方案 关键SQL语句: ALTER DATABASE [datebase] SET OFFLINE WITH ROLLBACK ...
- CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙。
firewall:systemctl start firewalld.service#启动firewallsystemctl stop firewalld.service#停止firewallsyst ...
- [terry笔记]Oracle数据泵-schema导入导出
数据泵是10g推出的功能,个人倒数据比较喜欢用数据泵. 其导入的时候利用remap参数很方便转换表空间以及schema,并且可以忽略服务端与客户端字符集问题(exp/imp需要排查字符集). 数据泵也 ...
- java 中 java.lang.ArrayIndexOutOfBoundsException: 0 异常
package test; public class Test { public static void main(String[] args) { final int num2 = Integer. ...
- Python核心编程--学习笔记--5--数字
本章的主题是Python中的数字,这里详细介绍每一种数字类型,它们适用的各种运算符,以及用于处理数字的内建函数.在本章的末尾简单介绍了几个标准库中用于处理数字的模块. 1 数字类型 数字:标量贮存,可 ...