UVA11992 - Fast Matrix Operations(线段树区间改动)

题目链接

题目大意:给你个r*c的矩阵,初始化为0。

然后给你三种操作:

1 x1, y1, x2, y2, v 把由x1,y1, x2, y2构成的子矩阵里的每一个元素都加上v。

2 x1, y1, x2, y2, v 把这个子矩阵的每一个元素都改动为v。

3 x1, y1, x2, y2 查询这个子矩阵的元素之和,和这些元素的最大值和最小值。

解题思路:由于矩阵的行最多20行,所以能够将这个矩阵的元素构建一棵线段树,每一个节点都有三个附加信息:sum,Max_num, Min_num.然后改动查询的时候就每行每行的来做。注意:每次set的时候都要将这个节点的add清理掉。由于add已经是不须要的了。

然后每次的pushdown都要先处理set再处理add。pushdown处理的是这个节点的子节点。然后通过pushup就能够更新这个节点的附加信息。

代码:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. using namespace std;
  5. #define lson(x) ((x)<<1)
  6. #define rson(x) (((x)<<1) + 1)
  7. const int N = 1e6 + 5;
  8. const int INF = 0x3f3f3f3f;
  9. struct Item {
  10. int sum, Max_num, Min_num;
  11. Item (int sum = 0, int Max_num = -INF, int Min_num = INF) {
  12. this->set(sum, Max_num, Min_num);
  13. }
  14. void set (int sum, int Max_num, int Min_num) {
  15. this->sum = sum;
  16. this->Max_num = Max_num;
  17. this->Min_num = Min_num;
  18. }
  19. };
  20. struct Node {
  21. Item v;
  22. int l, r;
  23. int addv, setv;
  24. void set (int l, int r, int addv = 0, int setv = -1) {
  25. this->l = l;
  26. this->r = r;
  27. this->addv = addv;
  28. this->setv = setv;
  29. }
  30. }node[4 * N];
  31. Item get_item (Item a, Item b) {
  32. return Item(a.sum + b.sum, max(a.Max_num, b.Max_num), min(a.Min_num, b.Min_num));
  33. }
  34. void Node_add(int u, int addv) {
  35. node[u].addv += addv;
  36. node[u].v.sum += addv * (node[u].r - node[u].l + 1);
  37. node[u].v.Max_num += addv;
  38. node[u].v.Min_num += addv;
  39. }
  40. void Node_set(int u, int setv) {
  41. node[u].setv = setv;
  42. node[u].addv = 0;
  43. node[u].v.sum = setv * (node[u].r - node[u].l + 1);
  44. node[u].v.Max_num = node[u].v.Min_num = setv;
  45. }
  46. void pushup (int u) {
  47. node[u].set(node[lson(u)].l, node[rson(u)].r);
  48. node[u].v = get_item(node[lson(u)].v, node[rson(u)].v);
  49. }
  50. void pushdown(int u) {
  51. if (node[u].setv >= 0) {
  52. Node_set(lson(u), node[u].setv);
  53. Node_set(rson(u), node[u].setv);
  54. node[u].setv = -1;
  55. }
  56. if (node[u].addv) {
  57. Node_add(lson(u), node[u].addv);
  58. Node_add(rson(u), node[u].addv);
  59. node[u].addv = 0;
  60. }
  61. }
  62. void Build (int u, int l, int r) {
  63. if (l == r) {
  64. node[u].set(l, r);
  65. node[u].v.set(0, 0, 0);
  66. } else {
  67. int m = (l + r) / 2;
  68. Build(lson(u), l, m);
  69. Build(rson(u), m + 1, r);
  70. pushup(u);
  71. }
  72. }
  73. void Add (int u, int l, int r, int addv) {
  74. if (node[u].l >= l && node[u].r <= r)
  75. Node_add(u, addv);
  76. else {
  77. pushdown(u);
  78. int m = (node[u].l + node[u].r) / 2;
  79. if (l <= m)
  80. Add (lson(u), l, r, addv);
  81. if (r > m)
  82. Add (rson(u), l, r, addv);
  83. pushup(u);
  84. }
  85. }
  86. void Set (int u, int l, int r, int setv) {
  87. if (node[u].l >= l && node[u].r <= r)
  88. Node_set(u, setv);
  89. else {
  90. pushdown(u);
  91. int m = (node[u].l + node[u].r) / 2;
  92. if (l <= m)
  93. Set (lson(u), l, r, setv);
  94. if (r > m)
  95. Set (rson(u), l, r, setv);
  96. pushup(u);
  97. }
  98. }
  99. Item Query (int u, int l, int r) {
  100. if (node[u].l >= l && node[u].r <= r)
  101. return node[u].v;
  102. else {
  103. Item ans;
  104. pushdown(u);
  105. int m = (node[u].l + node[u].r) / 2;
  106. if (l <= m)
  107. ans = get_item (ans, Query(lson(u), l, r));
  108. if (r > m)
  109. ans = get_item (ans, Query(rson(u), l, r));
  110. pushup(u);
  111. return ans;
  112. }
  113. }
  114. int main () {
  115. int r, c, m;
  116. int type;
  117. int x1, y1, x2, y2, v;
  118. while (scanf ("%d%d%d", &r, &c, &m) != EOF) {
  119. Build(1, 1, r * c);
  120. while (m--) {
  121. scanf ("%d", &type);
  122. if (type == 1) {
  123. scanf ("%d%d%d%d%d", &x1, &y1, &x2, &y2, &v);
  124. for (int i = x1 - 1; i < x2; i++)
  125. Add(1, i * c + y1, i * c + y2, v);
  126. } else if (type == 2) {
  127. scanf ("%d%d%d%d%d", &x1, &y1, &x2, &y2, &v);
  128. for (int i = x1 - 1; i < x2; i++)
  129. Set(1, i * c + y1, i * c + y2, v);
  130. } else {
  131. scanf ("%d%d%d%d", &x1, &y1, &x2, &y2);
  132. Item ans;
  133. for (int i = x1 - 1; i < x2; i++)
  134. ans = get_item(ans, Query(1, i * c + y1, i * c + y2));
  135. printf ("%d %d %d\n", ans.sum, ans.Min_num, ans.Max_num);
  136. }
  137. }
  138. }
  139. return 0;
  140. }

