首页 > 其他分享 >CF #727(div2)B. Love Song,前缀和

CF #727(div2)B. Love Song,前缀和

时间:2023-02-08 16:02:42浏览次数:44  
标签:Vasya Love string Song int song question CF length


problem

B. Love Song
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Petya once wrote a sad love song and shared it to Vasya. The song is a string consisting of lowercase English letters. Vasya made up q questions about this song. Each question is about a subsegment of the song starting from the l-th letter to the r-th letter. Vasya considers a substring made up from characters on this segment and repeats each letter in the subsegment k times, where k is the index of the corresponding letter in the alphabet. For example, if the question is about the substring “abbcb”, then Vasya repeats letter ‘a’ once, each of the letters ‘b’ twice, letter 'c" three times, so that the resulting string is “abbbbcccbb”, its length is 10. Vasya is interested about the length of the resulting string.

Help Petya find the length of each string obtained by Vasya.

Input
The first line contains two integers n and q (1≤n≤100000, 1≤q≤100000) — the length of the song and the number of questions.

The second line contains one string s — the song, consisting of n lowercase letters of English letters.

Vasya’s questions are contained in the next q lines. Each line contains two integers l and r (1≤l≤r≤n) — the bounds of the question.

Output
Print q lines: for each question print the length of the string obtained by Vasya.

Examples
inputCopy
7 3
abacaba
1 3
2 5
1 7
outputCopy
4
7
11
inputCopy
7 4
abbabaa
1 3
5 7
6 6
2 4
outputCopy
5
4
1
5
inputCopy
13 7
sonoshikumiwo
1 5
2 10
7 7
1 13
4 8
2 5
3 9
outputCopy
82
125
9
191
62
63
97
Note
In the first example Vasya is interested in three questions. In the first question Vasya considers the substring “aba”, that transforms to “abba”, so the answer is equal to 4. In the second question Vasya considers “baca”, that transforms to “bbaccca”, so the answer is 7. In the third question Vasya considers the string “abacaba”,that transforms to “abbacccabba” of length 11.

solution

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 100010;
vector<int>z[maxn];
int main(){
int n, q; cin>>n>>q;
string s; cin>>s; s="0"+s;
z[0].resize(27);
for(int i = 1; i <= n; i++){
z[i].resize(27);
z[i] = z[i-1];
z[i][s[i]-'a']++;
}
while(q--){
int l, r; cin>>l>>r;
LL ans = 0;
for(int i = 0; i < 26; i++){
ans += (z[r][i]-z[l-1][i])*1LL*(i+1);
}
cout<<ans<<"\n";
}
return 0;
}


标签:Vasya,Love,string,Song,int,song,question,CF,length
From: https://blog.51cto.com/gwj1314/6044581

相关文章

  • CF #727(div2)D. PriceFixed, 贪心,双指针
    problemD.PriceFixedtimelimitpertest1secondmemorylimitpertest256megabytesinputstandardinputoutputstandardoutputLenaisthemosteconomicalgirli......
  • CF #727(div2)C. Stable Groups,贪心,排序
    problemC.StableGroupstimelimitpertest1secondmemorylimitpertest256megabytesinputstandardinputoutputstandardoutputTherearenstudentsnumerated......
  • CF1693 ABCD 题解
    题目链接:https://codeforces.com/contest/1693这场的题都非常好啊……因为现在是从div1开始做了,所以可能刚开始会有点吃力(这场我就会做一个1B呜呜呜)1A先把后缀的极......
  • 每日一道思维题——CF1761C - Set Construction
    题意:存在一个n×n的01矩阵(i,j)处值为1代表Ai 是Aj的真子集,求出这个集合A思路:我们在一开始的时候将每个位置赋初值,若i处的值是j的真子集将i处的值赋值给j代码:#inc......
  • CF1511G Chips on a Board
    CF1511GChipsonaBoard比较有启发性的一道题。询问是最简单的nim游戏,不难发现若一列上有两个棋子,那么这两个棋子对于答案是没有贡献的,因此可以令\(c_i\)表示第\(......
  • CF961E 另解
    一种不用思考怎么树状数组/主席树的笨蛋做法将题目的a[i]视作一个横坐标[i-1,i],纵坐标[0,a[i]]的矩形,我们要统计的是二维平面上同时存在的(x,y)与(y,x)对数。这样的对子......
  • CF14D题解
    CF14DTwoPaths题解题目链接传送门题意简述给定一棵树,找出两条不经过相同点的最长路径,使得他们的长度乘积最大。题目分析首先,如果在一棵树上,两条路径没有共同的点,那......
  • CF1785B Letter Exchange 题解(思维+模拟)
    题目链接难度:绿+。题意给定\(t\)组testcase,每组testcase如下。有\(m\)个长度为3的字符串,每个字符都是\(\text{w}\)、\(\text{i}\)、\(\text{n}\)中的一个,一......
  • [CF1190D] Tokitsukaze and Strange Rectangle
    题目描述Thereare$n$pointsontheplane,the$i$-thofwhichisat$(x_i,y_i)$.Tokitsukazewantstodrawastrangerectangularareaandpickallt......
  • [SA记录] CF1073G Yet Another LCP Problem
    一开始刚看这题时感觉什么思路都没有,不过后来做完P4248[AHOI2013]差异和P7409SvT后再看感觉稍微好一点。这3道题都是SA+单调栈的套路。这一种套路看起来似乎基本都是处......