[BalticOI 2016 day1] Spiral

题意翻译

[BalticOI 2016 Day1]螺旋 ## 题目描述 一个矩阵的大小为 $(2n+1)\times (2n+1)$,我们们通过下述方法填数:数字 $1$ 在中心,数字 $2$ 在其右,其他数字依次按照逆时针螺旋摆放。 你的任务是对于 $q$ 个询问,计算出一个给定子矩阵所有数字的和对 $(10^9+7)$ 取余的结果。比如以下 $n=2$ 的矩阵,灰色区域的数字之和为 $74$: ![](https://i.loli.net/2018/08/11/5b6e3ead24175.png) ## 输入格式 第一行,两个整数 $n$ 和 $q$,分别表示矩阵的大小和询问的个数。 接下来 $q$ 行,每行四个整数 $x_1,y_1,x_2$ 和 $y_2$ $(-n \leq x_1 \leq x_2 \leq n,$ $-n \leq y_1 \leq y_2 \leq n)$。这表示你需要计算一个对角为 $(x_1,y_1)$ 和 $(x_2,y_2)$ 的子矩阵的数字之和。 ## 输出格式 对于每个询问,输出一行表示答案(对 $10^9+7$ 取余)。 由 @I_love_him52 提供翻译

题目描述

A grid of size $(2n+1)\times(2n+1)$ has been constructed as follows. Number $1$ has been placed in the center square, number $2$ 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 $q$ queries where the sum of numbers in an rectangular region in the grid is requested (modulo $10^9+7$). For example, in the following grid $n=2$ and the sum of numbers in the gray region is $74$ : ![](https://cdn.luogu.com.cn/upload/pic/20871.png)

输入输出格式

输入格式


The first input line contains two integers $n$ and $q$ : the size of the grid and the number of queries. After this, there are $q$ lines, each containing four integers $x_1, y_1, x_2$ and $y_2$ ($-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 $(x_1,y_1)$ and $(x_2,y_2)$.

输出格式


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

输入输出样例

输入样例 #1

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

输出样例 #1

74
9
14

说明

## Subtasks In all subtasks $1\leq q\leq100$. ### Subtask 1 (12 points) - $1\leq n\leq1000$ ### Subtask 2 (15 points) - $1\leq n\leq10^9$ - $x_1=x_2$ and $y_1=y_2$ ### Subtask 3 (17 points) - $1\leq n\leq10^5$ ### Subtask 4 (31 points) - $1\leq n\leq10^9$ - $x_1=y_1=1$ ### Subtask 5 (25 points) - $1\leq n\leq10^9$