CodeForces 445E DZY Loves Colors
DZY Loves Colors
This problem will be judged on CodeForces. Original ID: 445E
64-bit integer IO format: %I64d Java class name: (Any)
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
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
Hint
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.
Source
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = ;
struct node{
int lt,rt,color;
LL sum,len,add;
}tree[maxn<<];
void pushup(int v){
tree[v].sum = tree[v<<].sum + tree[v<<|].sum;
if(tree[v<<].color == tree[v<<|].color)
tree[v].color = tree[v<<].color;
else tree[v].color = ;
}
void pushdown(int v){
if(tree[v].add) {
tree[v<<].add += tree[v].add;
tree[v<<|].add += tree[v].add;
tree[v<<].sum += tree[v].add*tree[v<<].len;
tree[v<<|].sum += tree[v].add*tree[v<<|].len;
tree[v].add = ;
}
if(tree[v].color){
tree[v<<].color = tree[v<<|].color = tree[v].color;
tree[v].color = ;
}
}
void build(int lt,int rt,int v){
tree[v].lt = lt;
tree[v].rt = rt;
tree[v].len = rt - lt + ;
tree[v].add = ;
tree[v].sum = ;
if(lt == rt){
tree[v].color = lt;
return;
}
int mid = (lt + rt)>>;
build(lt,mid,v<<);
build(mid+,rt,v<<|);
pushup(v);
}
void update(int lt,int rt,int color,int v){
if(lt <= tree[v].lt && rt >= tree[v].rt && tree[v].color){
tree[v].add += abs(tree[v].color - color);
tree[v].sum += abs(tree[v].color - color)*tree[v].len;
tree[v].color = color;
return;
}
pushdown(v);
if(lt <= tree[v<<].rt) update(lt,rt,color,v<<);
if(rt >= tree[v<<|].lt) update(lt,rt,color,v<<|);
pushup(v);
}
LL query(int lt,int rt,int v){
if(lt <= tree[v].lt && rt >= tree[v].rt) return tree[v].sum;
pushdown(v);
LL sum = ;
if(lt <= tree[v<<].rt) sum += query(lt,rt,v<<);
if(rt >= tree[v<<|].lt) sum += query(lt,rt,v<<|);
pushup(v);
return sum;
}
int main(){
int n,m,op,x,y,color;
scanf("%d%d",&n,&m);
build(,n,);
while(m--){
scanf("%d%d%d",&op,&x,&y);
if(op == ){
scanf("%d",&color);
update(x,y,color,);
}else printf("%I64d\n",query(x,y,));
}
return ;
}
CodeForces 445E 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 ...
- 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 444 C - DZY Loves Colors
C - DZY Loves Colors 思路: 分块,复杂度有点玄学,和普通分块不同的是在这个块被一次染色的时候暴力染整个块. 代码: #pragma GCC optimize(2) #pragma ...
- Codeforces444C DZY Loves Colors(线段树)
题目 Source http://codeforces.com/problemset/problem/444/C Description DZY loves colors, and he enjoys ...
- CF444C. DZY Loves Colors[线段树 区间]
C. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Cf 444C DZY Loves Colors(段树)
DZY loves colors, and he enjoys painting. On a colorful day, DZY gets a colorful ribbon, which consi ...
- Codeforces 444A DZY Loves Physics(图论)
题目链接:Codeforces 444A DZY Loves Physics 题目大意:给出一张图,图中的每一个节点,每条边都有一个权值.如今有从中挑出一张子图,要求子图联通,而且被选中的随意两点.假 ...
- CodeForces 445B DZY Loves Chemistry
DZY Loves Chemistry Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64 ...
随机推荐
- 使用ajax实现搜索功能
最近要做一个搜索功能,网上搜了一圈,终于做出来了,很简单的一个,这里分享我的方法,希望对大家有用,不足之处还请指教. 这里使用ajax提交数据,配合jquery将数据显示出来. 用jq的keyup ...
- BZOJ 1176/2683 Mokia (三维偏序CDQ+树状数组)
题目大意: 洛谷传送门 三维偏序裸题.. 每次操作都看成一个三元组$<x,y,t>$,表示$x,y$坐标和操作时间$t $ 询问操作拆成$4$个容斥 接下来就是$CDQ$了,外层按t排序, ...
- WIN10 java环境变量问题之 配置的JDK1.8版本却是1.7
问题前沿,在开发项目中,发布的项目出现了内存溢出问题,我挨个把代码看了一遍,并不能准确定位到那个地方能出现内存溢出问题,后来想到使用压力测试,较可能出现内存溢出的接口进行一番测试. 我就安装了一个ap ...
- Docker决战到底(三) Rancher2.x的安装与使用 - 简书
原文:Docker决战到底(三) Rancher2.x的安装与使用 - 简书 image.png 当越来越多的容器化应用被部署,一个可以管理编排这些容器的工具此时就显得尤为重要了.目前容器编排领域 ...
- git 简单理解
现在git这个版本控制大行其道,弄了半天大概理解了一下他的工作原理. 使用流程 1,安装git ,小乌龟,小乌龟汉化(在设置里面第一项,检查更新,下载中文包安装) 2,设置 小乌龟 ->git ...
- ZOJ 3288 Domination
D - Domination Time Limit:8000MS Memory Limit:131072KB 64bit IO Format:%lld & %llu Descr ...
- cmd 下命令
tasklist 查看当前进程 taskkill /? 查看taskkill 的帮助信息 详情 cmd /? 查看cmd详情 color /? 查看颜色详情 比如 color 2 md d:\ji ...
- linux中sed的使用方法具体解释(对行数据的加入、删除等)
sed使用语法 [root@fwq test]# sed --help 使用方法: sed [选项]... {脚本(假设没有其它脚本)} [输入文件]... -n, --quiet, --silent ...
- 关于iOS7中UIView效果失效问题的解决
最近想做一个跑马灯的效果.于是写出了例如以下的跑马灯效果的代码...可是调试发现,在iOS6下动画是能够运行的,可是在iOS7下动画并不运行,没有达到预期的效果. [_scrollLabel size ...
- (转载) 据说年薪30万的Android程序员必须知道的
据说年薪30万的Android程序员必须知道的帖子 标签: android 2015-03-12 16:52 28705人阅读 评论(14) 收藏 举报 Android中国开发精英 目前包括: And ...