#C++初学记录ACM补题(D. Candies!)前缀和运算。

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而其他不相同。

posted @ 2019-07-25 09:09  十魇  阅读(277)  评论(0编辑  收藏  举报
GenerateContentList