CF1539B Love Song[题解]
Love Song
题目大意
给定长度为 \(n\) 的字符串和 \(q\) 个区间 \([l,r]\) 。定义一个字符的值为该字母在字母表中的序号,对于给定的每个区间,求其中所有字符的值的和。
分析
此题基本属于秒出了……
维护一个前缀和,就结束了。
CODE
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+10;
inline int read()
{
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9') { if(ch=='-') w*=-1; ch=getchar(); }
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
int n,q;
int a[N];
int sum[N];
signed main()
{
n=read(),q=read();
for(register int i=1;i<=n;i++){
int c=getchar();
if(c=='\n') { i--; continue; }
a[i]=c-'a'+1;
}
for(register int i=1;i<=n;i++) a[i]=a[i-1]+a[i];
for(register int i=1;i<=q;i++){
int l=read(),r=read();
printf("%lld\n",a[r]-a[l-1]);
}
return 0;
}