Margarite and the best present
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 aa of the size of 109109 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 ii-th element of the array aa is calculated using the formula ai=i⋅(−1)iai=i⋅(−1)i.
She immediately came up with qq queries on this array. Each query is described with two numbers: ll and rr. The answer to a query is the sum of all the elements of the array at positions from ll to rr 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 qq (1≤q≤1031≤q≤103) — the number of the queries.
Each of the next qq lines contains two integers ll and rr (1≤l≤r≤1091≤l≤r≤109) — the descriptions of the queries.
Output
Print qq lines, each containing one number — the answer to the query.
Example
input
Copy
5
1 3
2 5
5 5
4 4
2 3
output
Copy
-2
-2
-5
4
-1
Note
In the first query, you need to find the sum of the elements of the array from position 11 to position 33. The sum is equal to a1+a2+a3=−1+2−3=−2a1+a2+a3=−1+2−3=−2.
In the second query, you need to find the sum of the elements of the array from position 22 to position 55. The sum is equal to a2+a3+a4+a5=2−3+4−5=−2a2+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 55 to position 55. The sum is equal to a5=−5a5=−5.
In the fourth query, you need to find the sum of the elements of the array from position 44 to position 44. The sum is equal to a4=4a4=4.
In the fifth query, you need to find the sum of the elements of the array from position 22 to position 33. The sum is equal to a2+a3=2−3=−1a2+a3=2−3=−1.
题解:对于多种情况考虑分析即可
---------------------
作者:black_hole6
来源:CSDN
原文:https://blog.csdn.net/lbperfect123/article/details/84449954
版权声明:本文为博主原创文章,转载请附上博文链接!
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
using namespace std; int main()
{
int n;
cin>>n; for(int t=;t<n;t++)
{
int a,b;
long long int sum;
scanf("%d%d",&a,&b);
if(a%==&&b%==)
{
sum=-*((a-b)/+b);
}
else if(a%==&&b%==)
{
sum=(b-a+)/;
}
else if(a%==&&b%==)
{
sum=-*(b-a+)/;
}
else
{
sum=(a-b)/+b;
}
printf("%lld\n",sum); }
return ;
}
Margarite and the best present的更多相关文章
- Codeforces Round #524 (Div. 2) B. Margarite and the best present
B. Margarite and the best present 题目链接:https://codeforces.com/contest/1080/problem/B 题意: 给出一个数列:an=( ...
- CF1080B Margarite and the best present 题解
Content 有 \(t\) 次询问,每次询问给定两个整数 \(l,r\),求 \(\sum\limits_{i=l}^r (-1)^i\times i\). 数据范围:\(1\leqslant t ...
- Codeforces Round #524 (Div. 2)
A. Petya and Origamitime limit per test1 secondmemory limit per test256 megabytesinputstandard input ...
- Codeforces Round #524 (Div. 2) Solution
A. Petya and Origami Water. #include <bits/stdc++.h> using namespace std; #define ll long long ...
- CodeForces-Round524 A~D
A. Petya and Origami time limit per test 1 second memory limit per test 256 megabytes input stan ...
- Codeforces Round #524 (Div.2)题解
题解 CF1080A [Petya and Origami] 这道题其实要我们求的就是 \[\lceil 2*n/k \rceil + \lceil 5*n/k \rceil + \lceil 8*n ...
- Codeforces Round #524 (Div. 2)(前三题题解)
这场比赛手速场+数学场,像我这样读题都读不大懂的蒟蒻表示呵呵呵. 第四题搞了半天,大概想出来了,但来不及(中途家里网炸了)查错,于是我交了两次丢了100分.幸亏这次没有掉rating. 比赛传送门:h ...
- 跳转时候提示Attempt to present on while a presentation is in progress
出现这种情况,例如:我在获取相册图片后,直接present到另一个页面,但是上一个页面可能还未dismiss,所以,要在获取相册图片的dismiss方法的complete的block里面写获取图片及跳 ...
- find your present (感叹一下位运算的神奇)
find your present (2) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
随机推荐
- windows下查看端口占用(砖)
第一步,执行命令:netstat -ano -a 显示所有连接和监听端口 -n 以数字形式显示地址和端口号. 此选项一般与 -a选项组合使用 -o 显示与每个连接相关的所属进程 ID. Active ...
- java在Win7 64位 获取客户端的IP,MAC,计算机名
package com.javaweb.util; import java.io.IOException; import java.io.InputStreamReader; import java. ...
- [Python Study Notes]双层柱状图绘制
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
- RxAndroid基本使用1
1,基本使用 public class MainActivity extends ActionBarActivity implements View.OnClickListener, View.OnT ...
- java格式化数字、货币、金钱
网上摘来的,以后可能会用到 java开发中经常会有数字.货币金钱等格式化需求,货币保留几位小数,货币前端需要加上货币符号等.可以用java.text.NumberFormat和java.text.De ...
- UWP蓝牙的例子
https://answers.microsoft.com/zh-hans/windows/forum/windows_10-networking/%e5%9c%a8win10%e7%8e%af%e5 ...
- Luogu 4001 [BJOI2006]狼抓兔子
BZOJ 1001…… 并不会这个trick,所以笔记要详细一点. 前置知识 : 平面图转对偶图 传送门 听说直接$Dinic$就好了,还跑得比正解快…… 首先我们按照平面图的定义,把网格图中所 ...
- Ubuntu jdk安装详细
安装jdk步骤: 1.首先连接130的ip地址,进去Tools拷贝jdk压缩文件(Tools中已经有下载好的jdk版本,也可自己另行下载) 2.将拷贝好的压缩文件解压 tar -zxvf 压缩文件名 ...
- Json数据导出生成Excel
最近在做一个导入导出Excel的功能,导出其他类型的文件都比较熟悉,但是导入跟导出一个Excel还是稍微特殊点.根据这次的经验,写了个导出的小样例. 总体思路就是json数据的key,value跟Ex ...
- ElementUI的表单和vee-validate结合使用时发生冲突的解决
在Vue项目中使用ElementUI表单时,同时又引入了vee-validate进行使用的时候,在浏览器上会出现这样的报错: [Vue warn]: The computed property &qu ...