Codeforces 444 C. DZY Loves Colors (线段树+剪枝)
题目链接:http://codeforces.com/contest/444/problem/C
给定一个长度为n的序列,初始时ai=i,vali=0(1≤i≤n).有两种操作:
- 将区间[L,R]的值改为x,并且当一个数从y改成x时它的权值vali会增加|x−y|.
- 询问区间[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 (线段树+剪枝)的更多相关文章
- 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 444C DZY Loves Colors(线段树)
题目大意:Codeforces 444C DZY Loves Colors 题目大意:两种操作,1是改动区间上l到r上面德值为x,2是询问l到r区间总的改动值. 解题思路:线段树模板题. #inclu ...
- CF444C. DZY Loves Colors[线段树 区间]
C. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces 444 C - DZY Loves Colors
C - DZY Loves Colors 思路: 分块,复杂度有点玄学,和普通分块不同的是在这个块被一次染色的时候暴力染整个块. 代码: #pragma GCC optimize(2) #pragma ...
- HDU5649 DZY Loves Sorting 线段树
题意:BC 76 div1 1004 有中文题面 然后奉上官方题解: 这是一道良心的基础数据结构题. 我们二分a[k]的值,假设当前是mid,然后把大于mid的数字标为1,不大于mid的数字标为0.然 ...
- Codeforces Round #254 DZY Loves Colors
题意:输入n, m ; 有n给位置, 初始时第i个位置的color为i, colorfulness为0. 有m次操作,一种是把成段的区域color更新为x, 对于更新的区域,每个位置(令第i ...
- Codeforces444C DZY Loves Colors(线段树)
题目 Source http://codeforces.com/problemset/problem/444/C Description DZY loves colors, and he enjoys ...
- Codeforces Round #254 (Div. 1) C. DZY Loves Colors 分块
C. DZY Loves Colors 题目连接: http://codeforces.com/contest/444/problem/C Description DZY loves colors, ...
随机推荐
- ZeptoLab Code Rush 2015
A 题意:给出一串由.*组成的字符串,如果有等间距的五个及五个以上的*存在,则输出yes 直接枚举就可以了 看题一定要仔细啊,做的时候看成必须有五个等间距的".*"才可以跳跃= = ...
- 【C#学习笔记】改变颜色
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 【英语】Bingo口语笔记(20) - i长短音
短音有个ei的音,多练习下 长音是咦拉长
- 不可或缺的 sendEmail
还在为Linux下没有便捷的邮件程序苦恼,还在为复杂的邮件服务器架设Google N多网页? 对于小型,便捷的Linux下命令行邮件程序,sendEmail使得这一切变得轻松可行.一起来看看吧. 一. ...
- Android RecyclerView使用详解(三)
在上一篇(RecyclerView使用详解(二))文章中介绍了RecyclerView的多Item布局实现,接下来要来讲讲RecyclerView的Cursor实现,相较于之前的实现,Cursor有更 ...
- Android 开源项目DiskLruCache 详解
有兴趣的同学可以读完这篇文章以后 可以看看这个硬盘缓存和volley 或者是其他 图片缓存框架中使用的硬盘缓存有什么异同点. 讲道理的话,其实硬盘缓存这个模块并不难写,难就难在 你要考虑到百分之0.1 ...
- 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:1.资源准备
最近,在VmwareStation 10虚拟机上,基于CentOS5.4安装Oracle 11g RAC,并把过程记录下来.刚开始时,是基于CentOS 6.4安装Oracle 11g RAC, 没有 ...
- [再寄小读者之数学篇](2014-11-19 $\sin(x+y)=\sin x\cos y+\cos x\sin y$)
$$\bex \sin(x+y)=\sin x\cos y+\cos x\sin y. \eex$$ Ref. [Proof Without Words: Sine Sum Identity, The ...
- 使用Yii框架自带的CActiveForm实现ajax提交表单
Php代码: <div class="form"> <?php $form=$this->beginWidget('CActiveForm', array ...
- Ubuntu 升级到13.10之后出现Apache2启动失败的问题
昨天看到Ubuntu 13.04提示有新的发行版Ubuntu 13.10了,手痒了一下,没有忍住就升级了. 结果升级完毕之后发现Apache2服务启动失败了,失败信息是: Invalid comman ...