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

原题链接:
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;
}

欢迎评论!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值