题目描述
小美当上了会计。她现在拿到了n个开支数据a[1],a[2],…,a[n],现在她想稍微对这些数据做一些统计。小美有三种想统计的信息。第一种是她选择一个区间[L,R],希望知道a[L]+a[L+1]+…+a[R]等于多少,第二种是她选择一个区间[L,R],希望知道a[L],a[L+1],…,a[R]这些数据的有效值是多少,第三种是她选择一个区间[L,R],希望知道a[L],a[L+1],…,a[R]的最大值是多少。一组数据b[1],b[2],…,b[r]的有效值定义为:
∑
j
=
1
r
(
∑
i
=
1
r
b
[
i
]
−
b
[
j
]
)
2
\sum_{j=1}^{r}(\sum_{i=1}^{r}b[i]-b[j])^2
∑j=1r(∑i=1rb[i]−b[j])2
输入描述
第一行一个整数n
,表示一共有n个整数。
第二行n个整数a[1],a[2],...,a[n]
,表示小美拿到的数据是哪些。
第三行一个整数m
,表示一共有m个询问。
接下来m行,每行三个整数,opt
,L
,R
,当opt=1
时表示是第一种询问,opt=2
时表示第二种询问,opt=3
时便是第三种询问。(1<=opt<=3
,1<=L<=R<=n
)
数据保证每个收支数据a[i]满足-1000<=a[i]<=1000
。
统计次数m满足1<=m<=500
。
其中,对于60%的数据n满足1<=n<=1000
。
对于100%的数据n满足1<=n<=100000
。
输出描述
输出m
行,每行一个数,表示改行对应查询的答案。
样例输入
4
1 1 2 3
3
1 1 3
2 2 4
3 1 4
样例输出
4
50
3
参考代码
#include<iostream>
#include<vector>
using namespace std;
vector<int> a;
int fun_opt1(int L, int R)
{
int ret = 0;
for (int i = L-1; i < R; ++i)
{
ret = ret + a[i];
}
return ret;
}
int fun_opt2(int L, int R)
{
int ret = 0;
int tmp_1 = fun_opt1(L, R);
for (int i = L-1; i < R; ++i)
{
ret = ret + (tmp_1 - a[i]) * (tmp_1 - a[i]);
}
return ret;
}
int fun_opt3(int L, int R)
{
int ret = 0;
for (int i = L - 1; i < R; ++i)
{
if (ret < a[i])
{
ret = a[i];
}
}
return ret;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
int a_tmp;
for (int i = 0; i < n; ++i)
{
cin >> a_tmp;
a.push_back(a_tmp);
}
int m;
cin >> m;
int opt_tmp, L_tmp, R_tmp;
vector<int> opt, L, R;
for (int i = 0; i < m; ++i)
{
cin >> opt_tmp >> L_tmp >> R_tmp;
opt.push_back(opt_tmp);
L.push_back(L_tmp);
R.push_back(R_tmp);
}
int ret = 0;
for (int i = 0; i < m; ++i)
{
if (opt[i] == 1)
{
ret = fun_opt1(L[i], R[i]);
cout << ret << endl;
}
else if (opt[i] == 2)
{
ret = fun_opt2(L[i], R[i]);
cout << ret << endl;
}
else if (opt[i] == 3)
{
ret = fun_opt3(L[i], R[i]);
cout << ret << endl;
}
}
return 0;
}
注:此代码仅通过45%的数据