The beautiful values of the palace 树状数组求二维平面矩形区域点的权值和 扫描线思想降维

这篇博客介绍了如何利用树状数组和扫描线思想来解决二维平面上矩形区域点的权值和问题。具体讨论了蛇形矩阵的权值确定方法,并给出了一种优化的计算策略,适用于处理多个查询,同时提供了样例输入和输出,帮助读者理解算法的实现过程。
摘要由CSDN通过智能技术生成

https://nanti.jisuanke.com/t/41298题目链接

Here is a square matrix of n * nn∗n, each lattice has its value (nn must be odd), and the center value is n * nn∗n. Its spiral decline along the center of the square matrix (the way of spiral decline is shown in the following figure:)

The grid in the lower left corner is (1,1) and the grid in the upper right corner is (n , n)

Now I can choose mm squares to build palaces, The beauty of each palace is equal to the digital sum of the value of the land which it is located. Such as (the land value is 123213123213,the beautiful values of the palace located on it is 1+2+3+2+1+3=121+2+3+2+1+3=12) (666666 -> 1818) (456456 ->1515)

Next, we ask pp times to the sum of the beautiful values of the palace in the matrix where the lower left grid(x_1,y_1x1​,y1​), the upper right square (x_2,y_2x2​,y2​).

Input

The first line has only one number TT.Representing TT-group of test data (T\le 5)(T≤5)

The next line is three number: n \ m \ pn m p

The mm lines follow, each line contains two integers the square of the palace (x, y )(x,y)

The pp lines follow, each line contains four integers : the lower left grid (x_1,y_1)(x1​,y1​) the upper right square (x_2,y_2)(x2​,y2​)

Output

Next, p_1+p_2...+p_Tp1​+p2​...+pT​ lines: Represent the answer in turn(n \le 10^6)(m , p \le 10^5)(n≤106)(m,p≤105)

样例输入复制

1
3 4 4
1 1
2 2
3 3
2 3
1 1 1 1
2 2 3 3
1 1 3 3
1 2 2 3

样例输出复制

5
18
23
17

题意:每个点的数值是通过一个从中心最大值开始的蛇形矩阵确定的。其中有m个点上的权值可用,对于q个询问,输出左下角为(x1,y1),右上角为(x2,y2)的矩阵区域内所有可用点的权值经过处理后的和。

思路:蛇形矩阵求权值比较简单,就不多说了。我们知道求矩形x1,y1,x2,y2的和可以用二维树状数组 ans=sum(x2,y2)+sum(x1-1,y1-1)-sum(x1-1,y2)-sum(x2,y1-1);  这个题目当然不能这样求,利用扫描线的思想,将矩形x1,y1,x2,y2分成上面那求ans的4个点,(x,y,flag)flag为-+1  ;   加上那m个有权值的点,一共有4*q+m个点,先将着m个先按y从小到大排序,如果y相等将有权值的点放前面,如果遇到有权值的点就add(x,v) ,如果遇到没权值的点,那么那个矩形ans[i]=flag*sum(x)  画图就很好理解了

#include<bits/stdc++.h>
using namespace std;
#define LL long long
typedef pair<int,int>P;
const int len=1e6+9;
const LL mod=998244353;
struct point{
	int x,y,v,flag,id;
}p[len];
int n,m,q,tot;
LL f[len];
int digit(LL x)
{
	int ans=0;
	while(x)
	{
		ans+=x%10;
		x/=10;
	}
	return ans;
}
void solve()
{
	for(int i=1;i<=n;++i)
	{
		f[i]=f[i-1]+4*(n-(i-1)*2)-4;
		if(f[i]==f[i-1])break;
	}
	int x,y;
	for(int i=1;i<=m;++i)
	{
		scanf("%d %d",&x,&y);
		int quan=min(min(x,y),min(n+1-x,n+1-y));
		LL ggb;
		if(x==n+1-quan)ggb=f[quan-1]+(n+1-quan-y+1);
		else if(y==quan)ggb=f[quan-1]+(n+1-quan-quan)+(n+1-quan-x+1);
		else if(x==quan)ggb=f[quan-1]+(n+1-quan-quan)*2+(y-quan+1);
		else if(y==n+1-quan)ggb=f[quan-1]+(n+1-quan-quan)*3+(x-quan+1);
		p[q*4+i]={x,y,digit(ggb),2,q+i};
	}
}

bool cmp(point a,point b)
{
	if(a.y==b.y)return a.flag>b.flag;
	return a.y<b.y;
}
int c[len];
int ans[len];
void add(int i,int v)
{
	for(;i<=1e6;i+=i&-i)c[i]+=v;
}
int sum(int i)
{
	int cnt=0;
	for(;i;i-=i&-i)cnt+=c[i];
	return cnt;
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		memset(c,0,sizeof(c));
		scanf("%d%d%d",&n,&m,&q);
		solve();
		for(int i=1;i<=q;++i)
		{
			int x1,y1,x2,y2;
			scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
			p[4*(i-1)+1]={x2,y2,0,1,i};
			p[4*(i-1)+4]={x1-1,y1-1,0,1,i};
			p[4*(i-1)+2]={x2,y1-1,0,-1,i};
			p[4*(i-1)+3]={x1-1,y2,0,-1,i};
			
		}
		int tot=4*q+m;
		sort(p+1,p+1+tot,cmp);
		for(int i=1;i<=q;++i)ans[i]=0;
		for(int i=1;i<=tot;++i)
		{
			if(p[i].flag==2)add(p[i].x,p[i].v);
			else ans[p[i].id]+=p[i].flag*sum(p[i].x);
		}
		for(int i=1;i<=q;++i)printf("%d\n",ans[i]);
	}
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值