Codeforces Round #524 (Div. 2) B. 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=(-1)n,之后有询问,问 [l,r] 之间的ai和为多少。

题解:
这个分情况讨论一下就可以了,区间长度的奇偶数丶左端点的奇偶数。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;

typedef long long ll;
int q;
int main(){
    cin>>q;
    for(int i=1;i<=q;i++){
        int l,r;
        scanf("%d%d",&l,&r);
        int len = r-l+1;
        if(len%2){
            if(l%2) printf("%d
",len/2+r);
            else printf("%d
",r-len/2);
        }else{
            if(l%2) printf("%d
",len/2);
            else printf("%d
",-len/2);
        }
    }
}