A8708 | Fence
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
John Doe has a crooked fence, consisting of $n$ rectangular planks, lined up from the left to the right: the plank that goes $i$ -th $(1<=i<=n)$ (from left to right) has width 1 and height $h_{i}$ . We will assume that the plank that goes $i$ -th $(1<=i<=n)$ (from left to right) has index $i$ .
A piece of the fence from $l$ to $r$ $(1<=l<=r<=n)$ is a sequence of planks of wood with indices from $l$ to $r$ inclusive, that is, planks with indices $l,l+1,...,r$ . The width of the piece of the fence from $l$ to $r$ is value $r-l+1$ .
Two pieces of the fence from $l_{1}$ to $r_{1}$ and from $l_{2}$ to $r_{2}$ are called matching, if the following conditions hold:
- the pieces do not intersect, that is, there isn't a single plank, such that it occurs in both pieces of the fence;
- the pieces are of the same width;
- for all $i$ $(0<=i<=r_{1}-l_{1})$ the following condition holds: $h_{l1+i}+h_{l2+i}=h_{l1}+h_{l2}$ .
John chose a few pieces of the fence and now wants to know how many distinct matching pieces are for each of them. Two pieces of the fence are distinct if there is a plank, which belongs to one of them and does not belong to the other one.
A piece of the fence from $l$ to $r$ $(1<=l<=r<=n)$ is a sequence of planks of wood with indices from $l$ to $r$ inclusive, that is, planks with indices $l,l+1,...,r$ . The width of the piece of the fence from $l$ to $r$ is value $r-l+1$ .
Two pieces of the fence from $l_{1}$ to $r_{1}$ and from $l_{2}$ to $r_{2}$ are called matching, if the following conditions hold:
- the pieces do not intersect, that is, there isn't a single plank, such that it occurs in both pieces of the fence;
- the pieces are of the same width;
- for all $i$ $(0<=i<=r_{1}-l_{1})$ the following condition holds: $h_{l1+i}+h_{l2+i}=h_{l1}+h_{l2}$ .
John chose a few pieces of the fence and now wants to know how many distinct matching pieces are for each of them. Two pieces of the fence are distinct if there is a plank, which belongs to one of them and does not belong to the other one.
输入格式
The first line contains integer $n$ ( $1<=n<=10^{5}$ ) — the number of wood planks in the fence. The second line contains $n$ space-separated integers $h_{1},h_{2},...,h_{n}$ ( $1<=h_{i}<=10^{9}$ ) — the heights of fence planks.
The third line contains integer $q$ ( $1<=q<=10^{5}$ ) — the number of queries. Next $q$ lines contain two space-separated integers $l_{i}$ and $r_{i}$ ( $1<=l_{i}<=r_{i}<=n$ ) — the boundaries of the $i$ -th piece of the fence.
The third line contains integer $q$ ( $1<=q<=10^{5}$ ) — the number of queries. Next $q$ lines contain two space-separated integers $l_{i}$ and $r_{i}$ ( $1<=l_{i}<=r_{i}<=n$ ) — the boundaries of the $i$ -th piece of the fence.
输出格式
For each query on a single line print a single integer — the number of pieces of the fence that match the given one. Print the answers to the queries in the order, in which the queries are given in the input.
输入输出样例
输入 #1
10 1 2 2 1 100 99 99 100 100 100 6 1 4 1 2 3 4 1 5 9 10 10 10
输出 #1
1 2 2 0 2 9
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted