想分块想了很久一点思路都没有,结果一看都是写的线段树= = 。。。完全忘记了还有线段树这种操作

题意:给一个数组,一种操作是改变l到r为c,还有一种操作是查询l到r的总和差

线段树记得+lazy标记

  1. #include<bits/stdc++.h>
  2. #define fi first
  3. #define se second
  4. #define mp make_pair
  5. #define pb push_back
  6. #define pii pair<int,int>
  7. #define C 0.5772156649
  8. #define pi acos(-1.0)
  9. #define ll long long
  10. #define mod 998244353
  11. #define ls l,m,rt<<1
  12. #define rs m+1,r,rt<<1|1
  13.  
  14. using namespace std;
  15.  
  16. const double g=10.0,eps=1e-;
  17. const int N=+,maxn=sqrt(N)+,inf=0x3f3f3f3f;
  18.  
  19. ll color[N<<],lazy[N<<],sum[N<<];
  20. bool same[N<<];
  21. void changecolor(ll c,ll x,int l,int r,int rt)//x是改变的差值,c是上一个颜色
  22. {
  23. same[rt]=;
  24. sum[rt]+=x*(r-l+);
  25. color[rt]=c;
  26. lazy[rt]+=x;
  27. }
  28. void pushup(int rt)
  29. {
  30. if(same[rt<<]&&same[rt<<|]&&color[rt<<]==color[rt<<|])
  31. {
  32. same[rt]=;
  33. color[rt]=color[rt<<];
  34. }
  35. else same[rt]=;
  36. sum[rt]=sum[rt<<]+sum[rt<<|];
  37. }
  38. void pushdown(int l,int r,int rt)
  39. {
  40. if(lazy[rt])
  41. {
  42. int m=(l+r)>>;
  43. changecolor(color[rt],lazy[rt],ls);
  44. changecolor(color[rt],lazy[rt],rs);
  45. lazy[rt]=;
  46. }
  47. }
  48. void build(int l,int r,int rt)
  49. {
  50. if(l==r)
  51. {
  52. color[rt]=l;
  53. same[rt]=;
  54. sum[rt]=;
  55. return ;
  56. }
  57. int m=(l+r)>>;
  58. build(ls);
  59. build(rs);
  60. pushup(rt);
  61. }
  62. void update(int l,int r,int rt,int L,int R,ll x)
  63. {
  64. if(L<=l&&r<=R&&same[rt])
  65. {
  66. changecolor(x,abs(x-color[rt]),l,r,rt);
  67. return ;
  68. }
  69. pushdown(l,r,rt);
  70. int m=(l+r)>>;
  71. if(L<=m)update(ls,L,R,x);
  72. if(m<R)update(rs,L,R,x);
  73. pushup(rt);
  74. }
  75. ll query(int l,int r,int rt,int L,int R)
  76. {
  77. if(L<=l&&r<=R)return sum[rt];
  78. pushdown(l,r,rt);
  79. int m=(l+r)>>;
  80. ll ans=;
  81. if(L<=m)ans+=query(ls,L,R);
  82. if(m<R)ans+=query(rs,L,R);
  83. return ans;
  84. }
  85. int main()
  86. {
  87. int n,m;
  88. scanf("%d%d",&n,&m);
  89. build(,n,);
  90. while(m--)
  91. {
  92. int x,y;
  93. ll z;
  94. scanf("%d",&x);
  95. if(x==)
  96. {
  97. scanf("%d%d%lld",&x,&y,&z);
  98. update(,n,,x,y,z);
  99. }
  100. else
  101. {
  102. scanf("%d%d",&x,&y);
  103. printf("%lld\n",query(,n,,x,y));
  104. }
  105. }
  106. return ;
  107. }
  108. /********************
  109.  
  110. ********************/

