DZY Loves Colors

题意:有n个蛋糕,起初第i个蛋糕他的颜色值为i, 现在有2个操作, 1 x  y  c 在[x, y]的蛋糕上都加上一层颜色值为c的蛋糕片,加了这个蛋糕片之后,会产生一个惊喜值为abs(c-a) a为上一层蛋糕片的颜色。

2 x y 求出[x, y]区间内所有蛋糕的惊喜值之和。

题解:暴力分块处理, 如果一个块被盖上了相同的颜色,那么下次这个块又被同一种颜色蛋糕片覆盖了的话就可以打一个lz标记,并且求和。

线段树也能写, 练习分块所以分块搞了半天, 然后一直wa, 重敲了一遍就AC了, 也不知道发生了啥,可能会补上线段树的写法。

代码:

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
  4 #define LL long long
  5 #define ULL unsigned LL
  6 #define fi first
  7 #define se second
  8 #define pb push_back
  9 #define lson l,m,rt<<1
 10 #define rson m+1,r,rt<<1|1
 11 #define max3(a,b,c) max(a,max(b,c))
 12 #define min3(a,b,c) min(a,min(b,c))
 13 #define _S(X) cout << x << ' ';
 14 #define __S(x) cout << x << endl;
 15 typedef pair<int,int> pll;
 16 const int INF = 0x3f3f3f3f;
 17 const LL mod =  (int)1e9+7;
 18 const int N = 2e5 + 100;
 19 int l[N], r[N], belong[N];
 20 LL lz[N], sum[N], v[N], a[N], tmp[N];
 21 int n, m, tot;
 22 void Build(){
 23     m = sqrt(n);
 24     tot = n/m;
 25     if(n%m) tot++;
 26     for(int i = 1; i <= n; i++)
 27         belong[i] = (i-1)/m + 1, a[i] = i;
 28     for(int i = 1; i <= tot; i++)
 29         l[i] = (i-1)*m+1, r[i] = i*m;
 30     r[tot] = n;
 31 }
 32 void PushDown(int rt){
 33     for(int i = l[rt]; i <= r[rt]; i++)
 34         a[i] = lz[rt];
 35     lz[rt] = 0;
 36 }
 37 void Update(int x, int y, LL c){
 38     int idx = belong[x], idy = belong[y];
 39     if(idx == idy){
 40         if(lz[idx]) PushDown(idx);
 41         for(int i = x; i <= y; i++){
 42             sum[idx] += abs(c - a[i]);
 43             v[i] += abs(c - a[i]);
 44             a[i] = c;
 45         }
 46     }
 47     else {
 48         if(lz[idx]) PushDown(idx);
 49         if(lz[idy]) PushDown(idy);
 50         for(int i = x; i <= r[idx]; i++){
 51             sum[idx] += abs(c - a[i]);
 52             v[i] += abs(c - a[i]);
 53             a[i] = c;
 54         }
 55         for(int i = l[idy]; i <= y; i++){
 56             sum[idy] += abs(c - a[i]);
 57             v[i] += abs(c - a[i]);
 58             a[i] = c;
 59         }
 60         for(int i = idx+1; i < idy; i++){
 61             if(lz[i]){
 62                 tmp[i] += abs(lz[i] - c);
 63                 sum[i] += (r[i]-l[i]+1)*(abs(lz[i]-c));
 64                 lz[i] = c;
 65             }
 66             else {
 67                 lz[i] = c;
 68                 for(int j = l[i]; j <= r[i]; j++){
 69                     v[j] += abs(a[j] - c);
 70                     sum[i] += abs(a[j]-c);
 71                     a[j] = c;
 72                 }
 73             }
 74         }
 75     }
 76 }
 77 void Query(int x, int y){
 78     int idx = belong[x], idy = belong[y];
 79     LL ans = 0;
 80     if(idx == idy){
 81         for(int i = x; i <= y; i++)
 82             ans += (tmp[idx] + v[i]);
 83     }
 84     else {
 85         for(int i = x; i <= r[idx]; i++)
 86             ans += (tmp[idx] + v[i]);
 87         for(int i = l[idy]; i <= y; i++)
 88             ans += (tmp[idy] + v[i]);
 89         for(int i = idx+1; i < idy; i++)
 90             ans += sum[i];
 91     }
 92     printf("%lld\n", ans);
 93 }
 94 int main(){
 95     int q, x, y, c, op;
 96     scanf("%d%d", &n, &q);
 97     Build();
 98     while(q--){
 99         scanf("%d%d%d", &op, &x, &y);
100         if(op == 1) {
101             scanf("%d", &c);
102             Update(x,y,c);
103         }
104         else {
105             Query(x,y);
106         }
107
108     }
109     return 0;
110 }

