A11260 | Boredom
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Ilya is sitting in a waiting area of Metropolis airport and is bored of looking at time table that shows again and again that his plane is delayed. So he took out a sheet of paper and decided to solve some problems.
First Ilya has drawn a grid of size $n×n$ and marked $n$ squares on it, such that no two marked squares share the same row or the same column. He calls a rectangle on a grid with sides parallel to grid sides beautiful if exactly two of its corner squares are marked. There are exactly $n·(n-1)/2$ beautiful rectangles.
Ilya has chosen $q$ query rectangles on a grid with sides parallel to grid sides (not necessarily beautiful ones), and for each of those rectangles he wants to find its beauty degree. Beauty degree of a rectangle is the number of beautiful rectangles that share at least one square with the given one.
Now Ilya thinks that he might not have enough time to solve the problem till the departure of his flight. You are given the description of marked cells and the query rectangles, help Ilya find the beauty degree of each of the query rectangles.
First Ilya has drawn a grid of size $n×n$ and marked $n$ squares on it, such that no two marked squares share the same row or the same column. He calls a rectangle on a grid with sides parallel to grid sides beautiful if exactly two of its corner squares are marked. There are exactly $n·(n-1)/2$ beautiful rectangles.
Ilya has chosen $q$ query rectangles on a grid with sides parallel to grid sides (not necessarily beautiful ones), and for each of those rectangles he wants to find its beauty degree. Beauty degree of a rectangle is the number of beautiful rectangles that share at least one square with the given one.
Now Ilya thinks that he might not have enough time to solve the problem till the departure of his flight. You are given the description of marked cells and the query rectangles, help Ilya find the beauty degree of each of the query rectangles.
输入格式
The first line of input contains two integers $n$ and $q$ ( $2<=n<=200000$ , $1<=q<=200000$ ) — the size of the grid and the number of query rectangles.
The second line contains $n$ integers $p_{1},p_{2},...,p_{n}$ , separated by spaces ( $1<=p_{i}<=n$ , all $p_{i}$ are different), they specify grid squares marked by Ilya: in column $i$ he has marked a square at row $p_{i}$ , rows are numbered from $1$ to $n$ , bottom to top, columns are numbered from $1$ to $n$ , left to right.
The following $q$ lines describe query rectangles. Each rectangle is described by four integers: $l,d,r,u$ ( $1<=l<=r<=n$ , $1<=d<=u<=n$ ), here $l$ and $r$ are the leftmost and the rightmost columns of the rectangle, $d$ and $u$ the bottommost and the topmost rows of the rectangle.
The second line contains $n$ integers $p_{1},p_{2},...,p_{n}$ , separated by spaces ( $1<=p_{i}<=n$ , all $p_{i}$ are different), they specify grid squares marked by Ilya: in column $i$ he has marked a square at row $p_{i}$ , rows are numbered from $1$ to $n$ , bottom to top, columns are numbered from $1$ to $n$ , left to right.
The following $q$ lines describe query rectangles. Each rectangle is described by four integers: $l,d,r,u$ ( $1<=l<=r<=n$ , $1<=d<=u<=n$ ), here $l$ and $r$ are the leftmost and the rightmost columns of the rectangle, $d$ and $u$ the bottommost and the topmost rows of the rectangle.
输出格式
For each query rectangle output its beauty degree on a separate line.
输入输出样例
输入 #1
2 3 1 2 1 1 1 1 1 1 1 2 1 1 2 2
输出 #1
1 1 1
输入 #2
4 2 1 3 2 4 4 1 4 4 1 1 2 3
输出 #2
3 5
The first sample test has one beautiful rectangle that occupies the whole grid, therefore the answer to any query is 1.
In the second sample test the first query rectangle intersects 3 beautiful rectangles, as shown on the picture below:

There are 5 beautiful rectangles that intersect the second query rectangle, as shown on the following picture:

In the second sample test the first query rectangle intersects 3 beautiful rectangles, as shown on the picture below:

There are 5 beautiful rectangles that intersect the second query rectangle, as shown on the following picture:

C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted