Consider a sequence of digits of length 2k2k [a1,a2,…,a2k][a1,a2,…,a2k]. We perform the following operation with it: replace pairs (a2i+1,a2i+2)(a2i+1,a2i+2) with (a2i+1+a2i+2)mod10(a2i+1+a2i+2)mod10 for 0≤i<2k−10≤i<2k−1. For every ii where a2i+1+a2i+2≥10a2i+1+a2i+2≥10 we get a candy! As a result, we will get a sequence of length 2k−12k−1.
Less formally, we partition sequence of length 2k2k into 2k−12k−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−12k−1)-th and (2k2k)-th numbers. For every pair such that sum of numbers in it is at least 1010, we get a candy. After that, we replace every pair of numbers with a remainder of the division of their sum by 1010 (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][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][(8+7)mod10,(3+1)mod10,(7+0)mod10,(9+4)mod10] == [5,4,7,3][5,4,7,3], and we get 22 candies as 8+7≥108+7≥10 and 9+4≥109+4≥10.
After the second operation the sequence becomes [(5+4)mod10,(7+3)mod10][(5+4)mod10,(7+3)mod10] == [9,0][9,0], and we get one more candy as 7+3≥107+3≥10.
After the final operation sequence becomes [(9+0)mod10][(9+0)mod10] == [9][9].
Therefore, f([8,7,3,1,7,0,9,4])=3f([8,7,3,1,7,0,9,4])=3 as we got 33 candies in total.
You are given a sequence of digits of length nn s1,s2,…sns1,s2,…sn. You have to answer qq queries of the form (li,ri)(li,ri), where for ii-th query you have to output f([sli,sli+1,…,sri])f([sli,sli+1,…,sri]). It is guaranteed that ri−li+1ri−li+1 is of form 2k2k for some nonnegative integer kk.
The first line contains a single integer nn (1≤n≤1051≤n≤105) — the length of the sequence.
The second line contains nn digits s1,s2,…,sns1,s2,…,sn (0≤si≤90≤si≤9).
The third line contains a single integer qq (1≤q≤1051≤q≤105) — the number of queries.
Each of the next qq lines contains two integers lili, riri (1≤li≤ri≤n1≤li≤ri≤n) — ii-th query. It is guaranteed that ri−li+1ri−li+1 is a nonnegative integer power of 22.
Output qq lines, in ii-th line output single integer — f([sli,sli+1,…,sri])f([sli,sli+1,…,sri]), answer to the ii-th query.
8 8 7 3 1 7 0 9 4 3 1 8 2 5 7 7
3 1 0
6 0 1 2 3 3 5 3 1 2 1 4 3 6
0 0 1
The first example illustrates an example from the statement.
f([7,3,1,7])=1f([7,3,1,7])=1: sequence of operations is [7,3,1,7]→[(7+3)mod10,(1+7)mod10][7,3,1,7]→[(7+3)mod10,(1+7)mod10] == [0,8][0,8] and one candy as 7+3≥107+3≥10 →→ [(0+8)mod10][(0+8)mod10] == [8][8], so we get only 11 candy.
f([9])=0f([9])=0 as we don't perform operations with it.
一开始第七个用例说我TLE,心态都炸,O(NlogN)的操作TLE??
后来改了一下RE,才知道是tree的数组没有开够,1e5的数据量用tree的大小只有2e5,行不通
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <set> #include <queue> #include <map> #include <sstream> #include <cstdio> #include <cstring> #include <numeric> #include <cmath> #include <unordered_set> #include <unordered_map> //#include <xfunctional> #define ll long long #define mod 1000000007 using namespace std; int dir[4][2] = { { 0,1 },{ 0,-1 },{ -1,0 },{ 1,0 } }; const long long INF = 0x7f7f7f7f7f7f7f7f; const int inf = 0x3f3f3f3f; int s[100010]; struct node { int l, r, sum; }tree[440005]; void buildtree(int start,int end,int t) { if (start == end) { tree[t].sum = s[start]; tree[t].l = tree[t].r = start; } else { int mid = (start + end) / 2; buildtree(start, mid, t * 2); buildtree(mid + 1, end, 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; } } int query(int t,int start, int end, int L, int R) { if (L <= start && end <= R) { return tree[t].sum; } else { int sr=0, sl=0; int mid = (start + end) / 2; if (R > mid) { sr = query(t * 2 + 1, mid + 1, end, L, R); } if (L <= mid) { sl = query(t * 2, start, mid, L, R); } return sr + sl; } return 0; } int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { scanf_s("%d", &s[i]); } buildtree(1, n, 1); int q; cin >> q; while (q--) { int L, R; scanf_s("%d%d", &L, &R); cout << query(1, 1, n, L, R)/10<<endl; } return 0; }