The beautiful values of the palace 南京网络赛A(主席树)

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

     建立主席树,先对所有x,y坐标离散化,从x从小到大,y从小到大排序。然后进行挨个插入主席树,当x变换的时候,记录状态T,维护这个(1-x)里面每个(1-y)区间的值和。然后对于每个询问,找到上x状态,下x转态,查询主席树在(y1,y2)区间内权值和就好了。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int M = 300010;
ll getNUM(int n, int x, int y){
	ll r = 0;
	if (x <= y & x + y <= n + 1){
		r = x;
		return  4LL * (r - 1)*n - 4 * (r - 1)*(r - 1) + 1 + y - r;
	}
	if (x <= y & x + y >= n + 1){
		r = n - y + 1;
		return 4LL * (r - 1)*n - 4 * (r - 1)*(r - 1) + 1 + n - 2 * r + 1 + x - r;
	}
	if (x >= y & x + y >= n + 1){
		r = n - x + 1;
		return 4LL * (r - 1)*n - 4 * (r - 1)*(r - 1) + 1 + 3 * n - 6 * r + 3 - y + r;
	}
	if (x >= y & x + y <= n + 1){
		r = y;
		return 4LL * (r - 1)*n - 4 * (r - 1)*(r - 1) + 1 + 4 * n - 8 * r + 4 - x + r;
	}
}
inline bool sc(int &num){
	char in; bool IsN = false;
	in = getchar();
	if (in == EOF) return false;
	while (in != '-' && (in<'0' || in>'9')) in = getchar();
	if (in == '-') { IsN = true; num = 0; }
	else num = in - '0';
	while (in = getchar(), in >= '0'&&in <= '9') {
		num *= 10, num += in - '0';
	}
	if (IsN) num = -num;
	return true;
}
int T[M * 2], lson[M * 20], rson[M * 20], c[M * 20], tot = 0;
int build(int l, int r) {
	int root = tot++;
	c[root] = 0;
	if (l != r) {
		int mid = (l + r) >> 1;
		lson[root] = build(l, mid);
		rson[root] = build(mid + 1, r);
	}
	return root;
}
int update(int root, int pos, int val,int n) {
	int newroot = tot++, tmp = newroot;
	c[newroot] = c[root] + val;
	int l = 1, r = n;
	while (l < r) {
		int mid = (l + r) >> 1;
		if (pos <= mid) {
			lson[newroot] = tot++; rson[newroot] = rson[root];
			newroot = lson[newroot]; root = lson[root];
			r = mid;
		}
		else {
			rson[newroot] = tot++; lson[newroot] = lson[root];
			newroot = rson[newroot]; root = rson[root];
			l = mid + 1;
		}
		c[newroot] = c[root] + val;
	}
	return tmp;
}
int query(int left_root, int right_root, int l, int r, int L,int R) {
	if (L <= l && r <= R) {
		return c[right_root] - c[left_root];
	}
	if (l == r)return 0;
	int mid = (l + r) >> 1;
	int res = 0;
	if (L <= mid)
		res += query(lson[left_root], lson[right_root], l, mid, L, R);
	if (R > mid)
		res += query(rson[left_root], rson[right_root], mid + 1, r, L, R);
	return res;
}
struct fuck {
	int x, y, num;
}da[M];
bool cmp(fuck a, fuck b) {
	if (a.x != b.x)return a.x < b.x;
	else return a.y < b.y;
}
int qu[M][4];
int idx[2 * M], idy[2 * M], cntx, cnty;

int main(){
	int te; sc(te);
	while (te--) {
		int n, m, k; cntx = 1; cnty = 1; tot = 0;
		sc(n);sc(m);sc(k);
		for (int i = 1; i <= m; i++) {
			sc(da[i].x); sc(da[i].y);
			int x = n - da[i].x + 1;
			int y = n - da[i].y + 1;
			ll t = getNUM(n, x, y);
			da[i].num = 0;
			while (t) {
				da[i].num += t % 10;
				t /= 10;
			}
			idx[cntx++] = da[i].x;
			idy[cnty++] = da[i].y;
		}
		for (int i = 1; i <= k; i++) {
			sc(qu[i][0]); sc(qu[i][1]); sc(qu[i][2]); sc(qu[i][3]);
			idy[cnty++] = qu[i][1];	idy[cnty++] = qu[i][3];
		}
		sort(da + 1, da + 1 + m, cmp);
		sort(idx + 1, idx + cntx);
		sort(idy + 1, idy + cnty);
		cntx = unique(idx + 1, idx + cntx) - idx;
		cnty = unique(idy + 1, idy + cnty) - idy;

		int la = build(1, cnty); T[0] = la;
		for (int i = 1; i <= m; i++) {
			int tmpy = lower_bound(idy + 1, idy + cnty, da[i].y) - idy;
			la = update(la, tmpy, da[i].num, cnty);
			if (i == m || da[i].x != da[i + 1].x) {
				int tmpx = lower_bound(idx + 1, idx + cntx, da[i].x) - idx;
				T[tmpx] = la;
			}
		}
		for (int i = 1; i <= k; i++) {
			int li = lower_bound(idy + 1, idy + cnty, qu[i][1]) - idy;
			int ri = lower_bound(idy + 1, idy + cnty, qu[i][3]) - idy;
			int down = lower_bound(idx + 1, idx + cntx, qu[i][0]) - idx;
			int up = upper_bound(idx + 1, idx + cntx, qu[i][2]) - idx - 1;
			if (up < down || ri < li) {
				printf("0\n");
				continue;
			}
			int res = query(T[down - 1], T[up], 1, cnty, li, ri);
			printf("%d\n", res);
		}
	}
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值