题意:输入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. 自己动手搞定支付宝手机网站支付接口 FOR ECShop

    支付宝WAP网站版本的支付接口网上整合的比较少,看到很多网站在卖,顿觉无语. 主要是得自己查看支付宝官方提供的SDK中的开发文档. 支付宝sdk下载地址:https://doc.open.alipay ...

  2. FL2440驱动添加(4)LED 驱动添加

    硬件信息:FL2440板子,s3c2440CPU带四个LED,分别在链接GPB5,GPB6,GPB8,GPB10 内核版本:linux-3.8.0 led驱动代码如下: 值得注意地方地方: 1,定时器 ...

  3. Fundamentals of speech signal processing

    PDF版资料下载:链接:http://pan.baidu.com/s/1hrKntkw 密码:f2y9

  4. javascript中apply()和call()方法的区别

    一.方法的定义 call方法: 语法:call(thisObj,Object)定义:调用一个对象的一个方法,以另一个对象替换当前对象.说明:call 方法可以用来代替另一个对象调用一个方法.call ...

  5. 通过重写OnScrollListener来监听RecyclerView是否滑动到底部

    为了增加复用性和灵活性,我们还是定义一个接口来做监听滚动到底部的回调,这样你就可以把它用在listview,scrollView中去. OnBottomListener package kale.co ...

  6. MySQL全文索引应用简明教程

    本文从以下几个方面介绍下MySQL全文索引的基础知识: MySQL全文索引的几个注意事项 全文索引的语法 几种搜索类型的简介 几种搜索类型的实例 全文索引的几个注意事项 搜索必须在类型为fulltex ...

  7. C中的一些经常会用到的函数

    1.sscanf 函数原型: int sscanf(const char *,const char *,...); int sscanf(const char *buffer,const char * ...

  8. 基础学习day12--多线程一线程之间的通信和常用方法

    一.线程之间的通信 1.1.线程之间的通信方法 多个线程在处理统一资源,但是任务却不同,这时候就需要线程间通信.    等待/唤醒机制涉及的方法:    1. wait():让线程处于冻结状态,被wa ...

  9. 解决tableViewCell分割线不到左边界的问题

    在tableView控制器的.m文件中任何位置加入以下两个方法即可解决 /** * 下面两个方法解决cell分割线不到左边界的问题 */ -(void)viewDidLayoutSubviews { ...

  10. 提高Objective-C代码质量心机一:简化写法

    提高OC代码质量的小心机 一.OC特性 OC 为 C 语言添加了面向对象特性,是其超集; OC 使用动态绑定的消息结构,也就是,在运行时才会检查对象类型; 接收一条消息后,究竟应执行何种代码,由运行期 ...