1012: QAQ的区间统计
时间限制: 1 Sec 内存限制: 128 MB提交: 151 解决: 48
[ 提交][ 状态][ 讨论版]
题目描述
QAQ有一个区间[L, R]。已知QAQ很喜欢长度为奇数的区间,现在请你告诉他有多少个长度为奇数的子区间。
对于区间[2 4]来讲,它的子区间有[2 2] [2 3] [2 4] [3 3] [3 4] [4 4]共6个。
为了简化题目,QAQ所给的区间里面全是非负整数。
对于区间[2 4]来讲,它的子区间有[2 2] [2 3] [2 4] [3 3] [3 4] [4 4]共6个。
为了简化题目,QAQ所给的区间里面全是非负整数。
输入
第一行输入一个整数T,代表有T组测试数据。
每组数据输入两个整数L,R,代表区间的端点。
注:1 <= T <= 1000000,0 <= L <= R <= 100000000。
每组数据输入两个整数L,R,代表区间的端点。
注:1 <= T <= 1000000,0 <= L <= R <= 100000000。
输出
对每组测试数据,输出一个整数代表长度为奇数的子区间个数。
样例输入
2
1 6
1 5
样例输出
12
9
<span class="sampledata">举一个区间长度为偶数的例子:</span>
<span class="sampledata">1 -> 6 1 1 1 2 1 3 1 4 1 5 1 6 3 2 2 2 3 2 4 2 5 2 6 3 6 3 3 3 4 3 5 3 6 2 4 4 4 5 4 6 2 4 5 5 5 6 1 6 6 1 2</span>
<span class="sampledata">结果为2+4+6;</span>
12=3*(3+1)
为奇数时
1->5
1 2 3 4 5
5+3+1=(5+1)/2*(5+1)/2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
#include<cstdio>
#include<algorithm>
using
namespace
std;
int
main()
{
int
t;
long
long
l,r;
scanf
(
"%d"
,&t);
while
(t--)
{
scanf
(
"%lld%lld"
,&l,&r);
int
n=r-l+1;
long
long
sum=0;
long
long
m=n/2,mm=n%2;
if
(mm==0)
sum=m*(m+1);
else
{
m=(n+1)/2;
sum=m*m;
}
printf
(
"%lld\n"
,sum);
}
return
0;
}
/**************************************************************
Problem: 1012
User: 311509010108
Language: C++
Result: 正确
Time:748 ms
Memory:1036 kb
****************************************************************/
|