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 ...
随机推荐
- 基于ActiveMQ的Topic的数据同步——消费者持久化
前面一章中介绍了activemq的初步实现:基于ActiveMQ的Topic的数据同步——初步实现 下面来解决持久化订阅的问题: (1)使用queue,即队列时,每个消息只有一个消费者,所以,持久化很 ...
- oracle DML-(insert、select、update、delete)
一.插入记录INSERT INTO table_name (column1,column2,...) values ( value1,value2, ...); 示例:insert into emp ...
- SpringBoot11 读取properties文件、发送邮件
1 读取properties文件 1.1 ResourceBundle 帮助我们事先国际化 1.1.1 前提 properties文件的命名方式必须体现除语言和国别 例如:test_zh_CN.pro ...
- 业务逻辑: Quartz的整合应用
1. 请谈一下你对Quartz的理解 思路:根据他解决的什么问题方面去阐述 2. 完成quartz和spring的整合应用 思路:触发时间.任务调度工程 步骤: 1. 创建maven工程,并导入qua ...
- linux鸟哥的私房菜
这书还是感觉非常棒,真的是授之以渔而不是授之以鱼.我觉得只需要掌握一个命令就可以了man -k KEYWORD 比如我想查找和防火墙相关的命令,那么 man -k firewall 结果是ufw 然后 ...
- A - Dictionary
传送门 题目大意 给你n个字符串,问是否可以通过改变26个字母的排列顺序是这n个字符串的字典序是非降排列的. 分析 我们考虑设相邻两个字符串的第一个不相同字符的位置为j,以为要求字典序不降,所以有第i ...
- 9.Delegate类
Concepts 不像MVC模式,Model/View模式并没有单独用来处理事件和用户交互的组件(controller).通常,视图负责向用户呈现模型中的数据,并处理用户的输入.有时,为了让 ...
- Pig Latin程序设计1
Pig是一个大规模数据分析平台.Pig的基础结构层包括一个产生MapReduce程序的编译器.在编译器中,大规模并行执行依据存在.Pig的语言包括一个叫Pig Latin的文本语言,此语言有如下特性: ...
- C++面试笔记--STL模板与容器
1.C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂的数据结构算法和大量常用数据结构操作.vec ...
- C#/Python/MATLAB操作PostgreSQL数据库
PostgreSQL数据库是一个功能非常强大的开源数据库,支持多种SQL特性,非常好用.此外由于结合PostGIS可以实现空间数据库功能,故非常适合GIS领域的使用.本文旨在介绍C#.Python.M ...