Codeforces Round #524 (Div. 2)B

 

题意:就是一个简单的序列,奇数位为负,偶数位为正,求[l, r]的区间和 

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
	int q;
	scanf("%d", &q);
	while(q --)
	{
		int l, r;
		scanf("%d%d", &l, &r);
		if((r - l + 1) & 1)
		{
			int t;
			r ++;
			if(l & 1)
				t = (r - l + 1) / 2;
			else
				t = -(r - l + 1) / 2;
			if(r & 1)
				r = -r;
			t += -r;
			printf("%d\n", t);
		}
		else
		{
			if(l & 1)
			{
				printf("%d\n", (r - l + 1) / 2);
			}
			else
			{
				printf("%d\n", -(r - l + 1) / 2);
			}
		}
	}
}

猜你喜欢

转载自blog.csdn.net/qq_41695941/article/details/84668912