Source Code
#include <bits/stdc++.h>

using namespace std;

#define fast ios::sync_with_stdio(NULL); cin.tie(0); cout.tie(0);
#define MAXN 200011
#define SQRSIZE 500

int arr[MAXN];
int block[SQRSIZE];
int n, blk_sz, pp = 1;

string s;
char c[MAXN];
void read(string &s)
{
    scanf("%s",c);
    s = c;
}

int query(int l, int r)
{
    int sum = 0;
    int idx = n - r;
    int p = 1;
    while (l < r && idx % blk_sz != 0)
    {
        sum = (sum + arr[r] * p) % 10;
        r--;
        idx++;
        p = (p * 2) % 10;
    }
    while (l + blk_sz - 1 <= r)
    {
        sum = (sum + block[idx / blk_sz] * p) % 10;
        p = (p * pp) % 10;
        r -= blk_sz;
        idx += blk_sz;
    }
    while (l<=r)
    {
        sum = (sum + arr[r] * p) % 10;
        p = (p * 2) % 10;
        r--;
    }
    return sum;
}

void preprocess()
{
    int blk_idx = -1;

    blk_sz = sqrt(n);

    int p = 1;
    for(int i = n, j = 0; i > 0; i--, j++)
    {
        arr[i] = s[i] - '0';
        if (j % blk_sz == 0)
        {
            p = 1;
            blk_idx++;
        }
        block[blk_idx] += (arr[i] * p) % 10;
        p = (p * 2) % 10;
    }
}

int q, l, r;

int main()
{
    read(s);
    n = s.size();
    s = " " + s;
    preprocess();
    for(int i = 0; i < blk_sz; i++) pp = (pp * 2) % 10;
    scanf("%d", &q);
    while(q--)
    {
        scanf("%d%d", &l, &r);
        printf("%d\n", query(l, r));
    }
}
Copy
Binarithm Mohamad_Abodan
GNU G++17
1529 ms
2.0 MB
Accepted