Codeforces Round #254 DZY Loves Colors
题意:输入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的更多相关文章
- Codeforces 444 C - DZY Loves Colors
C - DZY Loves Colors 思路: 分块,复杂度有点玄学,和普通分块不同的是在这个块被一次染色的时候暴力染整个块. 代码: #pragma GCC optimize(2) #pragma ...
- Codeforces 444 C. DZY Loves Colors (线段树+剪枝)
题目链接:http://codeforces.com/contest/444/problem/C 给定一个长度为n的序列,初始时ai=i,vali=0(1≤i≤n).有两种操作: 将区间[L,R]的值 ...
- codeforces 444 C. DZY Loves Colors(线段树)
题目大意: 1 l r x操作 讲 [l,r]上的节点涂成x颜色,而且每一个节点的值都加上 |y-x| y为涂之前的颜色 2 l r 操作,求出[l,r]上的和. 思路分析: 假设一个区间为同样的颜 ...
- 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 ...
- Codeforces Round #254 (Div. 1) C. DZY Loves Colors 分块
C. DZY Loves Colors 题目连接: http://codeforces.com/contest/444/problem/C Description DZY loves colors, ...
- CodeForces 445E DZY Loves Colors
DZY Loves Colors Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces ...
- Codeforces 444C DZY Loves Colors(线段树)
题目大意:Codeforces 444C DZY Loves Colors 题目大意:两种操作,1是改动区间上l到r上面德值为x,2是询问l到r区间总的改动值. 解题思路:线段树模板题. #inclu ...
- Codeforces Round 254 (Div. 2)
layout: post title: Codeforces Round 254 (Div. 2) author: "luowentaoaa" catalog: true tags ...
- Codeforces444C DZY Loves Colors(线段树)
题目 Source http://codeforces.com/problemset/problem/444/C Description DZY loves colors, and he enjoys ...
随机推荐
- javascript运算符的优先级
最基木的运算符优先级就是所谓的“先乘除,后加减”.对于优先顺序处于同一层次上的运算符,按照从左到右出现的顺序计算.下面给出javascript定义的所有运算符的优先级.运算符 优先顺序成员选择.括号. ...
- C#语言各种集合介绍
集合,表示可以通过遍历每个元素来访问的一组对象(特别是可使用foreach循环访问)一个集合包括多个元素,即有一个集合类对象和N个元素对象 因为任何集合类都实现了IEnumerable接口,所以任何集 ...
- Python入门笔记(21):Python函数(4):关于函数式编程的内建函数
一.关于函数式编程的内建函数 apply()逐渐被舍弃,这里不讨论 1.filter() #filter(func,seq) """纯Python描述filter函数&q ...
- ajax回调函数Status问题
function readyDo() {// alert(xhr.readyState + "分" + xhr.Status); if ...
- javascript: Jquery each loop with json array or object
http://www.codeproject.com/Articles/779303/JSON-and-Microsoft-technologies http://www.codeproject.co ...
- Java中native的用法
原文来自:http://blog.csdn.net/funneies/article/details/8949660 native关键字说明其修饰的方法是一个原生态方法,方法对应的实现不是在当前文件, ...
- mysql ALL_O_DIRECT引发的unaligned AIO/DIO导致hang
公司内部有一套mysql环境,使用的是percona server分支(和其他几十套环境的版本.参数完全相同),就这套环境每隔两三天就会hang一次,关键hang的时候服务器cpu也就是百分之三四十, ...
- [转]MVC、MVP、MVVM
界面之下:还原真实的 MVC.MVP.MVVM 模式 [日期:2015-10-28] 来源:github.com/livoras 作者:戴嘉华 [字体:大 中 小] 前言 做客户端开发.前端开发 ...
- 如何实现两个Activity 之间如何通讯
<转> 今天主要学习了Activity 组件,在这里作一下总结 1,学习如何创建Activity 创建 Activity 要点: (1) 一个Activity就是一个类,并且这个类要继承A ...
- 【GOF23设计模式】建造者模式
来源:http://www.bjsxt.com/ 一.[GOF23设计模式]建造者模式详解类图关系 建造飞船 package com.test.Builder; public class AirShi ...