#P4676. [BalticOI 2016 day1] Spiral

[BalticOI 2016 day1] Spiral

题目描述

A grid of size (2n+1)×(2n+1)(2n+1)\times(2n+1) has been constructed as follows. Number 11 has been placed in the center square, number 22 has been placed to the right of it, and the following numbers have been placed along the spiral counterclockwise.

Your task is to calculate answers for qq queries where the sum of numbers in an rectangular region in the grid is requested (modulo 109+710^9+7). For example, in the following grid n=2n=2 and the sum of numbers in the gray region is 7474 :

输入格式

The first input line contains two integers nn and qq : the size of the grid and the number of queries.

After this, there are qq lines, each containing four integers x1,y1,x2x_1, y_1, x_2 and y2y_2 (nx1x2n,ny1y2n-n\leq x_1\leq x_2\leq n, -n\leq y_1\leq y_2\leq n). This means that you should calculate the sum of numbers in a rectangular region with corners (x1,y1)(x_1,y_1) and (x2,y2)(x_2,y_2).

输出格式

You should output the answer for each query (modulo 109+710^9+7).

题目大意

[BalticOI 2016 Day1]螺旋

题目描述

一个矩阵的大小为 (2n+1)×(2n+1)(2n+1)\times (2n+1),我们们通过下述方法填数:数字 11 在中心,数字 22 在其右,其他数字依次按照逆时针螺旋摆放。

你的任务是对于 qq 个询问,计算出一个给定子矩阵所有数字的和对 (109+7)(10^9+7) 取余的结果。比如以下 n=2n=2 的矩阵,灰色区域的数字之和为 7474

输入格式

第一行,两个整数 nnqq,分别表示矩阵的大小和询问的个数。

接下来 qq 行,每行四个整数 x1,y1,x2x_1,y_1,x_2y2y_2 (nx1x2n,(-n \leq x_1 \leq x_2 \leq n, ny1y2n)-n \leq y_1 \leq y_2 \leq n)。这表示你需要计算一个对角为 (x1,y1)(x_1,y_1)(x2,y2)(x_2,y_2) 的子矩阵的数字之和。

输出格式

对于每个询问,输出一行表示答案(对 109+710^9+7 取余)。

由 @I_love_him52 提供翻译

2 3
0 -2 1 1
-1 0 1 0
1 2 1 2

74
9
14

提示

Subtasks

In all subtasks 1q1001\leq q\leq100.

Subtask 1 (12 points)

  • 1n10001\leq n\leq1000

Subtask 2 (15 points)

  • 1n1091\leq n\leq10^9

  • x1=x2x_1=x_2 and y1=y2y_1=y_2

Subtask 3 (17 points)

  • 1n1051\leq n\leq10^5

Subtask 4 (31 points)

  • 1n1091\leq n\leq10^9

  • x1=y1=1x_1=y_1=1

Subtask 5 (25 points)

  • 1n1091\leq n\leq10^9