版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

仓库源文站点原文


title: "题解 - [BalticOI 2016 day1] [Luogu P4676] [LOJ 2782] Spiral" categories:


题目链接

<!-- more -->

原始题面

题目描述

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$ :

输入格式

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

样例输入 #1

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

样例输出 #1

74
9
14

提示

In all subtasks $1\leq q\leq100$.

Subtask 1 (12 points)

Subtask 2 (15 points)

Subtask 3 (17 points)

Subtask 4 (31 points)

Subtask 5 (25 points)

题意简述

给一个螺旋矩阵, 每次询问给出一个子矩阵, 问子矩阵中的元素和

解题思路

大力分讨

复杂度

$O(q)$

代码参考

<details open> <summary><font color='orange'>Show code</font></summary> {% icodeweb blog lang:cpp baltoi2016-3/std.cpp %} </details>