正题

题目链接: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. 【springboot】全局异常处理

    转自: https://blog.csdn.net/cp026la/article/details/86495196 前言: 开发中异常的处理必不可少,常用的就是 throw 和 try catch, ...

  2. C# 检测某版本VC++是否安装

    一.根据微软的产品号GUID,调用msi.dll class Program { static void Main(string[] args) { if (GetProcuct("{GUI ...

  3. 流媒体 Ubuntu部署srs、windows部署nginx

    一.获取项目//码云克隆git clone https://gitee.com/winlinvip/srs.oschina.git srs//githubgit clone https://githu ...

  4. 信号量-Semaphore、SemaphoreSlim

    核心类:Semaphore,通过int数值来控制线程个数. * 通过观察构造函数 public Semaphore(int initialCount, int maximumCount);: * in ...

  5. 服务端负载监控-参考srs服务器源码

    #include <map> #include <stdio.h> using namespace std; struct SrsMemoryObject { void* pt ...

  6. centos7 添加磁盘到/(根目录下),扩展VG卷和lv

    转载自:http://blog.itpub.net/25469263/viewspace-2662215/ centos7中使用vg方式扩充root分区 1.创建pv fdisk /dev/sdb 输 ...

  7. servlet通过响应头Content-Disposition实现文件下载效果

    package day08; import java.io.File; import java.io.FileInputStream; import java.io.IOException; impo ...

  8. JDBC中级篇(MYSQL)——模拟从数据库中上传下载附件

    注意:其中的JdbcUtil是我自定义的连接工具类:代码例子链接: package b_blob_clob; import java.io.BufferedOutputStream; import j ...

  9. Linux下MySQL主从复制(GTID)+读写分离(ProxySQL)-实施笔记

    GTID概念: GTID( Global Transaction Identifier)全局事务标识.GTID 是 5.6 版本引入的一个有关于主从复制的重大改进,相对于之前版本基于 Binlog 文 ...

  10. 如何攻击Java Web应用

    越来越多的企业采用Java语言构建企业Web应用程序,基于Java主流的框架和技术及可能存在的风险,成为被关注的重点. 本文从黑盒渗透的角度,总结下Java Web应用所知道的一些可能被利用的入侵点. ...