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就能够更新这个节点的附加信息。

代码:

#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; #define lson(x) ((x)<<1)
#define rson(x) (((x)<<1) + 1) const int N = 1e6 + 5;
const int INF = 0x3f3f3f3f; struct Item { int sum, Max_num, Min_num; Item (int sum = 0, int Max_num = -INF, int Min_num = INF) {
this->set(sum, Max_num, Min_num);
} void set (int sum, int Max_num, int Min_num) {
this->sum = sum;
this->Max_num = Max_num;
this->Min_num = Min_num;
}
}; struct Node { Item v;
int l, r;
int addv, setv; void set (int l, int r, int addv = 0, int setv = -1) {
this->l = l;
this->r = r;
this->addv = addv;
this->setv = setv;
} }node[4 * N]; Item get_item (Item a, Item b) { return Item(a.sum + b.sum, max(a.Max_num, b.Max_num), min(a.Min_num, b.Min_num));
} void Node_add(int u, int addv) { node[u].addv += addv;
node[u].v.sum += addv * (node[u].r - node[u].l + 1);
node[u].v.Max_num += addv;
node[u].v.Min_num += addv;
} void Node_set(int u, int setv) { node[u].setv = setv;
node[u].addv = 0;
node[u].v.sum = setv * (node[u].r - node[u].l + 1);
node[u].v.Max_num = node[u].v.Min_num = setv;
} void pushup (int u) { node[u].set(node[lson(u)].l, node[rson(u)].r);
node[u].v = get_item(node[lson(u)].v, node[rson(u)].v);
} void pushdown(int u) { if (node[u].setv >= 0) {
Node_set(lson(u), node[u].setv);
Node_set(rson(u), node[u].setv);
node[u].setv = -1;
} if (node[u].addv) {
Node_add(lson(u), node[u].addv);
Node_add(rson(u), node[u].addv);
node[u].addv = 0;
}
} void Build (int u, int l, int r) { if (l == r) {
node[u].set(l, r);
node[u].v.set(0, 0, 0); } else { int m = (l + r) / 2;
Build(lson(u), l, m);
Build(rson(u), m + 1, r);
pushup(u);
}
} void Add (int u, int l, int r, int addv) { if (node[u].l >= l && node[u].r <= r)
Node_add(u, addv);
else { pushdown(u);
int m = (node[u].l + node[u].r) / 2;
if (l <= m)
Add (lson(u), l, r, addv);
if (r > m)
Add (rson(u), l, r, addv);
pushup(u);
}
} void Set (int u, int l, int r, int setv) { if (node[u].l >= l && node[u].r <= r)
Node_set(u, setv);
else { pushdown(u);
int m = (node[u].l + node[u].r) / 2;
if (l <= m)
Set (lson(u), l, r, setv);
if (r > m)
Set (rson(u), l, r, setv);
pushup(u);
}
} Item Query (int u, int l, int r) { if (node[u].l >= l && node[u].r <= r)
return node[u].v;
else { Item ans; pushdown(u);
int m = (node[u].l + node[u].r) / 2;
if (l <= m)
ans = get_item (ans, Query(lson(u), l, r));
if (r > m)
ans = get_item (ans, Query(rson(u), l, r));
pushup(u); return ans;
}
} int main () { int r, c, m;
int type;
int x1, y1, x2, y2, v; while (scanf ("%d%d%d", &r, &c, &m) != EOF) { Build(1, 1, r * c); while (m--) { scanf ("%d", &type); if (type == 1) { scanf ("%d%d%d%d%d", &x1, &y1, &x2, &y2, &v); for (int i = x1 - 1; i < x2; i++)
Add(1, i * c + y1, i * c + y2, v);
} else if (type == 2) { scanf ("%d%d%d%d%d", &x1, &y1, &x2, &y2, &v);
for (int i = x1 - 1; i < x2; i++)
Set(1, i * c + y1, i * c + y2, v);
} else { scanf ("%d%d%d%d", &x1, &y1, &x2, &y2); Item ans;
for (int i = x1 - 1; i < x2; i++)
ans = get_item(ans, Query(1, i * c + y1, i * c + y2));
printf ("%d %d %d\n", ans.sum, ans.Min_num, ans.Max_num);
}
}
}
return 0;
}

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

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. Excel阅读器NPOI

    什么是NPOI? NPOI 它是 POI 项目的 .NET 版本号. POI是一个开源的Java读写Excel.WORD等微软OLE2组件文档的项目. 使用 NPOI 你就能够在没有安装 Office ...

  2. Linux下一个php+mysql+nginx构建编译(三)

    在此之前一直是一个关键构建webserver.但一个关键的建筑环境都比较旧的.假定使用一个相对较新的环境,尤其是正式的server.您必须手动编译自己建(基于以下的结构linux centos6.5 ...

  3. POJ 2485 Highways (prim最小生成树)

    对于终于生成的最小生成树中最长边所连接的两点来说 不存在更短的边使得该两点以不论什么方式联通 对于本题来说 最小生成树中的最长边的边长就是使整个图联通的最长边的边长 由此可知仅仅要对给出城市所抽象出的 ...

  4. 下的生产环境was重新启动不同意,怎么做?

    前一段时间上线.遇到一个jndi问题,它是如何是个问题?它是在测试环境中的原始没有问题,在生产环境中,您无法连接生产数据库,然后发现问题,那是,ibm工具生成在测试环境中自己主动的连接jndi资源文件 ...

  5. java.io.FileNotFoundException: /home/hadoop/hadoop/dfs/namenode/current/VERSION (Permission denied)

    今天布置hadoop集群,尝试单独将secondarynamenode分属到一台独立的虚拟机上, 当格式化后,start-dfs.sh.namenode没启动.查看日志.报错例如以下 查看权限才发现, ...

  6. android-sdk-windows下载版

    Android SDK 4.0.3 开发和执行环境配置 近期又装了一次最新版本号的ADK环境 眼下最新版是Android SDK 4.0.3 本文的插图和文本尽管是Android2.2的 步骤都是一样 ...

  7. [LeetCode82]Remove Duplicates from Sorted List II

    题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct  ...

  8. 每天努力一点之SQL(二) count sum case when then group by

    1. select sum(CASE WHEN A.[STATUS]=0 THEN 1 ELSE 0 end) as a1,  sum(CASE A.[STATUS] WHEN 1 THEN 1 EL ...

  9. oracle db于,一个特定的数据字典pct miss其计算公式

    这篇文章是原创文章,转载请注明出处: http://blog.csdn.net/msdnchina/article/details/38766801 本文提到的数据字典.以dc_histogram_d ...

  10. 也可以看看GCD(杭州电2504)(gcd)

    也可以看看GCD Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...