题目链接: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标记存的是增量。

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long LL;
  4. const int N = 1e5 + ;
  5. struct SegTree {
  6. LL l, r, Min, Max;
  7. LL val, lazy;
  8. }T[N << ];
  9.  
  10. LL Abs(LL a) {
  11. return a < ? -a : a;
  12. }
  13.  
  14. void pushup(int p) {
  15. T[p].Min = min(T[p << ].Min, T[(p << )|].Min);
  16. T[p].Max = max(T[p << ].Max, T[(p << )|].Max);
  17. T[p].val = T[p << ].val + T[(p << )|].val;
  18. }
  19.  
  20. void pushdown(int p) {
  21. if(T[p].l == T[p].r) {
  22. return ;
  23. } else if(T[p].lazy) {
  24. T[p << ].lazy += T[p].lazy;
  25. T[(p << )|].lazy += T[p].lazy;
  26. T[p << ].val += T[p].lazy*(LL)(T[p << ].r - T[p << ].l + );
  27. T[(p << )|].val += T[p].lazy*(LL)(T[(p << )|].r - T[(p << )|].l + );
  28. T[p << ].Min = T[p << ].Max = T[(p << )|].Max = T[(p << )|].Min = T[p].Min;
  29. T[p].lazy = ;
  30. }
  31. }
  32.  
  33. void build(int p, int l, int r) {
  34. int mid = (l + r) >> ;
  35. T[p].l = l, T[p].r = r, T[p].lazy = ;
  36. if(l == r) {
  37. T[p].Max = l, T[p].Min = l;
  38. T[p].val = ;
  39. return ;
  40. }
  41. build(p << , l, mid);
  42. build((p << )|, mid + , r);
  43. pushup(p);
  44. }
  45.  
  46. void update(int p, int l, int r, LL add) {
  47. int mid = (T[p].l + T[p].r) >> ;
  48. pushdown(p);
  49. if(l == T[p].l && T[p].r == r && T[p].Min == T[p].Max) {
  50. T[p].val += (LL)Abs(add - T[p].Min)*(LL)(r - l + );
  51. T[p].lazy += Abs(add - T[p].Max);
  52. T[p].Max = T[p].Min = add;
  53. return ;
  54. }
  55. if(r <= mid) {
  56. update(p << , l, r, add);
  57. } else if(l > mid) {
  58. update((p << )|, l, r, add);
  59. } else {
  60. update(p << , l, mid, add);
  61. update((p << )|, mid + , r, add);
  62. }
  63. pushup(p);
  64. }
  65.  
  66. LL query(int p, int l, int r) {
  67. int mid = (T[p].l + T[p].r) >> ;
  68. pushdown(p);
  69. if(l == T[p].l && T[p].r == r) {
  70. return T[p].val;
  71. }
  72. if(r <= mid) {
  73. return query(p << , l, r);
  74. } else if(l > mid) {
  75. return query((p << )|, l, r);
  76. } else {
  77. return query(p << , l, mid) + query((p << )|, mid + , r);
  78. }
  79. pushup(p);
  80. }
  81.  
  82. int main()
  83. {
  84. int n, m, l, r, c;
  85. LL add;
  86. scanf("%d %d", &n, &m);
  87. build(, , n);
  88. while(m--) {
  89. scanf("%d %d %d", &c, &l, &r);
  90. if(c == ) {
  91. scanf("%lld", &add);
  92. update(, l, r, add);
  93. } else {
  94. printf("%lld\n", query(, l, r));
  95. }
  96. }
  97. return ;
  98. }

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. ZeptoLab Code Rush 2015

    A 题意:给出一串由.*组成的字符串,如果有等间距的五个及五个以上的*存在,则输出yes 直接枚举就可以了 看题一定要仔细啊,做的时候看成必须有五个等间距的".*"才可以跳跃= = ...

  2. 【C#学习笔记】改变颜色

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. 【英语】Bingo口语笔记(20) - i长短音

    短音有个ei的音,多练习下 长音是咦拉长

  4. 不可或缺的 sendEmail

    还在为Linux下没有便捷的邮件程序苦恼,还在为复杂的邮件服务器架设Google N多网页? 对于小型,便捷的Linux下命令行邮件程序,sendEmail使得这一切变得轻松可行.一起来看看吧. 一. ...

  5. Android RecyclerView使用详解(三)

    在上一篇(RecyclerView使用详解(二))文章中介绍了RecyclerView的多Item布局实现,接下来要来讲讲RecyclerView的Cursor实现,相较于之前的实现,Cursor有更 ...

  6. Android 开源项目DiskLruCache 详解

    有兴趣的同学可以读完这篇文章以后 可以看看这个硬盘缓存和volley 或者是其他 图片缓存框架中使用的硬盘缓存有什么异同点. 讲道理的话,其实硬盘缓存这个模块并不难写,难就难在 你要考虑到百分之0.1 ...

  7. 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:1.资源准备

    最近,在VmwareStation 10虚拟机上,基于CentOS5.4安装Oracle 11g RAC,并把过程记录下来.刚开始时,是基于CentOS 6.4安装Oracle 11g RAC, 没有 ...

  8. [再寄小读者之数学篇](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 ...

  9. 使用Yii框架自带的CActiveForm实现ajax提交表单

    Php代码:  <div class="form"> <?php $form=$this->beginWidget('CActiveForm', array ...

  10. Ubuntu 升级到13.10之后出现Apache2启动失败的问题

    昨天看到Ubuntu 13.04提示有新的发行版Ubuntu 13.10了,手痒了一下,没有忍住就升级了. 结果升级完毕之后发现Apache2服务启动失败了,失败信息是: Invalid comman ...