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 ...
随机推荐
- 有关CLR的初学小整理(可能理解不深刻,望大牛指出)
1. .Net程序通过CLR去加载运行管理代码, 加载CLR的进程成为“宿主”,通常操作系统加载. 加载CLR的进程也可以为某个DLL,也成为“宿主” 2. 宿主接口使宿主能够对运行库的更多方面进行控 ...
- 百度地图API自定义地图
http://api.map.baidu.com/lbsapi/creatmap/index.html http://developer.baidu.com/map/index.php?title=w ...
- EntityFramework 更新表结构到数据库
在程序包管理器控制台 1.执行:Enable-Migrations -Force 生成:Migrations 2 修改AutomaticMigrationsEnabled默认为false改为true ...
- oracle client ORA-12541: TNS: 无监听程序
1. Question description: if you are setting the oracle client to add a local network service, you m ...
- C语言范例学习01
编程语言的能力追求T型. 以前学过C语言,但是只学了理论. 从今天开始,我买了本<C语言程序开发范例宝典>.我要把它通关掉. 这应该可以极大地提升我的编程能力. 第一章 基础知识 这章没太 ...
- 多准则决策模型-TOPSIS方法
多准则决策–Multiple Criteria Decision Making 多准则决策–Multiple Criteria Decision Making 多准则决策是指在具有相互冲突.不可共度的 ...
- mysql root用户kill connection报ERROR 1095 (HY000): You are not owner of thread N
今日某系统mysql root用户kill connection时报ERROR 1095 (HY000): You are not owner of thread N 按说通过root用户具有supe ...
- css权重是什么
css权重是什么? 概述 css Specificity中文一般译为css优先级.css权重.相比"权重","优先级"更好理解,mozilla官方中文文档就翻译 ...
- SASS语法学习
一.嵌套 1.选择器嵌套 <header> <nav> <a href=“##”>Home</a> <a href=“##”>About&l ...
- C语言预处理命令之条件编译
在C语言中,预处理指令是以#号开头的代码行.#号必须是该行除了任何空白字符外的第一个字符.#后是指令关键字,在关键字和#号之间允许存在任意个数的空白字符.整行语句构成了一条预处理指令,该指令将在编译器 ...