题意:输入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. Python记录-Pip安装

    1.第一步 下载py文件:https://bootstrap.pypa.io/ez_setup.py #!/usr/bin/env python """ Setuptoo ...

  2. python 的import机制2

    http://blog.csdn.net/sirodeng/article/details/17095591   python 的import机制,以备忘: python中,每个py文件被称之为模块, ...

  3. Linux下centos系统安装redis和php-redis

    源地址:http://mirrors.ustc.edu.cn/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm  //此为centos 6版本 安装输入 ...

  4. NoSuchMethodException <init>()

    1. Question Description: SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/n ...

  5. dbcp 1.4 底层连接断开时内存泄露bug

    在dbcp 1.4中,如果底层的连接已经与数据库断开了,此时dbcp 1.4的实现并不释放内部连接,虽然早已提供了removeAbandoned和removeAbandonedTimeout参数,但是 ...

  6. Intellij idea开发Hadoop MapReduce程序

    1.首先下载一个Hadoop包,仅Hadoop即可. http://mirrors.hust.edu.cn/apache/hadoop/common/hadoop-2.6.0/hadoop-2.6.0 ...

  7. WebForm(ASP开发方式,IIS服务器、WebForm开发基础)

    一.B/S和C/S 1.C/S C/S 架构是一种典型的两层架构,其全程是Client/Server,即客户端服务器端架构,其客户端包含一个或多个在用户的电脑上运行的程序,而服务器端有两种,一种是数据 ...

  8. HTML5&CSS3经典动态表单-2

    上一个demo效果没出来!实际如下 代码没问题.不知道为啥,展示的demo里光标获取焦点的时候不会有如下效果

  9. EntityFramework学习

    本文档主要介绍.NET开发中两项新技术,.NET平台语言中的语言集成查询技术 - LINQ,与ADO.NET中新增的数据访问层设计技术ADO.NET Entity Framework.ADO.NET的 ...

  10. Vue自定义过滤器

    gitHub地址: https://github.com/lily1010/vue_learn/tree/master/lesson05 一 自定义过滤器(注册在Vue全局) 注意事项: (1)全局方 ...