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 ...
随机推荐
- 剑指offer 38_统计数组中k出现的个数
思路: 二分法,分别找出第一个和最后一个k出现的位置.相减 加一 #include <stdio.h> //获取第一个K的位置 int getFirstK (int k,int *numb ...
- LinkedHashMap和HashMap的区别
一.问题描述: 前几天写webservices接口,需要同步人力资源,涉及到添加顺序:主账号需要添加在次账号之前,直接上级需要添加在下级之前.解析xml之后直接封装在HashMap中,导致取对象时顺序 ...
- JAVA中几个修饰符的作用以及一些相关话题
几个传统的修饰符: public 该类的子类,以及同包,或者其他情况下可以访问该修饰符修饰的方法/变量 protacted 只有同包,子类,该类本身可以访问 private 只有该类自身能访问 无修饰 ...
- css实现三角形(转)
css实现三角形 (2012-09-10 14:17:26) 标签: css 三角形 杂谈 分类: 网页制作 css实现三角形的原理是:当元素的宽高为0,边框(border)不为0时,四个角边框交界重 ...
- 框架之 hibernate之二
1. Hibernate持久化对象的状态 2. Hibernate的一级缓存 3. Hibernate操作持久化对象的方法 4. Hibernate的基本查询 Hibernate的持久化类 什么是持久 ...
- static、静态变量、静态方法
1 静态:static 1.1 用法 是一个修饰符:用于修饰成员(成员变量和成员函数) 1.2 好处 当成员变量被静态static修饰后,就多了一种调用方式,除了可以被对象调用外,还可以直接被类名调用 ...
- SQl Server 表链接查询
之前漏下了,这里补一偏 select * from student,score ——笛卡尔积 可以想想成c#里面的多维函数的样子,打印时每一张表的每一条数据都会连带着第二张表的所有数据 两个表的连接: ...
- C语言-郝斌笔记-004判断是否为回文数
判断是否为回文数 # include <stdio.h> int main(void) { int val; //存放待判断的数字 int m; ; printf("请输入您需要 ...
- ARC102D All Your Paths are Different Lengths
传送门 题目大意 让你构造一个有向图,使得从1到n有L条不同路径且长度分别是0~L-1. 分析 我们不难想到每一对相邻点之间连一条权值为0的边,之后二进制分解,将每一对点之间连一个权值为2^i的边,但 ...
- 自动化打包资源混淆集成python实践----打包
1.自动化打包方案 1)友盟多渠道多渠道打包 2)gradle productFlavors系统的条件编译 3)美团打包 4)APK文件注释写入渠道号 2.各打包方案简介 1)友盟多渠道多渠道打包(w ...