Codeforces Round #572 (Div. 2) C. Candies!

C. Candies!

Consider a sequence of digits of length 2k [a1,a2,…,a2k]. 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≥10.

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≤105) — 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≤105) — 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.

Examples
inputCopy
8
8 7 3 1 7 0 9 4
3
1 8
2 5
7 7
outputCopy
3
1
0
inputCopy
6
0 1 2 3 3 5
3
1 2
1 4
3 6
outputCopy
0
0
1
Note
The first example illustrates an example from the statement.

f([7,3,1,7])=1: sequence of operations is [7,3,1,7]→[(7+3)mod10,(1+7)mod10] = [0,8] and one candy as 7+3≥10 → [(0+8)mod10] = [8], so we get only 1 candy.

f([9])=0 as we don’t perform operations with it.

思路:差分数组

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<set>
#include<algorithm>
#define ll long long
#define dd double
#define PI acos(-1.0)
using namespace std;
ll n, a[100005], b[100005], q, l, r;
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++)b[i] = 0;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		for (int j = i; j <= n; j++) {
			b[j] += a[i];
		}
	}
 
	cin >> q;
	while (q--) {
		cin >> l >> r;
		ll p = b[r] - b[l-1];
		ll ans = p / 10;
		cout << ans << endl;
	}
	//system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值