题目

Source

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

Description

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?

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

3 3
1 1 2 4
1 2 3 5
2 1 3

3 4
1 1 3 4
2 1 1
2 2 2
2 3 3

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

Sample Output

8

3
2
1

129

分析

有点不明觉厉。。
http://blog.csdn.net/kyleyoung_ymj/article/details/51768532

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 111111 int col[MAXN<<2],col_tag[MAXN<<2];
long long sum[MAXN<<2],tag[MAXN<<2];
int N,x,y,z;
void update(int i,int j,int k){
if(x<=i && j<=y){
if(col[k]){
tag[k]+=abs(col[k]-z);
sum[k]+=(j-i+1LL)*abs(col[k]-z);
col[k]=z;
col_tag[k]=z;
return;
}
}
int mid=i+j>>1;
if(tag[k]){
tag[k<<1]+=tag[k];
sum[k<<1]+=(mid-i+1LL)*tag[k];
tag[k<<1|1]+=tag[k];
sum[k<<1|1]+=(j-mid)*tag[k];
tag[k]=0;
}
if(col_tag[k]){
col[k<<1]=col[k<<1|1]=col_tag[k<<1]=col_tag[k<<1|1]=col_tag[k];
col_tag[k]=0;
}
if(x<=mid) update(i,mid,k<<1);
if(y>mid) update(mid+1,j,k<<1|1);
sum[k]=sum[k<<1]+sum[k<<1|1];
if(col[k<<1] && col[k<<1]==col[k<<1|1]) col[k]=col[k<<1];
else col[k]=0;
}
long long query(int i,int j,int k){
if(x<=i && j<=y){
return sum[k];
}
int mid=i+j>>1;
if(tag[k]){
tag[k<<1]+=tag[k];
sum[k<<1]+=(mid-i+1LL)*tag[k];
tag[k<<1|1]+=tag[k];
sum[k<<1|1]+=(j-mid)*tag[k];
tag[k]=0;
}
if(col_tag[k]){
col[k<<1]=col[k<<1|1]=col_tag[k<<1]=col_tag[k<<1|1]=col_tag[k];
col_tag[k]=0;
}
long long ret=0;
if(x<=mid) ret+=query(i,mid,k<<1);
if(y>mid) ret+=query(mid+1,j,k<<1|1);
return ret;
}
void init(int i,int j,int k){
if(i==j){
col[k]=x;
return;
}
int mid=i+j>>1;
if(x<=mid) init(i,mid,k<<1);
else init(mid+1,j,k<<1|1);
} int main(){
int n,m;
scanf("%d%d",&n,&m);
for(N=1; N<n; N<<=1);
for(x=1; x<=n; ++x) init(1,N,1);
int a;
while(m--){
scanf("%d",&a);
if(a==1){
scanf("%d%d%d",&x,&y,&z);
update(1,N,1);
}else{
scanf("%d%d",&x,&y);
printf("%lld\n",query(1,N,1));
}
}
return 0;
}

Codeforces444C DZY Loves Colors(线段树)的更多相关文章

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

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

  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 444C DZY Loves Colors(线段树)

    题目大意:Codeforces 444C DZY Loves Colors 题目大意:两种操作,1是改动区间上l到r上面德值为x,2是询问l到r区间总的改动值. 解题思路:线段树模板题. #inclu ...

  4. Codeforces 444 C. DZY Loves Colors (线段树+剪枝)

    题目链接:http://codeforces.com/contest/444/problem/C 给定一个长度为n的序列,初始时ai=i,vali=0(1≤i≤n).有两种操作: 将区间[L,R]的值 ...

  5. codeforces 444 C. DZY Loves Colors(线段树)

    题目大意: 1 l r x操作 讲 [l,r]上的节点涂成x颜色,而且每一个节点的值都加上 |y-x| y为涂之前的颜色 2 l r  操作,求出[l,r]上的和. 思路分析: 假设一个区间为同样的颜 ...

  6. HDU5649 DZY Loves Sorting 线段树

    题意:BC 76 div1 1004 有中文题面 然后奉上官方题解: 这是一道良心的基础数据结构题. 我们二分a[k]的值,假设当前是mid,然后把大于mid的数字标为1,不大于mid的数字标为0.然 ...

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

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

  9. CodeForces 445E DZY Loves Colors

    DZY Loves Colors Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces ...

随机推荐

  1. CMS系统的实现图

  2. Hibernate的session缓存和对象的四种状态

    一.session缓存 说session缓存就得说到JAVA对象的生命周期,当没有任何引用指向一个对象时,对象则可以被gc回收,也就是生命周期结束了 而hibernate获取一个对象后,会将对象存入s ...

  3. 【转】HTML5的小知识点小集合

    html5的小知识点小集合 html5知识   1.  Doctype作用?标准模式与兼容模式各有什么区别? (1).<!DOCTYPE>声明位于位于HTML文档中的第一行,处于<h ...

  4. js 常用函数收集(基础)

    (1).判断是否为数值 function isNum(obj){ return !isNaN(parseFloat(obj)) && isFinite(obj); } (2).判断是否 ...

  5. [NHibernate]持久化类(Persistent Classes)

    系列文章 [Nhibernate]体系结构 [NHibernate]ISessionFactory配置 引言 持久化类是应用程序用来解决商业问题的类(比如,在电子交易程序中的Customer和Orde ...

  6. win7下安装和使用Windows XP Mode

    如果想在电脑中安装多个操作系统有几种方法: 1.安装虚拟机,继而在虚拟机中安装操作系统.虚拟机个数就相当于电脑个数,常用的虚拟机软件有VMVare,VMVare功能齐全,但是安装文件较大. 2.如果你 ...

  7. R的卸载和更新安装

      R包经常会遇到各种版本不兼容的毛病,比如当前的版本相较于包,新了/旧了都是麻烦 而升级R软件呢,最麻烦的就是之前安装的包怎么办? 搜罗了以下几种方法:   方法1: (1)直接安装新版本 (2)然 ...

  8. Sql Server 日期格式化函数

    Sql Server 中一个非常强大的日期格式化函数Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AMSelect CONVE ...

  9. PHP数组合并+与array_merge的区别分析 & 对多个数组合并去重技巧

    PHP中两个数组合并可以使用+或者array_merge,但之间还是有区别的,而且这些区别如果了解不清楚项目中会要命的! 主要区别是两个或者多个数组中如果出现相同键名,键名分为字符串或者数字,需要注意 ...

  10. sql语句修改列

    1.修改列名的常用语句 exec sp_rename '[dbo].[Table_1].[birthday]','birth'  --将表中列名为birthday改为birdh 属性不变,用的是数据库 ...