CF444C. DZY Loves Colors[线段树 区间]
2 seconds
256 megabytes
standard input
standard output
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?
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.
题意:区间修改颜色,colorfulness+=颜色差的绝对值,区间查询colorfulness
线段树区间更新和区间查询
col记录颜色,0说明颜色不同;sum是colorfulness;lazy记录增量(colorfulness的增量)
更新的时候遇到相同颜色的被包含的区间直接更新,否则下传标记更新孩子然后合并
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define m ((l+r)>>1)
#define lson o<<1,l,m
#define rson o<<1|1,m+1,r
#define lc o<<1
#define rc o<<1|1
using namespace std;
typedef long long ll;
const int N=5e5+,INF=2e9+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,q,op,ql,qr,x;
struct node{
ll sum,lazy,col;
}t[N<<];
void merge(int o){
t[o].col=t[lc].col!=t[rc].col?:t[lc].col;
t[o].sum=t[lc].sum+t[rc].sum;
}
void build(int o,int l,int r){
if(l==r) t[o].col=l;
else{
build(lson);
build(rson);
}
}
void pushDown(int o,int len){
if(t[o].col){
t[lc].col=t[rc].col=t[o].col;
t[lc].lazy+=t[o].lazy;
t[rc].lazy+=t[o].lazy;
t[lc].sum+=t[o].lazy*(len-(len>>));
t[rc].sum+=t[o].lazy*(len>>); t[o].col=t[o].lazy=;
}
}
void update(int o,int l,int r,int ql,int qr,ll v){//printf("update %d %d %d\n",o,l,r);
if(ql<=l&&r<=qr&&t[o].col){
t[o].sum+=(r-l+)*abs(v-t[o].col);
t[o].lazy+=abs(v-t[o].col);
t[o].col=v;
}else{
pushDown(o,r-l+);
if(ql<=m) update(lson,ql,qr,v);
if(m<qr) update(rson,ql,qr,v);
merge(o);
}
}
ll query(int o,int l,int r,int ql,int qr){
if(ql<=l&&r<=qr) return t[o].sum;
else{
pushDown(o,r-l+);
ll ans=;
if(ql<=m) ans+=query(lson,ql,qr);
if(m<qr) ans+=query(rson,ql,qr);
return ans;
}
}
int main(){
n=read();q=read();
build(,,n);
for(int i=;i<=q;i++){
op=read();ql=read();qr=read();
if(op==){x=read();update(,,n,ql,qr,x);}
else printf("%I64d \n",query(,,n,ql,qr));
}
}
CF444C. DZY Loves Colors[线段树 区间]的更多相关文章
- 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]上的和. 思路分析: 假设一个区间为同样的颜 ...
- CF444C DZY Loves Colors
考试完之后打的第一场CF,异常惨烈呀,又只做出了一题了.A题呆滞的看了很久,领悟到了出题者的暗示,应该就是两个点的时候最大吧,不然的话这题肯定特别难敲,YY一发交上去然后就过了.然后就在不停地YY B ...
- HDU5649 DZY Loves Sorting 线段树
题意:BC 76 div1 1004 有中文题面 然后奉上官方题解: 这是一道良心的基础数据结构题. 我们二分a[k]的值,假设当前是mid,然后把大于mid的数字标为1,不大于mid的数字标为0.然 ...
- ZOJ1610 Count the Colors —— 线段树 区间染色
题目链接:https://vjudge.net/problem/ZOJ-1610 Painting some colored segments on a line, some previously p ...
- ZOJ 1610 Count the Colors(线段树,区间覆盖,单点查询)
Count the Colors Time Limit: 2 Seconds Memory Limit: 65536 KB Painting some colored segments on ...
- zoj 1610 Count the Colors 线段树区间更新/暴力
Count the Colors Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/show ...
随机推荐
- [示例] Firemonkey 不规则按钮实做
利用 Firemonkey 控件的组合及可塑性,可以做出千变万化的效果及功能,下面展示一个不规则按钮的实做: 效果图: 实做方法: 开一个新工程 Multi-Device Application 放一 ...
- js promise chain
新的标准里增加了原生的Promise. 这里只讨论链式使用的情况,思考一下其中的细节部分. 一,关于 then() 和 catch() 的复习 then() 和 catch() 的参数里可以放置 ca ...
- php实现设计模式之 单例模式
<?php /*单例模式:作为对象的创建模式,单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统全局地提供这个实例.(创建型模式) * * */ class singleton{ pr ...
- AutoMapper之ABP项目中的使用介绍
最近在研究ABP项目,昨天写了Castle Windsor常用介绍以及其在ABP项目的应用介绍 欢迎各位拍砖,有关ABP的介绍请看阳光铭睿 博客 AutoMapper只要用来数据转换,在园里已经有很多 ...
- 2、ASP.NET MVC入门到精通——Entity Framework入门
实体框架(Entity Framework)简介 简称EF 与ADO.NET关系 ADO.NET Entity Framework 是微软以 ADO.NET 为基础所发展出来的对象关系对应 (O/R ...
- 理解 OpenStack 高可用(HA)(5):RabbitMQ HA
本系列会分析OpenStack 的高可用性(HA)概念和解决方案: (1)OpenStack 高可用方案概述 (2)Neutron L3 Agent HA - VRRP (虚拟路由冗余协议) (3)N ...
- iOS-私有API与runtime
转载:http://www.jianshu.com/p/6167b9ce7af8 序 有朋友在做类似iTool的功能,跟我聊起来,这几天闲,就写了一个demo,不是正经做这个,还很粗略,具体干货诸位等 ...
- 阻止默认事件event.preventDefault();
阻止浏览器默认事件.什么是默认事件,例如浏览器默认右键菜单.a标签默认连接跳转...,如何阻止呢? Firefox中,event必须作为参数传入. IE中,event是window对象的属性. ev ...
- sharepoint2013爬xls文件:Error initializing IFilter for extension的解决方案
最近sharepoint2013爬网出现: error initializing IFilter for extension '.xls' (Error code is 0x80030002). Th ...
- 自定义加载loading view动画组件的使用。
在github上找的一个有点酷炫的loading动画https://github.com/Fichardu/CircleProgress 我写写使用步骤 自定义view(CircleProgress ...