版权声明:本文博主原创文章,博客,未经同意不得转载。

UVA11992 - Fast Matrix Operations(段树部分的变化)的更多相关文章

  1. UVA 11992 - Fast Matrix Operations(段树)

    UVA 11992 - Fast Matrix Operations 题目链接 题意:给定一个矩阵,3种操作,在一个矩阵中加入值a,设置值a.查询和 思路:因为最多20列,所以全然能够当作20个线段树 ...

  2. uva 11992 Fast Matrix Operations 线段树模板

    注意 setsetset 和 addvaddvaddv 标记的下传. 我们可以控制懒惰标记的优先级. 由于 setsetset 操作的优先级高于 addaddadd 操作,当下传 setsetset ...

  3. [uva11992]Fast Matrix Operations(多延迟标记,二维线段树,区间更新)

    题目链接:https://vjudge.net/problem/UVA-11992 题意:n*m的矩阵,每次对一个子矩阵操作,有三种操作:加x,设置为x,查询.查询返回子矩阵和.最小值.最大值 n很小 ...

  4. UVA11992 Fast Matrix Operations

    思路 注意到最多20行,拆成20颗线段树即可 注意set标记清空左右儿子的add,不要清空自己的add,因为这个set操作之后可能还有add存在这个节点上 代码 #include <cstdio ...

  5. Fast Matrix Operations

    A Simple Problem with Integers 每次将区间向下更新,或是用之前的方法,统计当前节点到父节点处的覆盖数目. #include <cstdio> #include ...

  6. Fast Matrix Operations(UVA)11992

    UVA 11992 - Fast Matrix Operations 给定一个r*c(r<=20,r*c<=1e6)的矩阵,其元素都是0,现在对其子矩阵进行操作. 1 x1 y1 x2 y ...

  7. 【UVA】11992 - Fast Matrix Operations(段树模板)

    主体段树,要注意,因为有set和add操作,当慵懒的标志下推.递归优先set,后复发add,每次运行set行动add马克清0 WA了好几次是由于计算那一段的时候出问题了,可笑的是我对着模板找了一个多小 ...

  8. 线段树(多维+双成段更新) UVA 11992 Fast Matrix Operations

    题目传送门 题意:训练指南P207 分析:因为矩阵不超过20行,所以可以建20条线段的线段树,支持两个区间更新以及区间查询. #include <bits/stdc++.h> using ...

  9. luogu题解 UVA11992 【Fast Matrix Operations】

    题目链接: https://www.luogu.org/problemnew/show/UVA11992 题目大意: 一个r*c的矩阵,一开始元素都是0,然后给你m次三种操作,分别是将一个子矩阵中所有 ...

随机推荐

  1. Coreseek:部门查询和增量索引代替实时索引

    1.行业调查 索引系统需要通过主查询来获取所有的文档信息,一个简单的实现是整个表的数据到内存,但是这可能会导致整个表被锁定,并且使其它操作被阻止(例如:在MyISAM格款式上INSERT操作).同时, ...

  2. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  3. AIX 7.1 install python

    周围环境AIX7.1   设备python-2.6.2  因为互联网是非常多的安装文档.而且也没有细挑的版本号.因为我觉得python2.6 可能相对保守一些,至少之前用到的版本号是这个.所以此处依旧 ...

  4. Gradle cookbook(转)

    build.gradle apply plugin:"java" [compileJava,compileTestJava,javadoc]*.options*.encoding ...

  5. Light OJ 1406 Assassin`s Creed 减少国家DP+支撑点甚至通缩+最小路径覆盖

    标题来源:problem=1406">Light OJ 1406 Assassin`s Creed 意甲冠军:向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路: ...

  6. 在linux上创建nfs遇到的问题。

    我们部署程序时,图片server是单独的一台server,有自己独立的域名.而应用部署在还有一台server上,我们使用一些附件上传工具.比方ajaxfileupload上传附件时是无法跨域訪问的. ...

  7. richedit设置滚动条的位置和更新内容

    需要txt发现读者richedit的scrollbar位置(为了便于下一次读,直接访问与上次读取下一个读取位置)不值得治疗,采用GetScrollPos.SetScrollPos你可以设置scorll ...

  8. String.format()【演示具体的例子来说明】

    String.format()[演示样例具体解释] 整理者:Vashon 前言: String.format 作为文本处理工具.为我们提供强大而丰富的字符串格式化功能,为了不止步于简单调用 Strin ...

  9. Cocos2d-x-Lua 开发一个简单的游戏(记数字步进白色块状)

    Cocos2d-x-Lua 开发一个简单的游戏(记数字步进白色块状) 本篇博客来给大家介绍怎样使用Lua这门语言来开发一个简单的小游戏-记数字踩白块. 游戏的流程是这种:在界面上生成5个数1~5字并显 ...

  10. 【剑指offer】面试题28:弦乐

    def Permutation(data, i): if len( data ) == 0: return # i stand for the start of first part for i in ...