CF 444 C

转载于:https://www.cnblogs.com/MingSD/p/9119191.html

CodeForces 444 C DZY Loves Colors相关推荐

  1. Codeforces 444 C - DZY Loves Colors

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

  2. 444 C DZY Loves Colors

    /*线段树,注意传递参数的写法*/ #include <cstdio> #include <algorithm> using namespace std;#define LL ...

  3. Codeforces 444C DZY Loves Colors 线段树区间更新

    // Codeforces 444C DZY Loves Colors 线段树区间更新// 题目链接:// http://codeforces.com/problemset/problem/444/C ...

  4. [CodeForces - 447D] D - DZY Loves Modification

    D - DZY Loves Modification As we know, DZY loves playing games. One day DZY decided to play with a n ...

  5. CodeForces - 444C DZY Loves Colors(线段树+剪枝)

    题目链接:点击查看 题目大意:给出一个长度为 n 的数组 a 和计算贡献的数组 sum,需要执行 m 次操作,每次操作分为下列两种类型: 1 l r x:将区间 [ l , r ] 内的 a 用 x ...

  6. [Codeforces Round #254 div1] C.DZY Loves Colors 【线段树】

    题目链接:CF Round #254 div1 C 题目分析 这道题目是要实现区间赋值的操作,同时还要根据区间中原先的值修改区间上的属性权值. 如果直接使用普通的线段树区间赋值的方法,当一个节点表示的 ...

  7. Codeforces 444C DZY Loves colors(分块)

    我们维护一个标记表示区间内的数是否全相同即可. 如果全相同很容易算出 a , b a,b a,b 数组需要更新多少,打标记即可. 否则暴力修改. #include <map> #inclu ...

  8. CodeForces 444C. DZY Loves Physics(枚举+水题)

    转载请注明出处:http://blog.csdn.net/u012860063/article/details/37509207 题目链接:http://codeforces.com/contest/ ...

  9. Codeforces - DZY Loves Sequences

    题目链接:Codeforces - DZY Loves Sequences 做一个前后缀连续 LIS ,然后枚举每个位置即可. 注意细节. AC代码: #pragma GCC optimize(&qu ...

最新文章

  1. 贝叶斯推断及其互联网应用(一)
  2. wxWidgets 的打印演示
  3. poj1064 二分搜索 挑战程序设计竞赛
  4. 计算机二级计划总结,计算机二级细节总结
  5. java 反编译 行号对齐 decompiler如何去掉行号
  6. tensorflow sigmoid 如何计算训练数据的正确率_“来自蒙娜丽莎的凝视”— 结合 TensorFlow.js 和深度学习实现...
  7. 【转】Eclipse下支持编写HTML/JS/CSS/JSP页面的自动提示
  8. Activity生命周期详解二
  9. 用inno 打包程序 学习之路(转载)
  10. 关于sg90舵机的一点小想法
  11. 如何让在 LaTeX 中添加脚注,并且文中不出现编号?
  12. 网易经典评论(一)创业
  13. linux终端联网网速慢,Linux下上网速度慢的问题及其解决方法
  14. Linux notifier chain
  15. 第二十二课--提示工具(Tooltip)
  16. ruoyi框架文件上传之后端代码测试及打印日志
  17. Conway(康威)定律理解
  18. 七(7)探花功能-MongoDB地理位置查询-附近的人
  19. 初中教师资格证科学计算机面试,2019下半年初中科学教师资格证面试真题及答案汇总...
  20. Visual Basic Script 程序参考手册-学习第3天:更系统的深入研究数据类型和数据转换

热门文章

  1. #Python3中判断是不是回文数
  2. 硬件学习系列——设计之中——3EMC 电磁兼容性再学习
  3. python requests max retries_Python requests“Max retries exceeded with url” error
  4. 基于ai的预测_基于AI的预测性维护可增强战备状态,减少飞行故障
  5. ai建立使用图案_教你用Illustrator描摹创建矢量图形
  6. GJB 5000B二级-VV验证与确认
  7. 优思学院|西门子精益六西格玛的历程
  8. PixelConFi·番外2 | 烤仔新版表情包,这次你说了算!
  9. evernote 云笔记_极客评论:使用Evernote管理和组织笔记
  10. BCDEdit 启动项编辑器2