首页 > 其他 > 详细

Codeforces Round #524 (Div. 2) B. Margarite and the best present

时间:2018-11-25 21:56:58      阅读:207      评论:0      收藏:0      [点我收藏+]

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\n",len/2+r);
            else printf("%d\n",r-len/2);
        }else{
            if(l%2) printf("%d\n",len/2);
            else printf("%d\n",-len/2);
        }
    }
}

 

Codeforces Round #524 (Div. 2) B. Margarite and the best present

原文:https://www.cnblogs.com/heyuhhh/p/10017404.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!