正题

题目链接: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. C++ 矩形交集和并集的面积-离散化

    //离散化,x,y坐标分别按从小到大排序 //离散化 //1.首先分离出所有的横坐标和纵坐标分别按升序存入数组X[ ]和Y[ ]中. //2. 设数组XY[ ][ ].对于每个矩形(x1,y1)(x2 ...

  2. 【转】互联网项目中mysql应该选什么事务隔离级别

    作者:孤独烟 转自:https://www.cnblogs.com/rjzheng/p/10510174.html 摘要 企业千万家,靠谱没几家.社招选错家,亲人两行泪. 祝大家金三银四跳槽顺利! 引 ...

  3. 【AE】多表的联合查询

    多表的联合查询 // Create the query definition. IQueryDef queryDef = featureWorkspace.CreateQueryDef(); // P ...

  4. 基于mysql的sakila数据库脚本分析

    本例是基于mysql的sakila数据库脚本的复杂查询分析,大家可以去mysql官网上下载此脚本:也可以进入我的资源页进行下载: 关系图如下: 下面是查询的案例: 1.查询某部电影的所属类别,语言 S ...

  5. TDSQL MySQL版基本原理-水平分表 读写分离 弹性扩展 强同步

    TDSQL MySQL版(TDSQL for MySQL)是部署在腾讯云上的一种支持自动水平拆分.Shared Nothing 架构的分布式数据库.TDSQL MySQL版 即业务获取的是完整的逻辑库 ...

  6. python 动图gif合成与分解

    合成 #!/usr/bin/env python3 # -*- coding: utf-8 -*- import os import sys import imageio def main(imgs_ ...

  7. PyPDF2.py 合并pdf时报错问题

    报错如下: Traceback (most recent call last): File "./pdf_merge.py", line 68, in <module> ...

  8. SpringMVC-源码-图解

  9. Python - 执行cmd命令

    python操作cmd 我们通常可以使用os模块的命令进行执行cmd 方法一:os.system os.system(执行的命令) # 源码 def system(*args, **kwargs): ...

  10. autoCAD绘制简单三维立体图形

    第一步: 首先绘制一个简单的(封闭的)二维图形: 第二步: 变换观察视角,比如修改为 变换后的视角: 第三步: 选中闭合图形边框,使用组合键"ctrl + shift + E",然 ...