正题

题目链接: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. js函数聚合

    //掺杂类实现聚合(有的时候,我们需要某个或多个类里的一些方法函数) //将要被聚合的函数 var JSON = { toJsonString: function () { var output = ...

  2. prism 的学习网站

    C#的学习网址: https://www.cnblogs.com/zh7791

  3. CrackMe-Cycle

    转载自 OllyDBG入门教程   PS:自己逆在 最后的时候总会崩,不知道为什么. 我们输入用户名 CCDebuger,序列号 78787878,点上面那个"Check"按钮,呵 ...

  4. PE文件结构(32/64差异)

    1 基本概念 下表描述了贯穿于本文中的一些概念: 名称 描述 地址 是"虚拟地址"而不是"物理地址".为什么不是"物理地址"呢?因为数据在内 ...

  5. java Date操作的相关代码

    /** * 获取现在时间,这个好用 * * @return返回长时间格式 yyyy-MM-dd HH:mm:ss */ public static Date getSqlDate() { Date s ...

  6. Docker是简介

    Docker是什么  使用最广泛的开源容器引擎 一种操作系统级的虚拟化技术 依赖于Linux内核特性:Namespace(资源隔离)和Cgroups(资源限制) 一个简单的应用程序打包工具 D ...

  7. Java如何调用C语言程序,JNI技术

    Java为什么要调用C语言编写的程序因为涉及操作系统底层的事件,Java是处理不了的,例如用户上传一个视频文件,需要后台给视频加上水印,或者后台分离视频流和音频流,这个事Java就做不了,只能交给C语 ...

  8. docker《三》单机部署项目容器,nginx负载均衡

    接着<二> 创建一个网段(和二在一个网段) docker network create --subnet=172.19.0.0/24 pro-net docker run -d --nam ...

  9. (原创)[C#] DataTable排序扩展方法

    一,前言 DataTable的应用极其广泛,对DataTable进行排序也有很多方式,每种的实现方式都不难,但是使用起来却比较繁琐,所以本人便写了一个扩展方法,专门对DataTable进行操作. 本篇 ...

  10. WIN7下安装Python3.7和labelImg-1.7.0

    安装python3.7 官方https://www.python.org/downloads/windows/,下载windows 64bit python3.7版本 用Administrator权限 ...