//总的来说就是分别进行多次前缀和操作
C++ 代码
include [HTML_REMOVED]
include [HTML_REMOVED]
using namespace std;
const int N=1e5+10;
long long a[N], b[N],c[N],d[N];
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i)
{
scanf(“%d”,&a[i]);
b[i]=a[i];
}
sort(b+1,b+1+n); //到这里都是预处理操作
for(int i=1;i<=n;i)
{
c[i]=c[i-1]+a[i];
d[i]=d[i-1]+b[i];
} //前缀和数组的生成,类比数列的前n项和
int m;
cin>>m;
while(m–)
{
long long x,y,z;//不要忘记ll
cin>>x>>y>>z;
if(x==1) printf(“%lld\n”,c[z]-c[y-1]);
if(x==2) printf(“%lld\n”,d[z]-d[y-1]);
}
return 0;
}