A12213 | Margarite and the best present
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Little girl Margarita is a big fan of competitive programming. She especially loves problems about arrays and queries on them.
Recently, she was presented with an array $a$ of the size of $10^9$ elements that is filled as follows:
- $a_1 = -1$
- $a_2 = 2$
- $a_3 = -3$
- $a_4 = 4$
- $a_5 = -5$
- And so on ...
That is, the value of the $i$ -th element of the array $a$ is calculated using the formula $a_i = i \cdot (-1)^i$ .
She immediately came up with $q$ queries on this array. Each query is described with two numbers: $l$ and $r$ . The answer to a query is the sum of all the elements of the array at positions from $l$ to $r$ inclusive.
Margarita really wants to know the answer to each of the requests. She doesn't want to count all this manually, but unfortunately, she couldn't write the program that solves the problem either. She has turned to you — the best programmer.
Help her find the answers!
Recently, she was presented with an array $a$ of the size of $10^9$ elements that is filled as follows:
- $a_1 = -1$
- $a_2 = 2$
- $a_3 = -3$
- $a_4 = 4$
- $a_5 = -5$
- And so on ...
That is, the value of the $i$ -th element of the array $a$ is calculated using the formula $a_i = i \cdot (-1)^i$ .
She immediately came up with $q$ queries on this array. Each query is described with two numbers: $l$ and $r$ . The answer to a query is the sum of all the elements of the array at positions from $l$ to $r$ inclusive.
Margarita really wants to know the answer to each of the requests. She doesn't want to count all this manually, but unfortunately, she couldn't write the program that solves the problem either. She has turned to you — the best programmer.
Help her find the answers!
输入格式
The first line contains a single integer $q$ ( $1 \le q \le 10^3$ ) — the number of the queries.
Each of the next $q$ lines contains two integers $l$ and $r$ ( $1 \le l \le r \le 10^9$ ) — the descriptions of the queries.
Each of the next $q$ lines contains two integers $l$ and $r$ ( $1 \le l \le r \le 10^9$ ) — the descriptions of the queries.
输出格式
Print $q$ lines, each containing one number — the answer to the query.
输入输出样例
输入 #1
5 1 3 2 5 5 5 4 4 2 3
输出 #1
-2 -2 -5 4 -1
In the first query, you need to find the sum of the elements of the array from position $1$ to position $3$ . The sum is equal to $a_1 + a_2 + a_3 = -1 + 2 -3 = -2$ .
In the second query, you need to find the sum of the elements of the array from position $2$ to position $5$ . The sum is equal to $a_2 + a_3 + a_4 + a_5 = 2 -3 + 4 - 5 = -2$ .
In the third query, you need to find the sum of the elements of the array from position $5$ to position $5$ . The sum is equal to $a_5 = -5$ .
In the fourth query, you need to find the sum of the elements of the array from position $4$ to position $4$ . The sum is equal to $a_4 = 4$ .
In the fifth query, you need to find the sum of the elements of the array from position $2$ to position $3$ . The sum is equal to $a_2 + a_3 = 2 - 3 = -1$ .
In the second query, you need to find the sum of the elements of the array from position $2$ to position $5$ . The sum is equal to $a_2 + a_3 + a_4 + a_5 = 2 -3 + 4 - 5 = -2$ .
In the third query, you need to find the sum of the elements of the array from position $5$ to position $5$ . The sum is equal to $a_5 = -5$ .
In the fourth query, you need to find the sum of the elements of the array from position $4$ to position $4$ . The sum is equal to $a_4 = 4$ .
In the fifth query, you need to find the sum of the elements of the array from position $2$ to position $3$ . The sum is equal to $a_2 + a_3 = 2 - 3 = -1$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted