Luogu 4514 上帝造题的七分钟
二维差分+树状数组。
定义差分数组$d_{i, j} = a_{i, j} + a_{i - 1, j - 1} - a_{i, j - 1} - a_{i - 1, j}$,有$a_{i, j} = \sum_{x = 1}^{i}\sum_{y = 1}^{j}d_{i, j}$。
我们要求$sum(n, m) = \sum_{i = 1}^{n}\sum_{j = 1}^{m}a_{i, j} $,
代入$a_{i, j}$,得$sum(n, m) = \sum_{i = 1}^{n}\sum_{j = 1}^{m}\sum_{x = 1}^{i}\sum_{y = 1}^{j}d_{x, y}$。
列一下发现$d_{x, y}$出现了$(n - x + 1) * (m - y + 1)$次。
那么$sum(n, m) = \sum_{i = 1}^{n}\sum_{j = 1}^{m}d_{i, j} * (n - i + 1) * (m - j + 1)$。
把$(n + 1),(m + 1),i, j$看作四项展开,得到$(n + 1) * (m + 1)\sum_{i = 1}^{n}\sum_{j = 1}^{m}d_{i, j} + \sum_{i = 1}^{n}\sum_{j = 1}^{m}d_{i, j} * i * j - (m + 1) \sum_{i = 1}^{n}\sum_{j = 1}^{m}d_{i, j} * i - (n + 1)\sum_{i = 1}^{n}\sum_{j = 1}^{m}d_{i, j} * j$。
两个$\sum$可以用一个二维树状数组维护,这样子维护四个树状数组即可(修改好长)。
时间复杂度$O(qlognlogm)$。
另外,longlong在Luogu上会MLE最后两个点,要用int
Code:
#include <cstdio>
#include <cstring>
using namespace std;
typedef int ll; const int N = ; int n, m; template <typename T>
inline void read(T &X) {
X = ; char ch = ; T op = ;
for(; ch > ''|| ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} struct BinaryIndexTree {
ll arr[N][N]; #define lowbit(p) (p & (-p)) inline void modify(int x, int y, ll v) {
for(int i = x; i <= n; i += lowbit(i))
for(int j = y; j <= m; j += lowbit(j))
arr[i][j] += v;
} inline ll query(int x, int y) {
ll res = 0LL;
for(int i = x; i > ; i -= lowbit(i))
for(int j = y; j > ; j -= lowbit(j))
res += arr[i][j];
return res;
} } sum, mulij, muli, mulj; inline int min(int x, int y) {
return x > y ? y : x;
} inline int max(int x, int y) {
return x > y ? x : y;
} inline ll qSum(int x, int y) {
return 1LL * (x + ) * (y + ) * sum.query(x, y) + 1LL * mulij.query(x, y)
- 1LL * (y + ) * muli.query(x, y) - 1LL * (x + ) * mulj.query(x, y);
} int main() {
// freopen("Sample.txt", "r", stdin); char op = getchar();
read(n), read(m);
for(; ; ) {
for(op = getchar(); op != 'L' && op != 'k' && op >= ; op = getchar());
if(op < ) break; if(op == 'L') {
int a, b, c, d; ll v;
read(a), read(b), read(c), read(d), read(v);
int lx = min(a, c), ly = min(b, d), rx = max(a, c), ry = max(b, d);
sum.modify(lx, ly, v);
sum.modify(rx + , ry + , v);
sum.modify(lx, ry + , -v);
sum.modify(rx + , ly, -v); muli.modify(lx, ly, v * lx);
muli.modify(rx + , ry + , v * (rx + ));
muli.modify(lx, ry + , -v * lx);
muli.modify(rx + , ly, -v * (rx + )); mulj.modify(lx, ly, v * ly);
mulj.modify(rx + , ry + , v * (ry + ));
mulj.modify(lx, ry + , -v * (ry + ));
mulj.modify(rx + , ly, -v * ly); mulij.modify(lx, ly, v * lx * ly);
mulij.modify(rx + , ry + , v * (rx + ) * (ry + ));
mulij.modify(lx, ry + , -v * (ry + ) * lx);
mulij.modify(rx + , ly, -v * (rx + ) * ly);
} else {
int a, b, c, d;
read(a), read(b), read(c), read(d);
int lx = min(a, c), ly = min(b, d), rx = max(a, c), ry = max(b, d);
printf("%d\n", qSum(rx, ry) + qSum(lx - , ly - ) - qSum(rx, ly - ) - qSum(lx - , ry));
}
}
return ;
}
Luogu 4514 上帝造题的七分钟的更多相关文章
- [luogu] P4514 上帝造题的七分钟 (树状数组,二维差分)
P4514 上帝造题的七分钟 题目背景 裸体就意味着身体. 题目描述 "第一分钟,X说,要有矩阵,于是便有了一个里面写满了0的n×m矩阵. 第二分钟,L说,要能修改,于是便有了将左上角为(a ...
- 【题解】 Luogu P4145 上帝造题的七分钟2 / 花神游历各国
原题传送门 这道题实际和GSS4是一样的,只是输入方式有点区别 GSS4传送门 这道题暴力就能过qaq(这里暴力指线段树) 数据比较水 开方修改在线段树中枚举叶节点sqrt 查询区间和线段树基本操作 ...
- luogu P4145 上帝造题的七分钟2 / 花神游历各国 维护区间和&&区间开根号
因为开根号能使数字减小得非常快 所以开不了几次(6次?)很大的数就会变成1..... 所以我们可以维护区间最大值,若最大值>1,则继续递归子树,暴力修改叶节点,否则直接return (好像也可以 ...
- [Luogu P4145] 上帝造题的七分钟2 / 花神游历各国
题目链接 题目简要:我们需要一个能支持区间内每一个数开方以及区间求和的数据结构. 解题思路:说道区间修改区间查询,第一个想到的当然就是分块线段树.数据范围要用long long.本来我是看到区间这两个 ...
- GSS4 - Can you answer these queries IV || luogu4145上帝造题的七分钟2 / 花神游历各国 (线段树)
GSS4 - Can you answer these queries IV || luogu4145上帝造题的七分钟2 / 花神游历各国 GSS4 - Can you answer these qu ...
- 【BZOJ3211&3038】花神游历各国&上帝造题的七分钟2(CodeVS)
Description Input Output 每次x=1时,每行一个整数,表示这次旅行的开心度 Sample Input 4 1 100 5 5 5 1 1 2 2 1 2 1 1 2 2 ...
- C++之路进阶——codevs2492(上帝造题的七分钟 2)
2492 上帝造题的七分钟 2 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 大师 Master 题目描述 Description XLk觉得<上帝造题的七分钟&g ...
- 【BZOJ】3038: 上帝造题的七分钟2(线段树+暴力)
http://www.lydsy.com:808/JudgeOnline/problem.php?id=3038 这题我就有得吐槽了,先是线段树更新写错,然后不知哪没pushup导致te,精度问题sq ...
- BZOJ3132: 上帝造题的七分钟
3132: 上帝造题的七分钟 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 483 Solved: 222[Submit][Status] Desc ...
随机推荐
- 常用集合ArrayList浅度解析
首先,先看一下java中对ArrayList的定义代码: public class ArrayList<E> extends AbstractList<E> implement ...
- "Cannot declare member function ...to have static linkage"错误
英文解释: if you declare a method to be static in your .cc file. The reason is that static means somethi ...
- 打印iphone支持的所有字体
//打印iphone支持的所有字体 NSArray *familyNames = [UIFont familyNames]; for(NSString *familyName in familyNam ...
- UVA - 1343 The Rotation Game (BFS/IDA*)
题目链接 紫书例题. 首先附上我第一次bfs+剪枝TLE的版本: #include<bits/stdc++.h> using namespace std; typedef long lon ...
- docker容器与宿主机之间内容拷贝
来自:http://blog.csdn.net/yangzhenping/article/details/43667785 常用的方式有3种: 从容器内拷贝文件到主机上 docker cp <c ...
- HIVE-几道经典的hive题目
建表相关语句在此,具体的数据自己制作吧 create table student(Sno int,Sname string,Sex string,Sage int,Sdept string)row f ...
- webrtc自带client的音频引擎创建代码走读
src\webrtc\examples\peerconnection\client\conductor.cc1.bool Conductor::InitializePeerConnection()1. ...
- CentOS7安装wget 及配置
yum -y install wget yum -y install setup yum -y install perl
- HBase之八--(1):HBase二级索引的设计(案例讲解)
摘要 最近做的一个项目涉及到了多条件的组合查询,数据存储用的是HBase,恰恰HBase对于这种场景的查询特别不给力,一般HBase的查询都是通过RowKey(要把多条件组合查询的字段都拼接在RowK ...
- 【OpenCV】基于图像处理和模式识别的火灾检测方法
学期末一直忙考试,大作业,很久没来CSDN耕耘了... 虽然考试都结束了,手头还是累积了不少活儿要补,不多写了,晒个小项目,之前一直做的,后来当做模式识别课程的大作业交了. 大体框架如下: 还是之前的 ...