CF1080B.Margarite and the best present
入门
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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 109 elements that is filled as follows:
- a1=−1
- a2=2
- a3=−3
- a4=4
- a5=−5
- And so on ...
That is, the value of the i-th element of the array a is calculated using the formula ai=i⋅(−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!
小女孩玛格丽塔是竞技编程的忠实粉丝。她尤其喜欢关于数组及其查询的问题。
最近,她得到了一个大小为 109 的数组 a,该数组按如下方式填充:
- a1=−1
- a2=2
- a3=−3
- a4=4
- a5=−5
- 依此类推……
即,数组 a 的第 i 个元素的值由公式 ai=i⋅(−1)i 计算得出。
她立刻针对该数组提出了 q 个查询。每个查询由两个数 l 和 r 描述。查询的答案为数组中从位置 l 到 r(含端点)的所有元素之和。
玛格丽塔非常想知道每个查询的答案。她不想手动计算所有结果,但遗憾的是,她自己也无法编写解决该问题的程序。于是她向你——最优秀的程序员——寻求帮助。
请帮她求出所有查询的答案!
输入格式
The first line contains a single integer q (1≤q≤103) — the number of the queries.
Each of the next q lines contains two integers l and r (1≤l≤r≤109) — the descriptions of the queries.
第一行包含一个整数 q(1≤q≤103)—— 表示查询的次数。
接下来的 q 行,每行包含两个整数 l 和 r(1≤l≤r≤109)—— 表示每次查询的描述。
输出格式
Print q lines, each containing one number — the answer to the query.
输出 q 行,每行包含一个数字——对应查询的答案。
输入输出样例
输入#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 a1+a2+a3=−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 a2+a3+a4+a5=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 a5=−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 a4=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 a2+a3=2−3=−1.
在第一次查询中,你需要计算数组从第 1 个位置到第 3 个位置的所有元素之和。该和为 a1+a2+a3=−1+2−3=−2。
在第二次查询中,你需要计算数组从第 2 个位置到第 5 个位置的所有元素之和。该和为 a2+a3+a4+a5=2−3+4−5=−2。
在第三次查询中,你需要计算数组从第 5 个位置到第 5 个位置的所有元素之和。该和为 a5=−5。
在第四次查询中,你需要计算数组从第 4 个位置到第 4 个位置的所有元素之和。该和为 a4=4。
在第五次查询中,你需要计算数组从第 2 个位置到第 3 个位置的所有元素之和。该和为 a2+a3=2−3=−1。
输入解题思路,AI测评打分。不知道怎么写?