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 ...
随机推荐
- Unity3D游戏在iOS上因为trampolines闪退的原因与解决办法
http://7dot9.com/?p=444 http://whydoidoit.com/2012/08/20/unity-serializer-mono-and-trampolines/ 确定具体 ...
- mod-mono
http://go-mono.com/config-mod-mono/ 配置文件生成器 Mono remote debugging from Visual Studio http://stackov ...
- API接口:分页
// 查询满足要求的总记录数 $count = M("back")->where($back_map)->count(); $pagecount = ceil($cou ...
- 深入理解javascript原型和闭包(12)——简介【作用域】
提到作用域,有一句话大家(有js开发经验者)可能比较熟悉:“javascript没有块级作用域”.所谓“块”,就是大括号“{}”中间的语句.例如if语句: 再比如for语句: 所以,我们在编写代码的时 ...
- C语言基础(7)-float,double,long double类型
1.定义方式 3.14这个就是一个浮点常量,3f是一个浮点类型的常量 float a;//定义了一个浮点类型的小数变量,名字叫a double b;//定义了一个double类型的变量,名字叫b lo ...
- [NHibernate]代码生成器的使用
目录 写在前面 文档与系列文章 代码生成器的使用 总结 写在前面 前面的文章介绍了nhibernate的相关知识,都是自己手敲的代码,有时候显得特别的麻烦,比如你必须编写持久化类,映射文件等等,举得例 ...
- Android-修改TabWidget字体大小颜色及对齐
在Android中,我们可以定义TabWidget来分页.在上一篇文章中有说到使用TabWidget定义Tab分页布局,但大部分用户可能会觉得默认的字体有点小,但Tab选项卡默认又不能设定字体大小,如 ...
- LYDSY热身赛 escape
Description 给出数字N(1<=N<=10000),X(1<=x<=1000),Y(1<=Y<=1000),代表有N个敌人分布一个X行Y列的矩阵上矩形的行 ...
- 今天在学习NTP时发现了2个网站
NTP 调整系统时间 一个网站是:http://chrony.tuxfamily.org/doc/1.31/manual.html 这个是专门介绍chrony的,做的很详细. 另外一个是:http: ...
- svn 版本转为git
git clone 相当于git init 和 git svn fetch.git svn rease git svn fetch 从svn服务器取指定区间的版本转化成git库 git svn reb ...