题目

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. Ubuntu下的MySQL安装

    <1>安装mysql-server sudo apt-get update sudo apt-get install mysql-server mysql-client <2> ...

  2. 6Hibernate进阶----青软S2SH(笔记)

    关于关联关系的配置,用注解配置如下(这里引用的jar包是javax.persistence) // @ManyToOne(fetch=FetchType.LAZY) @ManyToOne(fetch= ...

  3. [Head First设计模式]餐馆中的设计模式——命令模式

    系列文章 [Head First设计模式]山西面馆中的设计模式——装饰者模式 [Head First设计模式]山西面馆中的设计模式——观察者模式 [Head First设计模式]山西面馆中的设计模式— ...

  4. krpano

    调试: krpano的场景下方,有一个Console面板可以用来输出即时日志. 可以使用 showlog(true); 来设置打开此功能,默认是关闭的. 这样就可以把下面三种日志实时显示出来了: tr ...

  5. 大熊君JavaScript插件化开发------(实战篇之DXJ UI ------ ItemSelector重构完结版)

    一,开篇分析 Hi,大家好!大熊君又和大家见面了,还记得上一篇文章吗.主要讲述了以“jQuery的方式如何开发插件”,以及过程化设计与面向对象思想设计相结合的方式是 如何设计一个插件的,两种方式各有利 ...

  6. [Data Structure & Algorithm] 七大查找算法

    查找是在大量的信息中寻找一个特定的信息元素,在计算机应用中,查找是常用的基本运算,例如编译程序中符号表的查找.本文简单概括性的介绍了常见的七种查找算法,说是七种,其实二分查找.插值查找以及斐波那契查找 ...

  7. Github.com的Git和TortoiseGit图文教程

    图文介绍Windows系统下使用 Github账户 + msysgit + TortoiseGit 进行文件管理的方法. 安装 安装mysysgit 下载地址:msysgit 安装过程: 0.启动 1 ...

  8. JavaScript闭包之“词法作用域”

    大家应该写过下面类似的代码吧,其实这里我想要表达的是有时候一个方法定义的地方和使用的地方会相隔十万八千里,那方法执行时,它能访问哪些变量,不能访问哪些变量,这个怎么判断呢?这个就是我们这次需要分析的问 ...

  9. update语句关联表更新

    UPDATE dbo.NodeInstance SET OrderNumber=temp.OrderNo FROM dbo.NodeInstance ins,dbo.NodeTemplate temp ...

  10. HMac基本介绍

    基本介绍 HMAC(散列消息身份验证码: Hashed Message Authentication Code) 它不是散列函数,而是采用散列函数(MD5 or 或SHA)与共享密钥一起使用的消息身份 ...