703 二维前缀和
// 703 二维前缀和.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
/*
http://oj.daimayuan.top/course/22/problem/894
给一个 n×m的矩阵 a11,a12,…,a1m,…,anm和 q个询问。
每次询问给出四个数 x1,y1,x2,y2,求∑i=x1~x2∑j=y1~y2a[ij]的值。
输入格式
第一行六个整数 n,m,q,A,B,C(1≤n,m≤2×103,1≤q≤106,0≤A,B,C<232)。
为了防止输入过大,ai,j和询问用下面代码生成。
输出格式
为了防止输出过大,输出所有询问答案的异或和。
样例输入
5 5 10 1 2 3
样例输出
1655687751
*/
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
const int N = 2010;
int n, m, q;
unsigned int A, B, C;
long long a[N][N];
long long pre[N][N];
inline unsigned int rng61() {
A ^= A << 16;
A ^= A >> 5;
A ^= A << 1;
unsigned int t = A;
A = B;
B = C;
C ^= t ^ A;
return C;
}
int main() {
scanf("%d%d%d%u%u%u", &n, &m, &q, &A, &B, &C);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
a[i][j] = rng61();
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
pre[i][j] = a[i][j] + pre[i - 1][j] + pre[i][j - 1] - pre[i - 1][j - 1];
}
}
long long ans = 0;
for (int i = 1; i <= q; i++) {
int x1 = rng61() % n + 1, x2 = rng61() % n + 1;
int y1 = rng61() % m + 1, y2 = rng61() % m + 1;
if (x1 > x2) swap(x1, x2);
if (y1 > y2) swap(y1, y2);
// 执行查询操作
ans ^= pre[x2][y2] - pre[x1 - 1][y2] - pre[x2][y1 - 1] + pre[x1 - 1][y1 - 1];
}
cout << ans << endl;
return 0;
}
作 者: itdef
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力

