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
来源:优快云
原文:https://blog.youkuaiyun.com/lbperfect123/article/details/84449954
版权声明:本文为博主原创文章,转载请附上博文链接!

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
using namespace std;
 
int main()
{
    int n;
    cin>>n;
 
    for(int t=0;t<n;t++)
    {
        int a,b;
        long long int sum;
        scanf("%d%d",&a,&b);
        if(a%2==1&&b%2==1)
        {
            sum=-1*((a-b)/2+b);
        }
        else if(a%2==1&&b%2==0)
        {
            sum=(b-a+1)/2;
        }
        else if(a%2==0&&b%2==1)
        {
            sum=-1*(b-a+1)/2;
        }
        else
        {
            sum=(a-b)/2+b;
        }
        printf("%lld\n",sum);
        
    }
    return 0;
 } 

 

转载于:https://www.cnblogs.com/Staceyacm/p/10012742.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值