版权声明:欢迎转载欢迎评论! https://blog.csdn.net/rabbit_ZAR/article/details/85254282
题目:Margarite and the best present
思路:
其实我们可以先列个表看看的——
a_i -1 2 -3 4 -5 6 -7 8 -9 10
sum_i -1 1 -2 2 -3 3 -4 4 -5 5 //sum是a的前缀和
找个规律——
发现
然后就可以求出 和 了,相减即可。
代码:
#include<bits/stdc++.h>
using namespace std;
#define read(x) scanf("%d",&x)
int main() {
int T;
read(T);
while(T--) {
int L,R;
read(L),read(R);
printf("%d\n",((R+1)/2*((R&1)?-1:1))-((L)/2*((L&1)?1:-1)));
}
return 0;
}