SSLOJ 1467.U【二维前缀和】


题意:

给出一个 n ∗ n n*n nn的矩阵,每次操作会对一个下三角形中的每一个数增加 s s s,问最终矩阵的所有元素的异或和


分析:

因为是要维护一个下三角形里的每个数,所以我们可以用前缀和的思路来做
分别建出两个前缀和,一个用来维护一列上的前缀和,另一个用来维护斜线上的前缀和,最后统计答案两者相加就是了


代码:

#pragma GCC optimize(2)
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<cmath>
#include<vector>
#define LL long long 
using namespace std;
inline LL read() {
    
    
    LL d=0,f=1;char s=getchar();
    while(s<'0'||s>'9'){
    
    if(s=='-')f=-1;s=getchar();}
    while(s>='0'&&s<='9'){
    
    d=d*10+s-'0';s=getchar();}
    return d*f;
}
LL t1[2100][2100],t2[2100][2100],ans=0;
LL t[2100][2100];
int main()
{
    
    
	LL n=read(),q=read();
	for(LL i=1;i<=q;i++)
	{
    
    
		LL r=read(),c=read(),l=read(),s=read();
		t1[r][c]+=s;t1[r+l][c+l]-=s;
		t2[r+l][c]-=s;t2[r+l][c+l]+=s;
	}
	for(LL i=1;i<=n;i++)
	  for(LL j=1;j<=n;j++)
	  {
    
    
	  	t1[i][j]+=t1[i-1][j-1];
	  	t2[i][j]+=t2[i][j-1];
	  	t[i][j]+=t[i-1][j]+t1[i][j]+t2[i][j];
	  	ans^=t[i][j];
	  }
	cout<<ans;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_35786326/article/details/108042452