题意:输入n, m ; 有n给位置, 初始时第i个位置的color为i, colorfulness为0。

     有m次操作,一种是把成段的区域color更新为x, 对于更新的区域,每个位置(令第i个位置未更新前颜色为color[i])的colorfulness增加|color[i] -x|;

     另一种操作是询问一段区间[L,R]输出该区间的colorfulness总和。

 

代码实现

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long ll;
const int N = 111111;
int n, m, color[N<<2];
ll delta[N<<2], sum[N<<2]; void build(int l, int r, int rt)
{
color[rt] = delta[rt] = sum[rt] = 0;
if(l==r){
color[rt] = l;//第i个位置颜色为i
return ;
}
int m = (l+r)>>1;
build(lson);
build(rson);
} void Up(int l, int r, int rt)
{
sum[rt] = sum[rt<<1] + sum[rt<<1|1] + 1LL*delta[rt]*(r-l+1);
} void Down(int rt)
{
if(color[rt]>0)
{
color[rt<<1] = color[rt<<1|1] = color[rt];
color[rt] = 0;
}
} void Check(int x, int l, int r,int rt)
{
if(color[rt]>0)
{
delta[rt] += abs(x - color[rt]);
sum[rt] += 1LL *abs(x - color[rt]) * (r-l+1);
}
else
{
int m = (l+r)>>1;
Check(x,lson); Check(x,rson);
Up(l, r, rt);
}
color[rt] = x;
} void update(int L, int R, int x, int l, int r, int rt)
{
if(L<=l&&R>=r){
Check(x, l, r, rt);
return ;
}
Down(rt);
int m = (l+r)>>1;
if(L<=m) update(L, R, x, lson);
if(R>m) update(L, R, x, rson);
Up(l, r, rt);
} ll query(int L, int R, int l, int r,int rt)
{
if(L<=l&&R>=r) return sum[rt] ;
int m = (l+r)>>1;
ll ans = 0;
if(R<=m)
ans += query(L, R, lson) + 1LL * delta[rt] *(R-L+1);
else if(L>m)
ans += query(L, R, rson) + 1LL * delta[rt] * (R-L+1);
else
ans += query(L, m , lson) + query(m+1, R, rson) + 1LL * delta[rt] * (R-L+1);
return ans;
} int main()
{
// freopen("in.txt", "r", stdin);
while(scanf("%d%d", &n, &m)>0)
{
build(1, n, 1);
int type, l, r, x;
while(m--)
{
scanf("%d%d%d", &type, &l, &r);
if(type == 1)
{
scanf("%d", &x);
update(l, r, x, 1, n, 1);
}
else
printf("%I64d\n",query(l, r, 1, n, 1));
}
}
return 0;
}

Codeforces Round #254 DZY Loves Colors的更多相关文章

  1. Codeforces 444 C - DZY Loves Colors

    C - DZY Loves Colors 思路: 分块,复杂度有点玄学,和普通分块不同的是在这个块被一次染色的时候暴力染整个块. 代码: #pragma GCC optimize(2) #pragma ...

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

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

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

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

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

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

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

  6. CodeForces 445E DZY Loves Colors

    DZY Loves Colors Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces ...

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

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

  8. Codeforces Round 254 (Div. 2)

    layout: post title: Codeforces Round 254 (Div. 2) author: "luowentaoaa" catalog: true tags ...

  9. Codeforces444C DZY Loves Colors(线段树)

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

随机推荐

  1. KMP算法 --- 深入理解next数组

    在KMP算法中有个数组,叫做前缀数组,也有的叫next数组. 每一个子串有一个固定的next数组,它记录着字符串匹配过程中失配情况下可以向前多跳几个字符. 当然它描述的也是子串的对称程度,程度越高,值 ...

  2. 重构第16天 封装条件(Encapsulate Conditional)

    理解:本文中的“封装条件”是指条件关系比较复杂时,代码的可读性会比较差,所以这时我们应当根据条件表达式是否需要参数将条件表达式提取成可读性更好的属性或者方法,如果条件表达式不需要参数则可以提取成属性, ...

  3. 如何理解css中的float

    最近一段时间一直在为一个即将上线的新站进行一些前端开发.自然,对CSS的使用是必不可少的了.我们在CSS 中很多时候会用到浮动来布局.常见的有 float:left 或者 float:right .简 ...

  4. SVG操作插件:SVG.JS 个人提取部分实用中文文档

    先贴出github地址:https://github.com/svgdotjs/svg.js(也就是原文档的说明和文件的下载地址) 创建SVG文档 var draw = SVG('drawing'). ...

  5. 【JS复习笔记】05 正则表达式

    好吧,正则表达式,我从来没记过.以前要用的时候都是网上Copy一下的. 这里还是扯一下吧,以后要是有要用到的正则表达式那么就收集到这个帖子里.(尽管我认为不会,因为我根本就不是一个专业的前端,我只是来 ...

  6. java判定字符串中仅有数字和- 正则表达式匹配 *** 最爱那水货

    1.当有其他字符出现时,返回的数组长度>1 String s = "3---6\\5656";        Pattern pattern = Pattern.compil ...

  7. 微软发布ASP.NET 5路线图

    这次随 Visual Studio 2015 发布的 ASP.NET 版本是 ASP.NET 4.6 与 ASP.NET 5 beta5.在 VS2015 发布的同时,微软也发布了 ASP.NET 5 ...

  8. Android 手机卫士12--进程管理

    1.本进程不能被选中,所以先将checkbox隐藏掉--手机卫士 不能自杀 if(getItem(position).packageName.equals(getPackageName())){ ho ...

  9. windows下redis启动报Creating Server TCP listening socket *:6379: bind: No such file or directory

    解决方法: 按顺序输入如下命令就可以连接成功 # redis-cli.exe# > shutdown# > exit# redis-server.exe redis.windows.con ...

  10. 同源策略 JSONP(实践篇)

    JSONP详解 json相信大家都用的多,jsonp我就一直没有机会用到,但也经常看到,只知道是“用来跨域的”,一直不知道具体是个什么东西.今天总算搞明白了.下面一步步来搞清楚jsonp是个什么玩意. ...