正题

题目链接:https://www.luogu.com.cn/problem/CF444C


题目大意

\(n\)个物品第\(i\)个颜色为\(i\),权值为\(0\)。要求支持\(m\)次操作

  1. 给出\(l,r,x\),对于所有区间\([l,r]\)中的物品,如果颜色为\(c\),那么该位置的权值加上\(|c-x|\),并且颜色改为\(x\)
  2. 询问区间权值和

解题思路

区间染色有一种简单的做法并且可以求出每个被染色的相同颜色段。

用\(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】的更多相关文章

  1. CF444C. DZY Loves Colors[线段树 区间]

    C. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  2. 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 ...

  3. Codeforces 444C DZY Loves Colors(线段树)

    题目大意:Codeforces 444C DZY Loves Colors 题目大意:两种操作,1是改动区间上l到r上面德值为x,2是询问l到r区间总的改动值. 解题思路:线段树模板题. #inclu ...

  4. Codeforces 444 C. DZY Loves Colors (线段树+剪枝)

    题目链接:http://codeforces.com/contest/444/problem/C 给定一个长度为n的序列,初始时ai=i,vali=0(1≤i≤n).有两种操作: 将区间[L,R]的值 ...

  5. codeforces 444 C. DZY Loves Colors(线段树)

    题目大意: 1 l r x操作 讲 [l,r]上的节点涂成x颜色,而且每一个节点的值都加上 |y-x| y为涂之前的颜色 2 l r  操作,求出[l,r]上的和. 思路分析: 假设一个区间为同样的颜 ...

  6. CF444C DZY Loves Colors

    考试完之后打的第一场CF,异常惨烈呀,又只做出了一题了.A题呆滞的看了很久,领悟到了出题者的暗示,应该就是两个点的时候最大吧,不然的话这题肯定特别难敲,YY一发交上去然后就过了.然后就在不停地YY B ...

  7. HDU5649 DZY Loves Sorting 线段树

    题意:BC 76 div1 1004 有中文题面 然后奉上官方题解: 这是一道良心的基础数据结构题. 我们二分a[k]的值,假设当前是mid,然后把大于mid的数字标为1,不大于mid的数字标为0.然 ...

  8. Codeforces444C DZY Loves Colors(线段树)

    题目 Source http://codeforces.com/problemset/problem/444/C Description DZY loves colors, and he enjoys ...

  9. Cf 444C DZY Loves Colors(段树)

    DZY loves colors, and he enjoys painting. On a colorful day, DZY gets a colorful ribbon, which consi ...

  10. Codeforces Round #254 (Div. 1) C. DZY Loves Colors 分块

    C. DZY Loves Colors 题目连接: http://codeforces.com/contest/444/problem/C Description DZY loves colors, ...

随机推荐

  1. spring开发中常见错误集合,逐步添加

    1.关于jstl错误:原因,在jsp页面中使用了jstl标签库,但是却没有导入,可以将相应的jar包放在tomcat的lib目录下,一劳永逸 Java.lang.NoClassDefFoundErro ...

  2. JAVA集合类(代码手写实现,全面梳理)

    参考网址:https://blog.csdn.net/weixin_41231928/article/details/103413167 目录 一.集合类关系图 二.Iterator 三.ListIt ...

  3. linux中的分号&&和&,|和||说明与用法

    在用linux命令时候,我们可以一行执行多条命令或者有条件的执行下一条命令,下面我们讲解一下linux命令分号&&和&,|和||的用法 在用linux命令时候,我们可以一行执行 ...

  4. springboot&&springcloud知识点

    spring cloud 常见面试题: A.https://blog.csdn.net/panhaigang123/article/details/79587612 B.https://blog.cs ...

  5. BUUCTF-[CISCN2019 总决赛 Day2 Web1]Easyweb

    BUUCTF-[CISCN2019 总决赛 Day2 Web1]Easyweb 就给了一个这个... 先打上robots.txt看看 发现有源码备份,但不是index.php.bak... 看源码发现 ...

  6. Vue 组件通信方案

    父组件--> 子组件 1. 属性设置 父组件关键代码如下: <template> <Child :child-msg="msg"></Child ...

  7. MySQL——MySQL安装

    1.rpm yum安装:安装方便.速度快.无法定制 2.二进制安装:解压即可使用,不能定制功能 3.编译安装: 可定制.安装慢: MySQL5.5之前:./configure make make in ...

  8. Python - 面向对象编程 - 小实战(1)

    题目 设计一个类Person,生成若干实例,在终端输出如下信息 小明,10岁,男,上山去砍柴 小明,10岁,男,开车去东北 小明,10岁,男,最爱大保健 老李,90岁,男,上山去砍柴 老李,90岁,男 ...

  9. Appium自动化(5) - 如何获取android app 的Activity 和 Package

    如果你还想从头学起Appium,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1693896.html 前言 在Desired Capab ...

  10. java 多线程Thread和Runnable的区别

    如果一个类继承Thread,则不适合资源共享.但是如果实现了Runable接口的话,则很容易的实现资源共享 实现Runnable接口比继承Thread类所具有的优势:1. 适合多个相同的程序代码的线程 ...