CF444C-DZY Loves Colors【线段树,set】
正题
题目链接:https://www.luogu.com.cn/problem/CF444C
题目大意
\(n\)个物品第\(i\)个颜色为\(i\),权值为\(0\)。要求支持\(m\)次操作
- 给出\(l,r,x\),对于所有区间\([l,r]\)中的物品,如果颜色为\(c\),那么该位置的权值加上\(|c-x|\),并且颜色改为\(x\)
- 询问区间权值和
解题思路
区间染色有一种简单的做法并且可以求出每个被染色的相同颜色段。
用\(set\)维护每个相同的连续颜色段,那么每次修改最多会产生\(3\)个新的颜色端。均摊下来就是\(O(n\log n)\)了。
然后加一个线段树维护权值就好了,总时间复杂度也是\(O(n\log n)\)的
code
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
#define ll long long
#define lowbit(x) (x&-x)
using namespace std;
const ll N=1e5+10;
struct node{
ll l,r,w;
node(ll L=0,ll rr=0,ll ww=0)
{l=L;r=rr;w=ww;return;}
};
multiset<node> s;
ll n,m;
bool operator<(node x,node y)
{return x.r<y.r;}
struct TreeBinary{
ll w[N<<2],lazy[N<<2];
void Downdata(ll x,ll l,ll r){
if(!lazy[x])return;
ll mid=(l+r)>>1;
w[x*2]+=lazy[x]*(mid-l+1);
w[x*2+1]+=lazy[x]*(r-mid);
lazy[x*2]+=lazy[x];lazy[x*2+1]+=lazy[x];
lazy[x]=0;return;
}
void Change(ll l,ll r,ll val,ll L=1,ll R=n,ll x=1){
if(L==l&&R==r){w[x]+=val*(r-l+1);lazy[x]+=val;return;}
ll mid=(L+R)>>1;Downdata(x,L,R);
if(r<=mid)Change(l,r,val,L,mid,x*2);
else if(l>mid)Change(l,r,val,mid+1,R,x*2+1);
else Change(l,mid,val,L,mid,x*2),Change(mid+1,r,val,mid+1,R,x*2+1);
w[x]=w[x*2]+w[x*2+1];return;
}
ll Ask(ll l,ll r,ll L=1,ll R=n,ll x=1){
if(L==l&&R==r)return w[x];
ll mid=(L+R)>>1;Downdata(x,L,R);
if(r<=mid)return Ask(l,r,L,mid,x*2);
if(l>mid)return Ask(l,r,mid+1,R,x*2+1);
return Ask(l,mid,L,mid,x*2)+Ask(mid+1,r,mid+1,R,x*2+1);
}
}T;
signed main()
{
multiset<node>::iterator it;
scanf("%lld%lld",&n,&m);
for(ll i=1;i<=n;i++)s.insert(node(i,i,i));
while(m--){
ll op,l,r,x;
scanf("%lld%lld%lld",&op,&l,&r);
if(op==1){
scanf("%lld",&x);
while(1){
it=s.lower_bound(node(l,l,l));
node tmp=*it;
s.erase(it);
if(tmp.l<l&&tmp.r>r)
T.Change(l,r,abs(x-tmp.w));
if(tmp.l<l){
s.insert(node(tmp.l,l-1,tmp.w));
if(tmp.r<=r)T.Change(l,tmp.r,abs(x-tmp.w));
}
if(tmp.r>r){
s.insert(node(r+1,tmp.r,tmp.w));
if(tmp.l>=l)T.Change(tmp.l,r,abs(x-tmp.w));
break;
}
if(tmp.l>=l&&tmp.r<=r)T.Change(tmp.l,tmp.r,abs(x-tmp.w));
if(tmp.r==r)break;
}
s.insert(node(l,r,x));
}
else printf("%lld\n",T.Ask(l,r));
}
return 0;
}
CF444C-DZY Loves Colors【线段树,set】的更多相关文章
- 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]上的和. 思路分析: 假设一个区间为同样的颜 ...
- CF444C DZY Loves Colors
考试完之后打的第一场CF,异常惨烈呀,又只做出了一题了.A题呆滞的看了很久,领悟到了出题者的暗示,应该就是两个点的时候最大吧,不然的话这题肯定特别难敲,YY一发交上去然后就过了.然后就在不停地YY B ...
- HDU5649 DZY Loves Sorting 线段树
题意:BC 76 div1 1004 有中文题面 然后奉上官方题解: 这是一道良心的基础数据结构题. 我们二分a[k]的值,假设当前是mid,然后把大于mid的数字标为1,不大于mid的数字标为0.然 ...
- Codeforces444C DZY Loves Colors(线段树)
题目 Source http://codeforces.com/problemset/problem/444/C Description DZY loves colors, and he enjoys ...
- 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, ...
随机推荐
- 基于typescript编写vue的ts文件语法模板
1 <template> 2 <div> 3 <input v-model="msg"> 4 <p>prop: {{ propMes ...
- C#中使用WavHelper保存录音数据为wav文件
C#将录音数据文件保存为wav格式文件,这里使用到的是WavHelper工具类. WavHelper工具类: using System; using System.Collections.Generi ...
- dataTemplate 之 ContentTemplate 的使用
<Window x:Class="WpfApplication1.Window38" xmlns="http://schemas.microsoft.com/win ...
- Spring第一课:配置文件及IOC引入(一)
Spring最核心的特点就是控制反转:(IOC)和面向切面(AOP) 首先作为一个Spring的项目需要导入的四个核心包,一个依赖: 四核心:core.context.beans.expression ...
- springcloud<seata配置文件解释及其说明>
出现如下错误时: Could not found property service.disableGlobalTransaction, try to use default value instead ...
- NVidia Jetson Ubuntu 18.04 安装ROS过程中运行sudo rosdep init指令出错
参考:https://www.cnblogs.com/xuhaoforwards/p/9399744.html 安装ROS过程中运行sudo rosdep init后,出现如下错误LOG: ERROR ...
- linux shell 删除满足正则表达式的文件
用find配合xargs rm find . -type f -name "to_delete_file_[a-z]_*_[0-9].jpg" | xargs rm
- QT学习日记篇-02-QT信号和槽
课程大纲: <1>给控件改名字 随着UI界面的控件变多,如果使用系统自带的名称,后期会让人不明觉厉,说白了,就是掌握C++的命名规则:易懂,条例清晰,人性化 方法:直接点击控件,进入右侧对 ...
- uniapp 设置背景图片
uniapp 由于其特殊机制,导致了背景图片不能引用本地图片.只能通过 转成 base64 来进行设置 附上链接:https://oktools.net/image2base64 图片转成base64 ...
- Java HashMap工作原理:不仅仅是HashMap
前言: 几乎所有java程序员都用过hashMap,但会用不一定会说. 近年来hashMap是非常常见的面试题,如何为自己的回答加分?需要从理解开始. "你用过hashMap吗?" ...