题目链接: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. 宽字节SQL注入

    1.联想lelink站 例1, 联想lelink站user参数存在宽字节SQL注入 提交,user=wctest%df’ and 1=2%23 结果,出现了”運”字,如图:

  2. 写个 Hello World 而已,要不要这么震撼?!

    Atom 编辑器的一个插件,可以让你写代码的时候体验狂拽酷炫的效果! 如果来点音乐.再配上机械键盘的话,写代码是不是爽到爆呢? 这货全名叫: activate-power-mode atom pack ...

  3. asp.net下载的方法1

    1. 首先新建一个用于进行下载处理的page页,如download.aspx,里面什么东西也没有. 2. 添加一个DownloadHandler类,它继承于IHttpHandler接口,可以用来自定义 ...

  4. WPF翻转动画

    在WPF中要翻转对象,估计是得用三维变换,所以我用到了AxisAngleRotation3D,让图形绕着Z轴来旋转. 先看看效果. 是的,就是这样的效果,在XAML中,由于涉及三维图形,我先做了两个用 ...

  5. c语言,strcmp(),字符串比较,看Asic 码,str1>str2,返回值 > 0;两串相等,返回

    #include<stdio.h> #include<string.h> int main() {  char *buffer1="aaa",*buffer ...

  6. /etc/resolv.conf文件详解

    大家好,今天51开源给大家介绍一个在配置文件,那就是/etc/resolv.conf.很多网友对此文件的用处不太了解.其实并不复杂,它是DNS客户机配置文件,用于设置DNS服务器的IP地址及DNS域名 ...

  7. Java获取某年某周的最后一天

    package test; import java.text.SimpleDateFormat; import java.util.Calendar; /** * ClassName: LastDay ...

  8. 【转】iOS屏幕适配

    一.iOS屏幕适配发展历程 设备 适配技术 4及以前(iPad未出) 直接用代码计算 有了iPad autoResizing 有不同屏幕的iPhone后 autoLayout 有更多不同屏幕的iPho ...

  9. OS X EL Capitan安装Cocoapods 报错ERROR

    升级OS X EL Capitan10.11之后,原来的pod不能用了,重新安装cocoapods,发现 在运行 “sudo gem install cocoapods” 的时候出现问题: ERROR ...

  10. 【转】Html标签大全

     Html标签大全 2013-07-05 18:22:33 分类: Python/Ruby Html标签大全 <a></a> 超文本链接 <a href="UR ...