题目链接: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. Ubuntu下配置tftp服务

    Ubuntu下配置tftp服务 1.安装TFTP软件 sudo apt-get install tftp-hpa tftpd-hpa tftp-hpa是客户端,tftpd-hpa是服务器端 2.建立t ...

  2. Android EventBus

    https://github.com/greenrobot/EventBus onEvent:如果使用onEvent作为订阅函数,那么该事件在哪个线程发布出来的,onEvent就会在这个线程中运行,也 ...

  3. android自定义View之仿通讯录侧边栏滑动,实现A-Z字母检索

    我们的手机通讯录一般都有这样的效果,如下图: OK,这种效果大家都见得多了,基本上所有的android手机通讯录都有这样的效果.那我们今天就来看看这个效果该怎么实现. 一.概述 1.页面功能分析 整体 ...

  4. 第一章建立asp.net MVC

    第一步 第二步 创建controller 创建View view和controller之间的关系

  5. Ganymed SSH-2 for Java

    Ganymed SSH-2 for Java是一个纯Java实现的SHH2库,官网为http://www.ganymed.ethz.ch/ssh2/,最新的更新时间为2006年10月,在用之前,请仔细 ...

  6. 理解Objective-c中的copy

    说一下深拷贝和浅拷贝的基本概念:a指针指向地址A1, 浅拷贝是创建了一个b指针指向地址A1:深拷贝是创建了一个c指针指向地址A2,A1和A2的地址不同. 我们看到NSObject接口里面是已经声明了c ...

  7. Bootstrap--组件之下拉菜单

    用于显示链接列表的可切换.有上下文的菜单. 对齐 B默认情况下,下拉菜单自动沿着父元素的上沿和左侧被定位为 100% 宽度. 为 .dropdown-menu 添加 .dropdown-menu-ri ...

  8. Ext.Net学习笔记09:Ext.Net Store的用法

    使用Handler处理分页 首先来创建一般处理程序,我命名为StoreHandler.ashx,然后它的处理过程代码如下: public void ProcessRequest(HttpContext ...

  9. mysql更改root密码及root远程登录

    1.更改root密码 use mysql; update user set password=password('petecc') where user='root'; 2.root远程登录 1 up ...

  10. PHP学习笔记(五)

    关于Response header 的一些小知识: Host :address url,  host 是浏览器给服务器提供的address标识.由于http协议是无状态的,服务器需要根据host的这个 ...