Codeforces444C DZY Loves Colors(线段树)
题目
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(线段树)的更多相关文章
- CF444C. DZY Loves Colors[线段树 区间]
C. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 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 ...
- Codeforces 444C DZY Loves Colors(线段树)
题目大意:Codeforces 444C DZY Loves Colors 题目大意:两种操作,1是改动区间上l到r上面德值为x,2是询问l到r区间总的改动值. 解题思路:线段树模板题. #inclu ...
- Codeforces 444 C. DZY Loves Colors (线段树+剪枝)
题目链接:http://codeforces.com/contest/444/problem/C 给定一个长度为n的序列,初始时ai=i,vali=0(1≤i≤n).有两种操作: 将区间[L,R]的值 ...
- codeforces 444 C. DZY Loves Colors(线段树)
题目大意: 1 l r x操作 讲 [l,r]上的节点涂成x颜色,而且每一个节点的值都加上 |y-x| y为涂之前的颜色 2 l r 操作,求出[l,r]上的和. 思路分析: 假设一个区间为同样的颜 ...
- HDU5649 DZY Loves Sorting 线段树
题意:BC 76 div1 1004 有中文题面 然后奉上官方题解: 这是一道良心的基础数据结构题. 我们二分a[k]的值,假设当前是mid,然后把大于mid的数字标为1,不大于mid的数字标为0.然 ...
- Cf 444C DZY Loves Colors(段树)
DZY loves colors, and he enjoys painting. On a colorful day, DZY gets a colorful ribbon, which consi ...
- 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 445E DZY Loves Colors
DZY Loves Colors Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces ...
随机推荐
- jQuery动态增删改查表格信息,可左键/右键提示
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Java程序员岗位
Java程序员岗位面试题有哪些? 1.面向对象的特征有哪些方面(1)抽象:抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择 ...
- springmvc @PathVariable("b") double b 丢失精度问题
Spring MVC从3.0开始支持REST,而主要就是通过@PathVariable来处理请求参数和路径的映射.由于考虑到SEO的缘故,很多人喜欢把新闻的名称作为路径中的一部分去处理,这时候中文的名 ...
- Debugging D Program on Windows
http://x64dbg.com/ http://www.ollydbg.de/version2.html
- Mac Sublime Text complie python .py error /bin/bash: shell_session_update: command not found
1.get the rvm version rvm -v 2.make sure the version at least 1.26 above. 3.then go ahead rvm get he ...
- C# Process.Start()方法详解
System.Diagnostics.Process.Start(); 能做什么呢?它主要有以下几个功能: 1.打开某个链接网址(弹窗). 2.定位打开某个文件目录. 3.打开系统特殊文件夹,如“控制 ...
- Bash 中的 _ 是不是环境变量
首先,我们想到的会是 export(等价于 declare -x)命令: $ export | grep 'declare -x _=' 没有找到,那么结论就是 _ 不是环境变量?当然没那么简单,否则 ...
- 大熊君JavaScript插件化开发------(实战篇之DXJ UI ------ ItemSelector)
一,开篇分析 Hi,大家好!大熊君又和大家见面了,还记得前两篇文章吗.主要讲述了以“jQuery的方式如何开发插件”,以及过程化设计与面向对象思想设计相结合的方式是 如何设计一个插件的,两种方式各有利 ...
- 大熊君JavaScript插件化开发------(第一季)
一,开篇分析 Hi,大家!大熊君又来了,今天这系列文章主要是说说如何开发基于“JavaScript”的插件式开发,我想很多人对”插件“这个词并不陌生, 有的人可能叫“组件”或“部件”,这不重要,关键是 ...
- jaxb
一.简介 JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术.该过程中,JAXB也提供了将XML实 ...