Consider a sequence of digits of length 2k2k [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 ii 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 2k2k 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 11. Let f([a1,a2,…,a2k])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 qq queries of the form (li,ri), where for ii-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 nn 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 qq lines contains two integers lili, riri (1≤li≤ri≤n) — ii-th query. It is guaranteed that ri−li+1 is a nonnegative integer power of 2.
Output
Output qq lines, in ii-th line output single integer — f([sli,sli+1,…,sri]), answer to the ii-th query.
Examples
input
8
8 7 3 1 7 0 9 4
3
1 8
2 5
7 7
output
3
1
0
input
6
0 1 2 3 3 5
3
1 2
1 4
3 6
output
0
0
1
题意:就是输入一个整型数组,q个查询,问查询数组下标a~b的和是10的多少倍,就一个线段树求和。
代码:
#include <bits/stdc++.h>
using namespace std;
int num[100010];
struct node
{
int l,r,sum;
}tree[444110];
void buildtree(int l,int r,int t)
{
if(l==r)
{
tree[t].sum=num[l];
tree[t].l=tree[t].r=l;
}
else
{
int mid=(int)(l+r)/2;
buildtree(l,mid,t*2);
buildtree(mid+1,r,t*2+1);
tree[t].sum=tree[t*2].sum+tree[t*2+1].sum;
tree[t].l=tree[t*2].l;
tree[t].r=tree[t*2+1].r;
}
return;
}
int query(int l,int r,int t,int x,int y)
{
if(x<=l&&y>=r)
return tree[t].sum;
else
{
int sr=0,sl=0;
int mid=(int)(l+r)/2;
if(y>mid){
sr = query(mid+1,r,t*2+1,x,y);
}
if(x<=mid)
sl = query(l,mid,t*2,x,y);
return sr+sl;
}
return 0;
}
int main()
{
int n,m;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&num[i]);
buildtree(1,n,1);
scanf("%d",&m);
int a,b;
for(int i=0;i<m;i++){
scanf("%d%d",&a,&b);
int st=query(1,n,1,a,b);
int ans=(int)st/10;
printf("%d\n",ans);
}
return 0;
}