CodeForces 444C 线段树的更多相关文章

  1. DZY Loves Colors CodeForces - 444C (线段树势能分析)

    大意:有$n$个格子, 初始$i$位置的颜色为$i$, 美丽值为0, 有两种操作 将区间$[l,r]$内的元素全部改为$x$, 每个元素的美丽值增加$|x-y|$, $y$为未改动时的值 询问区间$[ ...

  2. Codeforces 444C 线段树 懒惰标记

    前天晚上的CF比赛div2的E题,很明显一个线段树,当时还在犹豫复杂度的问题,因为他是区间修改和区间查询,肯定是要用到懒惰标记. 然后昨天真的是给这道题跪了,写了好久好久,...我本来是写了个add标 ...

  3. Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论

    Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...

  4. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 831E) - 线段树 - 树状数组

    Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this int ...

  5. Codeforces 938G 线段树分治 线性基 可撤销并查集

    Codeforces 938G Shortest Path Queries 一张连通图,三种操作 1.给x和y之间加上边权为d的边,保证不会产生重边 2.删除x和y之间的边,保证此边之前存在 3.询问 ...

  6. codeforces 1136E 线段树

    codeforces 1136E: 题意:给你一个长度为n的序列a和长度为n-1的序列k,序列a在任何时候都满足如下性质,a[i+1]>=ai+ki,如果更新后a[i+1]<ai+ki了, ...

  7. Z - New Year Tree CodeForces - 620E 线段树 区间种类 bitset

    Z - New Year Tree CodeForces - 620E 这个题目还没有写,先想想思路,我觉得这个题目应该可以用bitset, 首先这个肯定是用dfs序把这个树转化成线段树,也就是二叉树 ...

  8. D - The Bakery CodeForces - 834D 线段树优化dp···

    D - The Bakery CodeForces - 834D 这个题目好难啊,我理解了好久,都没有怎么理解好, 这种线段树优化dp,感觉还是很难的. 直接说思路吧,说不清楚就看代码吧. 这个题目转 ...

  9. B - Legacy CodeForces - 787D 线段树优化建图+dij最短路 基本套路

    B - Legacy CodeForces - 787D 这个题目开始看过去还是很简单的,就是一个最短路,但是这个最短路的建图没有那么简单,因为直接的普通建图边太多了,肯定会超时的,所以要用线段树来优 ...

随机推荐

  1. C 和 C++ 的标准库分别有自己的 locale 操作方法,C 标准库的 locale 设定函数是 setlocale(),而 C++ 标准库有 locale 类和流对象的 imbue() 方法(gcc使用zh_CN.GBK,或者zh_CN.UTF-8,VC++使用Chinese_People's Republic of China.936或者65001.)

    转自:http://zyxhome.org/wp/cc-prog-lang/c-stdlib-setlocale-usage-note/ [在此向原文作者说声谢谢!若有读者看到文章转载时请写该转载地址 ...

  2. Hosts文件的位置

    Operating System Version(s) Location Unix, Unix-like, POSIX   /etc/hosts Microsoft Windows 3.1 %WinD ...

  3. DMR技术白皮书

    DMR技术白皮书 主页(http://pttcn.net):DMR技术白皮书 关于DMR 1.模拟技术的局限性 虽然模拟技术仍具有不少优势,如低廉的成本.可自定的功能以及简便的搭建方式等.但模拟技术已 ...

  4. javaweb action无法跳转、表单无法跳转的解决方法

    action无法跳转,表单无法跳转的解决方法 刚在网上搜索了一下,发现我的这篇文章已被非常多人转载了去其他站点.暗爽,只是还是希望大家注明出处. 顺便说明一下.下面是在struts2中通过測试的 ac ...

  5. c# 获取网页源代码(支持cookie),最简单代码

    /// /// 获取网页源码 public static string GetHtmls(string url, string referer = "", string cooki ...

  6. 20170330 webservice代理类测试

    代理类测试 执行事物码SE80,找到之前创建好的代理类,如下图所示: 双击该代理类,进入其显示界面,如下图所示: 点击执行按钮,或者快捷键F8.如下图所示:. 逻辑端口文本框就是之前创建的逻辑端口技术 ...

  7. php mysqli扩展库之预处理操作

    分享下php使用mysqli扩展库进行预处理操作的二个例子,有意研究mysqli用法的朋友,可以参考学习下,一定会有所帮助的. 例1.使用mysqli扩展库的预处理技术 mysqli stmt 向数据 ...

  8. pyhon时间输出

    参考博客:http://www.cnblogs.com/xisheng/p/7634125.html http://www.cnpythoner.com/post/89.html 有些时候想要输出,但 ...

  9. python删除列表中所有的空元素

    while '' in list: list.remove('')

  10. PAT 天梯赛 L1-028. 判断素数 【水】

    题目链接 https://www.patest.cn/contests/gplt/L1-028 AC代码 #include <iostream> #include <cstdio&g ...