周练 找规律

 

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 109109elements that is filled as follows:

  • a1=−1a1=−1
  • a2=2a2=2
  • a3=−3a3=−3
  • a4=4a4=4
  • a5=−5a5=−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!

想错了很多次
AC的代码思想是:
分i,j都是偶数、都是奇数、i,j奇偶不同来判断
不能暴力
#include<iostream>
#include<cmath>
#include<queue>
using namespace std;
int main()
{
    int q;
    cin>>q;
    for(int i=0;i<q;i++){
        int l,r,as=0;
        cin>>l>>r;
        if((l%2==0&&r%2==0)){
            as=(l+r)/2;
        }else if(l%2==1&&r%2==1){
            as=-(l+r)/2;
        }else {
            if(l%2==1&&r%2==0){
                as=-(l+r+1)/2+r+1;
            }
            if(l%2==0&&r%2==1){
                as=(l+r+1)/2-r-1;
            }
        }
        cout<<as<<endl;
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值