D - Candies!

Consider a sequence of digits of length [a1,a2,…,a]. We perform the following operation with it: replace pairs (a2i+1,a2i+2) with (a2i+1+a2i+2)mod10 for 0≤i<2k−1. For every i where a2i+1+a2i+2≥10 we get a candy! As a result, we will get a sequence of length 2k−1

Less formally, we partition sequence of length 2k into 2k−1 pairs, each consisting of 2 numbers: the first pair consists of the first and second numbers, the second of the third and fourth …, the last pair consists of the (2k−1)-th and (2k)-th numbers. For every pair such that sum of numbers in it is at least 10, we get a candy. After that, we replace every pair of numbers with a remainder of the division of their sum by 10 (and don't change the order of the numbers).

Perform this operation with a resulting array until it becomes of length 1 . Let f([a1,a2,…,a2k]) denote the number of candies we get in this process.

For example: if the starting sequence is [8,7,3,1,7,0,9,4] then:

After the first operation the sequence becomes [(8+7)mod10,(3+1)mod10,(7+0)mod10,(9+4)mod10] = [5,4,7,3], and we get 2 candies as 8+7≥10 and 9+4≥10.

After the second operation the sequence becomes [(5+4)mod10,(7+3)mod10] = [9,0], and we get one more candy as 7+3≥1.

After the final operation sequence becomes [(9+0)mod10] = [9].

Therefore, f([8,7,3,1,7,0,9,4])=3 as we got 3 candies in total.

You are given a sequence of digits of length n s1,s2,…sn. You have to answer q queries of the form (li,ri), where for i-th query you have to output f([sli,sli+1,…,sri]). It is guaranteed that ri−li+1 is of form 2k for some nonnegative integer k.

Input

The first line contains a single integer n (1≤n≤1e5) — the length of the sequence.

The second line contains n digits s1,s2,…,sn (0≤si≤9).

The third line contains a single integer q(1≤q≤1e5) — the number of queries.

Each of the next q lines contains two integers li, ri (1≤li≤ri≤n) — i-th query. It is guaranteed that ri−li+1 is a nonnegative integer power of 2.

Output

Output q lines, in i-th line output single integer — f([sli,sli+1,…,sri]), answer to the i-th query.

这道题使用暴力求解会因为时间复杂度问题超时,正确做法是使用前缀和或者线段树或者树桩数组进行编写,下面一个一个进行学习。

前缀和

什么是前缀和:

给定一个数组a[0],a[1],...,a[j],,,,,a[n],a[n+1]。

a[i]+a[i+1]+…+a[j]=sum[j]-sum[i-1]。

所以,该题运用前缀和进行计算给定区间前缀和除以10的结果即是最终答案。

正确代码

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std; const int maxn=1e5+5;
int bit[maxn+1],n; int sum(int i){
int s=0;
while(i>0){
s+=bit[i];
i-=i&-i;
}
return s;
} void add(int i,int x){
while(i<=n){
bit[i]+=x;
i+=i&-i;
}
} void init(int n){
int tn=n,t;
n=1;
while(n<tn) n=(n<<1);
memset(bit,0,2*n-1);
for(int i=1;i<=tn;i++){
scanf("%d",&t);
add(i,t);
}
} void solve(){
scanf("%d",&n);
init(n); int q,l,r;
scanf("%d",&q);
while(q--){
scanf("%d%d",&l,&r);
printf("%d\n",(sum(r)-sum(l))/10);
}
} int main(){
solve();
return 0;
}

位与运算符&,对ascll码进行运算,就是二进制运算, 0001 , 0011 , 0101 , 0111 就是1 , 3 , 5 , 7的二进制表示则1&3&5&7=1因为他们末尾都是1而其他不相同。

#C++初学记录ACM补题(D. Candies!)前缀和运算。的更多相关文章

  1. #C++初学记录(ACM试题1)

    A - Diverse Strings A string is called diverse if it contains consecutive (adjacent) letters of the ...

  2. #C++初学记录(ACM试题2)

    Max Sum Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-seq ...

  3. #C++初学记录(ACM8-6-cf-f题)

    F. Vanya and Label While walking down the street Vanya saw a label "Hide&Seek". Becaus ...

  4. 第十届山东省acm省赛补题(1)

    今天第一场个人训练赛的题目有点恐怖啊,我看了半个小时多硬是一道都不会写.我干脆就直接补题去了.... 先补的都是简单题,难题等我这周末慢慢来吧... A Calandar Time Limit: 1 ...

  5. 【补题记录】ZJU-ICPC Summer Training 2020 部分补题记录

    补题地址:https://zjusummer.contest.codeforces.com/ Contents ZJU-ICPC Summer 2020 Contest 1 by Group A Pr ...

  6. 【JOISC 2020 补题记录】

    目录 Day 1 Building 4 Hamburg Steak Sweeping Day 2 Chameleon's Love Making Friends on Joitter is Fun R ...

  7. 【cf补题记录】Codeforces Round #608 (Div. 2)

    比赛传送门 再次改下写博客的格式,以锻炼自己码字能力 A. Suits 题意:有四种材料,第一套西装需要 \(a\).\(d\) 各一件,卖 \(e\) 块:第二套西装需要 \(b\).\(c\).\ ...

  8. 【cf补题记录】Codeforces Round #607 (Div. 2)

    比赛传送门 这里推荐一位dalao的博客-- https://www.cnblogs.com/KisekiPurin2019/ A:字符串 B:贪心 A // https://codeforces.c ...

  9. ACM思维题训练 Section A

    题目地址: 选题为入门的Codeforce div2/div1的C题和D题. 题解: A:CF思维联系–CodeForces -214C (拓扑排序+思维+贪心) B:CF–思维练习-- CodeFo ...

随机推荐

  1. vue 属性props定义方法

    当子组件接收父组件传过来的值的时候,我们一般有两种方式来接收 不过大家好像都用第二种方式,我只有在不确定数据类型的时候才用第一种方式 第一种: export default { // 不检测类型,全盘 ...

  2. python基础-函数递归

    函数递归 概念:直接或间接地重复调用函数本身,是一种函数嵌套调用的表现形式. 直接调用:在函数内部,直接调用函数本身 def foo(): print("这是foo函数") foo ...

  3. MySQL MHA候选主库选择

    MHA在选择新主库时,会将所有存活的从库分为下面几类: 存活从库数组:挑选所有存活的从库 最新从库数组: 挑选Master_Log_File+Read_Master_Log_Pos最高的从库 优选从库 ...

  4. Java开发环境之Maven

    查看更多Java开发环境配置,请点击<Java开发环境配置大全> 肆章:Maven安装教程 1)下载Maven安装包 https://maven.apache.org/download.c ...

  5. java验证邮件正则

    这里,本人从commons-validator包中源码,拷出部分内容,如下: private static final String EMAIL_REGEX = "^\\s*?(.+)@(. ...

  6. iniparser——C配置文件解析库

    简介 ini文件则是一些系统或者软件的配置文件,iniparser是免费.独立的INI解析器,Github地址(也是主要更新地址)请点击这个,官网上的tarball版本比较老,主要是为了保留之前的di ...

  7. hash表的理解

    哈希表 先从数组说起 任何一个程序员,基本上对数组都不会陌生,这个最常用的数据结构,说到它的优点,最明显的就是两点: 简单易用,数组的简易操作甚至让大多数程序员依赖上了它,在资源富足的情况下,我们甚至 ...

  8. 亚洲唯一:瀚思科技入选2019 Gartner SIEM 领域 Peer Insights,其他第一象限的有splunk和logrithym,elastic==,RSA、fortinet、rapid7和翰思一样都在第二象限

    亚洲唯一:瀚思科技入选 Gartner SIEM 领域 Peer Insights     网络安全技术与产业,正在由传统的合规驱动,走向合规与需求双轮驱动.关注用户需求.倾听用户声音,根据实际情况打 ...

  9. [USACO08OCT]:打井Watering Hole(MST)

    题意:有N个牧场,每个牧场修水井花费Wi,连接牧场花费Pij,问最小花费,使得每个牧场要么有水井,要么和有水井的牧场有通道. 思路:加一个格外的节点O,连接O表示修井,边权是修井的费用.     那么 ...

  10. 为什么管理人员都喜欢用Visio画图

    一.形状数据一体化 这是管理者最喜欢的功能了,这也Visio的最核心的功能: 操作如下: 例如流程中的步骤.开始日期或结束日期.成本.设备部件等.数字.图标.颜色.标志和进度条等图形有助于快速方便地浏 ...