The Preliminary Contest for ICPC Asia Nanjing 2019 南京网络赛 A题 The beautiful values of the palace

题目链接:

点击前往

题目:

Here is a square matrix of n∗nn * nnn, each lattice has its value (nnn must be odd), and the center value is n∗nn * nnn. 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 mmm 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 123213123213123213,the beautiful values of the palace located on it is 1+2+3+2+1+3=121+2+3+2+1+3=121+2+3+2+1+3=12) (666666666 -> 181818) (456456456 ->151515)

Next, we ask ppp times to the sum of the beautiful values of the palace in the matrix where the lower left grid(x1,y1x_1,y_1x1,y1), the upper right square (x2,y2x_2,y_2x2,y2).

Input

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

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

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

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

Output

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


题目大意:

有一个 n ∗ n ( n ≤ 1 0 6 ) n*n (n \leq 10^6) nn(n106)的螺旋矩阵,图像见题目。然后有 m m m个宫殿,每一个宫殿坐落在一个格子里,该宫殿的美丽值为所在格子的各个数位的和。比如一个宫殿位于值为123的格子,那么他的美丽值就是 1 + 2 + 3 = 6 1+2+3 = 6 1+2+3=6。然后给你 q q q个询问,每个询问包含一个矩形的左下角和右上角,要求输出在这个范围内的宫殿的美丽值的和。

解题思路

注意到 n n n很大,所以我们没有办法模拟的去得到螺旋矩阵每一个位置的值,但很显然这个矩阵具有很强的规律性,所以我们可以尝试用 O ( 1 ) O(1) O(1)的时间去计算。
注意:此题左下角的坐标是 ( 1 , 1 ) (1,1) (1,1),右上角的坐标是 ( n , n ) (n,n) (n,n) ( x , y ) (x,y) (x,y)代表位于从左往后数第x列,从下往上数第y行的元素。一定要注意这里!!!!
然后我们就可以开始计算了,因为元素是一圈一圈填充进去的,所以,我们可以先计算出来所求位置元素位于第几圈(从外向内数,第一圈的边长是 n n n,第二圈的边长是 n − 2 n-2 n2,以此类推…)上(假设是 T T T),然后先计算出前 T − 1 T-1 T1圈的总的数字数量,然后再计算出第T圈中小于等于所求位置数字的数字数量,就可以得到对应位置的值了。具体情况如下:
首先 T T T圈的边长为 n − 2 ( T − 1 ) = n + 2 T + 2 n-2(T-1) = n+2T+2 n2(T1)=n+2T+2,当前点 ( x , y ) (x,y) (x,y)位于从下往上数第 y y y行,从上往下数第 n − y + 1 n-y+1 ny+1行,从左往右数第 x x x列,从右往左数第 n − x + 1 n-x+1 nx+1列。每一圈的数字可以看作由四个长度为 当 前 边 长 − 1 当前边长-1 1的数字段组成。那么,我们可得 ( x , y ) (x,y) (x,y)位置的数字的值(即 v a l ( x , y ) val(x,y) val(x,y))为:
首先,设该元素所在的圈为第 T T T圈,可得 T = m i n

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值