题目链接:CF Round #254 div1 C

题目分析

这道题目是要实现区间赋值的操作,同时还要根据区间中原先的值修改区间上的属性权值。

如果直接使用普通的线段树区间赋值的方法,当一个节点表示的区间完全被要求修改的区间包含时,就直接打上赋值的标记然后 return 。但是这样这个节点中每个位置原先的值不同,需要进行的属性权值修改也就不同,是不能直接实现的。如果我们在节点表示的区间被修改的区间包含时,并不直接打标记 return ,而是当节点表示的区间被修改的区间完全包含而且这个节点中的每个位置的颜色相同时,才直接打标记然后 return ,否则就继续递归下去。这样就可以直接打标记修改属性权值了。但是这样看起来是会使复杂度退化的,但是实际上通过一些我不懂的势能分析,这样修改复杂度还是 O(log n) 的。所以这样这道题就变成线段树水题了。

需要维护一下每个节点表示的区间是否颜色相同。

区间赋值的题目就可以使用这种修改方式。

代码

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm> using namespace std; typedef long long LL; inline int Abs(int x) {return x < 0 ? -x : x;} const int MaxN = 100000 + 5; int n, m;
int Col[MaxN * 4], Len[MaxN * 4], Pc[MaxN * 4]; LL T[MaxN * 4], D[MaxN * 4]; bool Same[MaxN * 4]; inline void Update(int x)
{
Same[x] = Same[x << 1] && Same[x << 1 | 1] && Col[x << 1] == Col[x << 1 | 1];
if (Same[x]) Col[x] = Col[x << 1];
T[x] = T[x << 1] + T[x << 1 | 1];
Len[x] = Len[x << 1] + Len[x << 1 | 1];
} inline void Add(int x, LL Num)
{
D[x] += Num;
T[x] += (LL)Len[x] * Num;
} inline void PushDown(int x)
{
if (D[x] != 0)
{
Add(x << 1, D[x]);
Add(x << 1 | 1, D[x]);
D[x] = 0;
}
if (Pc[x] != 0)
{
Col[x << 1] = Pc[x << 1] = Pc[x];
Col[x << 1 | 1] = Pc[x << 1 | 1] = Pc[x];
Pc[x] = 0;
}
} void Build(int x, int s, int t)
{
if (s == t)
{
Same[x] = true;
Col[x] = s;
T[x] = D[x] = Pc[x] = 0;
Len[x] = 1;
return;
}
int m = (s + t) >> 1;
Build(x << 1, s, m);
Build(x << 1 | 1, m + 1, t);
Update(x);
} void Change(int x, int s, int t, int l, int r, int Num)
{
if (l <= s && r >= t && Same[x])
{
int Temp = Abs(Col[x] - Num);
D[x] += (LL)Temp;
T[x] += (LL)Temp * (LL)Len[x];
Col[x] = Pc[x] = Num;
return;
}
PushDown(x);
int m = (s + t) >> 1;
if (l <= m) Change(x << 1, s, m, l, r, Num);
if (r >= m + 1) Change(x << 1 | 1, m + 1, t, l, r, Num);
Update(x);
} LL Query(int x, int s, int t, int l, int r)
{
if (l <= s && r >= t) return T[x];
PushDown(x);
int m = (s + t) >> 1;
LL ret = 0;
if (l <= m) ret += Query(x << 1, s, m, l, r);
if (r >= m + 1) ret += Query(x << 1 | 1, m + 1, t, l, r);
return ret;
} int main()
{
scanf("%d%d", &n, &m);
Build(1, 1, n);
int f, l, r, Num;
for (int i = 1; i <= m; ++i)
{
scanf("%d%d%d", &f, &l, &r);
if (f == 1)
{
scanf("%d", &Num);
Change(1, 1, n, l, r, Num);
}
else printf("%I64d\n", Query(1, 1, n, l, r));
}
return 0;
}

  

[Codeforces Round #254 div1] C.DZY Loves Colors 【线段树】的更多相关文章

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

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

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

  3. CF444C. DZY Loves Colors[线段树 区间]

    C. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

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

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

  5. Codeforces Round #254 (Div. 2) DZY Loves Chemistry【并查集基础】

    一开始不知道题意是啥意思,迟放进去反应和后放进去反应有什么区别 对于第三组数据不是很懂,为啥312,132的组合是不行的 后来发现这是一道考察并查集的题目 QAQ 怒贴代码: #include < ...

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

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

  7. Codeforces #254 div1 B. DZY Loves FFT 暴力乱搞

    B. DZY Loves FFT 题目连接: http://codeforces.com/contest/444/problem/B Description DZY loves Fast Fourie ...

  8. Codeforces Round #FF 446 C. DZY Loves Fibonacci Numbers

    參考:http://www.cnblogs.com/chanme/p/3843859.html 然后我看到在别人的AC的方法里还有这么一种神方法,他预先设定了一个阈值K,当当前的更新操作数j<K ...

  9. Codeforces Round #603 (Div. 2) E. Editor(线段树)

    链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...

随机推荐

  1. lsb_release 提示命令不存在

    1. 报错信息 bash: lsb_release: command not found 2. 问题原因 未安装 lsb_release 命令 3. 解决方法 用 yum 命令安装 lsb_relea ...

  2. mysql内存使用情况

    可以直接使用top命令后,查看%MEM的内容.可以选择按进程查看或者按用户查看,如想查看oracle用户的进程内存使用情况的话可以使用如下的命令: (1)top top命令是Linux下常用的性能分析 ...

  3. linux学习记录(第六章、Linux 的文件权限与目录配置)

    书看的是鸟哥的私房菜,系统用的是centos.被微软坑了N年才发现linux才是王道. 在这里记录些学习的记录.备忘

  4. C#泛型集合—Dictionary<K,V>使用技巧

    转载:http://blog.csdn.net/a125138/article/details/7742022 1.要使用Dictionary集合,需要导入C#泛型命名空间 System.Collec ...

  5. jquery操作iframe中的js函数

    关键字:jquery操作iframe中的js函数 1.jquery操作iframe中的元素(2种方式) var tha = $(window.frames["core_content&quo ...

  6. ng1中 如何用双向绑定 实现单向绑定的初始时不显示双括号效果?

    ng1中 如何用双向绑定 实现单向绑定(ng-bind就可以不显示{{}})的初始时不显示双括号效果? AngularJS 实例 页面加载时防止应用闪烁: <div ng-app="& ...

  7. java中split以"."分割的问题

    今天开发中使用字符串分割函数split(),发现:输出的并不是想要的结果 或者直接报错都有可能 查询后才发现,需要转译 原来在java中函数split(".")必须是是split( ...

  8. LSJ_NHibernate第二章 ManagerPage

    前言: 项目为传统的三层架构,可以根据个人的需求进行拓展. 很多人都在质疑B层的作用,我认为B层才是核心,这个取决于业务的复杂度 项目的结构也比较的简单,我们先从最底层说起,ManagerPage,这 ...

  9. 二、 What's Maven,How to learning?

    1. 哈哈,什么是Maevn, ←_←|| ?我怎么知道,来看看官方解释, Apache Maven is a software project management and comprehensio ...

  10. poi 导入/导出 工具类

    package com.holy.util; import java.io.File; import java.io.FileOutputStream; import java.io.IOExcept ...