Cf 444C DZY Loves Colors(段树)
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 thei-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.
3 3
1 1 2 4
1 2 3 5
2 1 3
8
3 4
1 1 3 4
2 1 1
2 2 2
2 3 3
3
2
1
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
129
In the first sample, the color of each unit is initially [1, 2, 3], and the colorfulness is [0, 0, 0].
After the first operation, colors become [4, 4, 3], colorfulness become [3, 2, 0].
After the second operation, colors become [4, 5, 5], colorfulness become [3, 3, 2].
So the answer to the only operation of type 2 is 8.
线段树的操作,因为涉及到·类似染色的问题。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
typedef long long LL;
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
const int maxn=1e5+100;
LL col[maxn<<2],sum[maxn<<2],d[maxn<<2];//col[i]!=0代表区间颜色都为col[i];d[i]用于lazy操作
int n,m;
void pushup(int rs)
{
if(col[rs<<1]==col[rs<<1|1]) col[rs]=col[rs<<1];
else col[rs]=0;
sum[rs]=sum[rs<<1]+sum[rs<<1|1];
}
void pushdown(int rs,int m)
{
if(col[rs])
{
col[rs<<1]=col[rs<<1|1]=col[rs];
d[rs<<1]+=d[rs];d[rs<<1|1]+=d[rs];
sum[rs<<1]+=(LL)(m-(m>>1))*d[rs];
sum[rs<<1|1]+=(LL)(m>>1)*d[rs];
d[rs]=col[rs]=0;
}
}
void build(int rs,int l,int r)
{
if(l==r)
{
sum[rs]=0;
col[rs]=l;
return ;
}
col[rs]=d[rs]=0;
int mid=(l+r)>>1;
build(rs<<1,l,mid);
build(rs<<1|1,mid+1,r);
pushup(rs);
}
void update(int rs,int x,int y,int l,int r,int c)
{
if(l>=x&&r<=y&&col[rs])
{
sum[rs]+=abs(col[rs]-c)*(LL)(r-l+1);
d[rs]+=abs(col[rs]-c);
col[rs]=c;
return ;
}
pushdown(rs,r-l+1);
int mid=(l+r)>>1;
if(x<=mid) update(rs<<1,x,y,l,mid,c);
if(y>mid) update(rs<<1|1,x,y,mid+1,r,c);
pushup(rs);
}
LL query(int rs,int x,int y,int l,int r)
{
// cout<<"2333 "<<<<endl;
if(l>=x&&y>=r)
return sum[rs];
// if(l==r) return sum[rs];
int mid=(l+r)>>1;
pushdown(rs,r-l+1);
LL res=0;
if(x<=mid) res+=query(rs<<1,x,y,l,mid);
if(y>mid) res+=query(rs<<1|1,x,y,mid+1,r);
return res;
}
int main()
{
// std::ios::sync_with_stdio(false);
int l,r,x,op;
while(~scanf("%d%d",&n,&m))
{
build(1,1,n);
while(m--)
{
scanf("%d",&op);
if(op==1)
{
scanf("%d%d%d",&l,&r,&x);
update(1,l,r,1,n,x);
}
else
{
scanf("%d%d",&l,&r);
printf("%I64d\n",query(1,l,r,1,n));
}
}
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
Cf 444C DZY Loves Colors(段树)的更多相关文章
- Codeforces 444C DZY Loves Colors(线段树)
题目大意:Codeforces 444C DZY Loves Colors 题目大意:两种操作,1是改动区间上l到r上面德值为x,2是询问l到r区间总的改动值. 解题思路:线段树模板题. #inclu ...
- 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 ...
- CF444C. DZY Loves Colors[线段树 区间]
C. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- HDU 5649.DZY Loves Sorting-线段树+二分-当前第k个位置的数
DZY Loves Sorting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Oth ...
- CF 444C DZY Loves Physics(图论结论题)
题目链接: 传送门 DZY Loves Chemistry time limit per test1 second memory limit per test256 megabytes Des ...
- 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]上的和. 思路分析: 假设一个区间为同样的颜 ...
- Codeforces444C DZY Loves Colors(线段树)
题目 Source http://codeforces.com/problemset/problem/444/C Description DZY loves colors, and he enjoys ...
- Codeforces Round #254 (Div. 1) C. DZY Loves Colors 分块
C. DZY Loves Colors 题目连接: http://codeforces.com/contest/444/problem/C Description DZY loves colors, ...
随机推荐
- JTree
http://www.easyicon.net/ package swing.tree; import java.awt.BorderLayout; import java.awt.Component ...
- SPOJ SUBLEX 7258. Lexicographical Substring Search
看起来像是普通的SAM+dfs...但SPOJ太慢了......倒腾了一个晚上不是WA 就是RE ..... 最后换SA写了...... Lexicographical Substring Searc ...
- 【嵌入式开发】写入开发板Linux系统-模型S3C6410
笔者 : 万境绝尘 转载请著名出处 最终拿到板子了, 嵌入式开发正式开启. 板子型号 : 三星 S3C6410 基于ARM11, 指令集基于arm6指令集; 为毛不是 Cortext A9的板子; 烧 ...
- 终端查询数据库sqlite(创建你自己,或者是coredata创建)那里的东西
首先需要知道数据库的路径,coredata一般都是在沙箱中创建Documents目录的.虽然他建立了自己看, sqlite3 #数据库路径# //进入数据库 .tables //查看数 ...
- 使用cocoapods install友盟时报错Error installing UMengAnalytics
报错: [!] /usr/bin/unzip /Users/soindy/Documents/SmartThermo/ios/SmartThermo/Pods/UMengAnalytics/file. ...
- 它们的定义TextView使之具有跑马灯的效果
一.引入问题 使用通用textview快乐效应,焦点事件不启动滚动,button目前的焦点事件,但丑,因此,需要定制TextView 天生焦点 个textview FocusedTextView.ja ...
- 如何实现Web聊天
假设你web聊天不知道这件事情,那么最好的方法可能是:openfire+jsjac openfire它是java做开源xmppserver,jsjac它是javascript做开源的Web版本xmpp ...
- 移动web点5像素的秘密(转)
最近和一个朋友聊天,朋友吐露了工作上的一些不开心,说自己总是喜欢跟别人比较,活得比较累,这种感觉大部分人经历过,往往觉得是自己心态不好,其实不然,这是人性,此时应该快速摆脱这种状态,想到DOTA大9神 ...
- DWR入门的例子(一个)
DWR(Direct Web Remoting)是WEB远程调用框架.使用这种框架使AJAX发展至今已成为非常easy.使用DWR能client利用JavaScript直接调用服务端的Java方法并返 ...
- oracle HA 高可用性具体解释(之中的一个)
oracle HA 高可用性具体解释(之二,深入解析TAF,以及HA框架) :http://blog.csdn.net/panfelix/article/details/38436197 一.HA F ...