题目链接:http://codeforces.com/contest/444/problem/C

给定一个长度为n的序列,初始时ai=i,vali=0(1≤i≤n).有两种操作:

  1. 将区间[L,R]的值改为x,并且当一个数从y改成x时它的权值vali会增加|x−y|.
  2. 询问区间[L,R]的权值和.

n≤10^5,1≤x≤10^6.

感觉这是一个比较好的考察线段树区间更新的性质。

当区间的a[i]一样时,区间更新即可,这是剪枝。

注意,lazy标记存的是增量。

 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e5 + ;
struct SegTree {
LL l, r, Min, Max;
LL val, lazy;
}T[N << ]; LL Abs(LL a) {
return a < ? -a : a;
} void pushup(int p) {
T[p].Min = min(T[p << ].Min, T[(p << )|].Min);
T[p].Max = max(T[p << ].Max, T[(p << )|].Max);
T[p].val = T[p << ].val + T[(p << )|].val;
} void pushdown(int p) {
if(T[p].l == T[p].r) {
return ;
} else if(T[p].lazy) {
T[p << ].lazy += T[p].lazy;
T[(p << )|].lazy += T[p].lazy;
T[p << ].val += T[p].lazy*(LL)(T[p << ].r - T[p << ].l + );
T[(p << )|].val += T[p].lazy*(LL)(T[(p << )|].r - T[(p << )|].l + );
T[p << ].Min = T[p << ].Max = T[(p << )|].Max = T[(p << )|].Min = T[p].Min;
T[p].lazy = ;
}
} void build(int p, int l, int r) {
int mid = (l + r) >> ;
T[p].l = l, T[p].r = r, T[p].lazy = ;
if(l == r) {
T[p].Max = l, T[p].Min = l;
T[p].val = ;
return ;
}
build(p << , l, mid);
build((p << )|, mid + , r);
pushup(p);
} void update(int p, int l, int r, LL add) {
int mid = (T[p].l + T[p].r) >> ;
pushdown(p);
if(l == T[p].l && T[p].r == r && T[p].Min == T[p].Max) {
T[p].val += (LL)Abs(add - T[p].Min)*(LL)(r - l + );
T[p].lazy += Abs(add - T[p].Max);
T[p].Max = T[p].Min = add;
return ;
}
if(r <= mid) {
update(p << , l, r, add);
} else if(l > mid) {
update((p << )|, l, r, add);
} else {
update(p << , l, mid, add);
update((p << )|, mid + , r, add);
}
pushup(p);
} LL query(int p, int l, int r) {
int mid = (T[p].l + T[p].r) >> ;
pushdown(p);
if(l == T[p].l && T[p].r == r) {
return T[p].val;
}
if(r <= mid) {
return query(p << , l, r);
} else if(l > mid) {
return query((p << )|, l, r);
} else {
return query(p << , l, mid) + query((p << )|, mid + , r);
}
pushup(p);
} int main()
{
int n, m, l, r, c;
LL add;
scanf("%d %d", &n, &m);
build(, , n);
while(m--) {
scanf("%d %d %d", &c, &l, &r);
if(c == ) {
scanf("%lld", &add);
update(, l, r, add);
} else {
printf("%lld\n", query(, l, r));
}
}
return ;
}

Codeforces 444 C. DZY Loves Colors (线段树+剪枝)的更多相关文章

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

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

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

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

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

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

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

  5. Codeforces 444 C - DZY Loves Colors

    C - DZY Loves Colors 思路: 分块,复杂度有点玄学,和普通分块不同的是在这个块被一次染色的时候暴力染整个块. 代码: #pragma GCC optimize(2) #pragma ...

  6. HDU5649 DZY Loves Sorting 线段树

    题意:BC 76 div1 1004 有中文题面 然后奉上官方题解: 这是一道良心的基础数据结构题. 我们二分a[k]的值,假设当前是mid,然后把大于mid的数字标为1,不大于mid的数字标为0.然 ...

  7. Codeforces Round #254 DZY Loves Colors

    题意:输入n, m ; 有n给位置, 初始时第i个位置的color为i, colorfulness为0.      有m次操作,一种是把成段的区域color更新为x, 对于更新的区域,每个位置(令第i ...

  8. Codeforces444C DZY Loves Colors(线段树)

    题目 Source http://codeforces.com/problemset/problem/444/C Description DZY loves colors, and he enjoys ...

  9. Codeforces Round #254 (Div. 1) C. DZY Loves Colors 分块

    C. DZY Loves Colors 题目连接: http://codeforces.com/contest/444/problem/C Description DZY loves colors, ...

随机推荐

  1. [原创] - C#编程大幅提高OUTLOOK的邮件搜索能力!

    使用OUTLOOK, 你有没有遇到过上图的问题? 多达18419封邮件! 太多了, 每次想找一个邮件都非常耗时, 想办法解决这个问题成了一件非常紧迫的事情. 利用MS Search当然可以, 但是它太 ...

  2. spring tx:advice 和 aop:config 配置事务

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  3. Oracle安装错误ora-00922(zhuan)

    Oracle安装错误ora-00922(缺少或无效选项) (2012-03-19 10:49:27) 转载▼ 标签: 杂谈   安装Oracle 11g R2的过程中,在新建数据库实例时出现了该错误, ...

  4. 几个排序算法的JS实现

    最近找工作,复习一下数据结构的知识,看到排序这一块,顺便动手改了一下. 直接插入排序: 插入排序就是把数据分为有序区和无序区,遍历到的数据和有序区域的数据进行比较,找到要插入的位置,插入位置后的数据做 ...

  5. Oracle 11gR2用gpnp profile存放ASM的spfile路径

    从Oracle 11gR2开始,GI集成了ASM,OCR/VOTEDISK也存放在ASM磁盘组了(11gR2以前需要存放于裸设备中),同时ASM的功能较10g也有很大增强. 我们先引入一个问题:11g ...

  6. Hadoop学习总结之三:Map-Reduce入门

    1.Map-Reduce的逻辑过程 假设我们需要处理一批有关天气的数据,其格式如下: 按照ASCII码存储,每行一条记录 每一行字符从0开始计数,第15个到第18个字符为年 第25个到第29个字符为温 ...

  7. Android按键事件传递流程(二)

    5    应用层如何从Framework层接收按键事件 由3.2和4.5.4节可知,当InputDispatcher通过服务端管道向socket文件描述符发送消息后,epoll机制监听到了I/O事件, ...

  8. 【转】IOS 开发环境,证书和授权文件等详解

    (转自:http://blog.csdn.net/gtncwy/article/details/8617788) 一.成员介绍1.    Certification(证书)证书是对电脑开发资格的认证, ...

  9. Math.random();函数 随机数

    random函数参数 无参数 random函数返回值 返回0和1之间的伪随机数,可能为0,但总是小于1,[0,1) random函数示例 document.write(Math.random()); ...

  10. oracle 常用语句

    创建用户及授权create temporary tablespace test_temp tempfile 'C:\oracle\product\10.2.0\oradata\hszxdbtemp.d ...