The beautiful values of the palace

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 the upper right square

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 )
The pp lines follow, each line contains four integers : the lower left grid
the upper right square

样例输入 复制
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

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<set>
#include<math.h>
#include<queue>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fod(i,a,b) for(int i=a;i>=b;i--)
#define me0(a) memset(a,0,sizeof(a))
#define me1(a) memset(a,-1,sizeof(a))
#define op freopen("in.txt", "r", stdin)
#define ls root<<1
#define rs root<<1|1
using namespace std;
const int mod = 1e9 + 7;
typedef long long LL;
const LL INF = 0x3f3f3f3f;
void read(LL& val) { int x = 0; int bz = 1; char c; for (c = getchar(); (c<'0' || c>'9') && c != '-'; c = getchar()); if (c == '-') { bz = -1; c = getchar(); }for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - 48; val = x * bz; }

typedef long long LL;
const int maxn = 2e6 + 10;
LL n, m, p;

struct node {//查询
	node(LL a = 0, LL b = 0, LL c = 0, LL d = 0) {
		x = a; y = b; id = c; flag = d;
	}
	LL x, y, id, flag;
	bool operator<(node b) {
		return y < b.y;//从下往上计算
	}
}qu[maxn << 2];

struct node2 {//点
	node2(LL a = 0, LL b = 0, LL c = 0) {
		x = a; y = b; val = c;
	}
	LL x, y;
	LL val;
	bool operator<(node2 b) {
		return y < b.y;//从下往上编号
	}
}po[maxn];

LL f1[maxn<<1];//x轴上比x小的数字
LL lowbit(LL x) { return x & (-x); }
void add1(LL x, LL val) {//
	while (x < maxn) {
		f1[x] += val;
		x += lowbit(x);
	}
}

LL query1(LL x) {
	LL res = 0;
	while (x) {
		res += f1[x];
		x -= lowbit(x);
	}
	return res;
}

void insert(LL x, LL y, LL val) {
	if (x == 0) return;
	add1(x, val);
}

LL ans[maxn];
LL tot = 0;

void solve() {
	int pos = 1;
	fo(i, 1, tot) {
		while (pos <= m && po[pos].y <= qu[i].y) {
			insert(po[pos].x, po[pos].y, po[pos].val);
			pos++;
		}
		ans[qu[i].id] += query1(qu[i].x) * qu[i].flag;
	}
}

LL ind(LL x, LL y, LL n){
	LL qs = n / 2, q = min(n - y + 1, min(n - x + 1, min(x, y))) - 1;
	if (x == qs + 1 && y == qs + 1)	return n * n;
	LL ans = 1ll * q * (8 * qs + 8 * (qs - q + 1)) / 2;
	if (n - x == q)	ans += n - q - y + 1;
	else if (y - 1 == q)	ans += n - 2 * q + 1 + n - q - 1 - x;
	else if (x - 1 == q)	ans += n - 2 * q + 1 + n - 2 * q - 2 + y - q - 1;
	else ans += n - 2 * q + 1 + n - 2 * q - 2 + n - 2 * q - 1 + x - q - 1;
	return ans;
}

LL cc(LL x) {
	LL sum = 0;
	while (x) {
		sum += x % 10;
		x /= 10;
	}
	return sum;
}

int main() {
	LL t;
	read(t);
	while (t--) {
		read(n); read(m); read(p);
		//init
		fo(i, 0, n + n) f1[i] = 0;
		fo(i, 0, p) ans[i] = 0;

		LL x, y;
		fo(i, 1, m) {
			read(x); read(y);
			LL val = ind(x, y, n);
			val = cc(val);
			po[i] = node2(x, y, val);
		}

		sort(po + 1, po + 1 + m);
		LL x1, y1, x2, y2;
		tot = 0;
		fo(i, 1, p) {
			read(x1); read(y1); read(x2); read(y2);
			qu[++tot] = node(x1 - 1, y1 - 1, i, 1);
			qu[++tot] = node(x1 - 1, y2, i, -1);
			qu[++tot] = node(x2, y1 - 1, i, -1);
			qu[++tot] = node(x2, y2, i, 1);
		}
		sort(qu + 1, qu + 1 + tot);
		solve();
		fo(i, 1, p) {
			printf("%lld\n", max((LL)0, ans[i]));
		}
	}
	return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值