原题链接:
http://codeforces.com/contest/1080/problem/B
B. Margarite and the best present
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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!

Input
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.

Output
Print q lines, each containing one number — the answer to the query.

Example
input
5
1 3
2 5
5 5
4 4
2 3
output
-2
-2
-5
4
-1
Note
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.
题意:
数列ai满足ai=i*(-1)^i
有一个q个问题,每一个问题包含一个左边界和右边界,输出区间内的和。
题解:
这是一道关于前缀和的题目,可以在计算的时候通过规律计算每一个点的前缀和,最后相减即可。
附上AC代码:

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{int q,l,r;scanf("%d",&q);while(q--){int suml=0,sumr=0;scanf("%d%d",&l,&r);if(l%2==0)//计算前缀和{suml=l/2-l;}else{suml=l/2;}if(r%2==0)//计算前缀和{sumr=r/2;}else{sumr=r/2-r;}printf("%d\n",sumr-suml);}return 0;
}

欢迎评论!

CodeForces Round#524B - Margarite and the best present (前缀和)相关推荐

  1. Educational Codeforces Round 9 B. Alice, Bob, Two Teams 前缀和

    B. Alice, Bob, Two Teams 题目连接: http://www.codeforces.com/contest/632/problem/B Description Alice and ...

  2. Codeforces Round #626 (Div. 2) D. Present 按位贡献 + 快排新姿势

    传送门 文章目录 题意: 思路: 题意: 给你一个长度为nnn的序列aaa,让你计算 n≤4e5,a≤1e7n\le 4e5,a\le 1e7n≤4e5,a≤1e7 思路: 首先这个式子是n2n^2n ...

  3. Codeforces Round #262 (Div. 2) 460C. Present(二分)

    题目链接:http://codeforces.com/problemset/problem/460/C C. Present time limit per test 2 seconds memory ...

  4. Codeforces Round #262 (Div. 2) 460C. Present

    题目连接:http://codeforces.com/problemset/problem/460/C C. Present time limit per test 2 seconds memory ...

  5. Codeforces Round #706 (Div. 2)-B. Max and Mex-题解

    目录 Codeforces Round #706 (Div. 2)-B. Max and Mex Problem Description Input Output Sample Input Sampl ...

  6. Codeforces Round #506 (Div. 3)

    Codeforces Round #506 (Div. 3) 实习期间事不多,对div3 面向题解和数据编程了一波 A. Many Equal Substrings 题目链接 A题就是找后缀和前缀重合 ...

  7. Codeforces Round #417:E. FountainsSagheer and Apple Tree(树上博弈)

    Codeforces Round #417:E. FountainsSagheer and Apple Tree(树上博弈) 标签: codeforces 2017-06-02 11:41 29人阅读 ...

  8. Codeforces Round #563 (Div. 2)/CF1174

    Codeforces Round #563 (Div. 2)/CF1174 CF1174A Ehab Fails to Be Thanos 其实就是要\(\sum\limits_{i=1}^n a_i ...

  9. Codeforces Round #270

    Codeforces Round #270 题目链接 A:我是筛了下素数.事实上偶数仅仅要输出4和x - 4,奇数输出9和x - 9就可以 B:贪心的策略,把时间排序后.取每k个的位置 C:贪心.每次 ...

  10. 构造 Codeforces Round #302 (Div. 2) B Sea and Islands

    题目传送门 1 /* 2 题意:在n^n的海洋里是否有k块陆地 3 构造算法:按奇偶性来判断,k小于等于所有点数的一半,交叉输出L/S 4 输出完k个L后,之后全部输出S:) 5 5 10 的例子可以 ...

最新文章

  1. *:学习心得——C语言----S1T62班 王作舟
  2. ESP8266常用脚本命令之一
  3. centos 卸载自带的 java
  4. 我的博客网站开发6——博文关键字搜索
  5. android.net是哪个jar,【Android Clock Synchronization】Android时钟同步:基于NTP协议的第三方库Apache Commons Net......
  6. jsp数据交互(一),九大内置对象
  7. Twitter数据抓取
  8. springzuul本地路由和跨服务器路由问题
  9. 【三维路径规划】基于matlab改进的蝙蝠算法农用无人机三维路径规划【含Matlab源码 1514期】
  10. 等级保护第三级安全通用要求笔记(2019)
  11. 疫情推动下的云联络中心终于引起了销售行业的重视。
  12. 在MS Word 中添加 Mathtype 插件(vbe6ext.olb不能被加载问题 已解决)
  13. 看了几个技术入股的帖子,忍不住写个自己亲身经历吧
  14. Flex语法和常用鼠标手势
  15. Ubuntu安装.md阅读工具-Typora
  16. Java毕设项目-医药信息管理系统
  17. 2016-HitCon-Pwn-house_of_orange学习(附赠FSOP基础知识)
  18. 渲大师携手向日葵,远控赋能云渲染及GPU算力服务
  19. JavaScript武器库
  20. led显示屏怎么连接电脑主机

热门文章

  1. Invalid url-pattern /admin/*.jsp in filter mapping
  2. IT 接口对接:足迹第十二步接口对接的定义(接口对接分三种:中间库方式的接口对接,Rest格式URL对接和HTTP格式URL对接;)
  3. Mandriva linux 资源列表
  4. iOS 适配iPhone XR/XS/XS MAX
  5. R 4.0 版本安装 rtools40教程,解决 Rtools is required to build R packages but is not currently installed 问题
  6. 人工智能之不确定推理方法
  7. 微信小程序wxml传递值总结(e.currentTarget,e.detail.value)
  8. C++中namespace detail或namespace internal的使用
  9. 国际象棋 小麦 用java怎么算_蓝桥杯-放麦子-java
  10. 机械臂速成小指南(二):机械臂的应用