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

using namespace std;
typedef long long ll;
#define x           first
#define y           second
#define all(v)      v.begin(),v.end()
#define clr(v,d)    memset(v,d,sizeof(v));

const int N = 2e5 + 10;

const ll mod = 1e9 + 7;

int n ;

ll pr[4][N];
int v[] = {2,4,8,6} ;
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);

    string s ;
    cin >> s ;
    n = s.size() - 1;
    int po[]= {0,1,2,3};
    for(int i = n ; i >= 0 ; i--)
    {
        for (int j = 0; j < 4; ++j) {
            pr[ j ][ i+1 ] = v[po[j]]*(s[i] - '0') + pr[j][i+2];
            pr[j][i+1] %= 10 ;
            po[j]++;
            po[j] %= 4 ;
        }
    }

    //cout << n << " " << pr[0][n+1] << " " << pr[1][n+1] << " " << pr[2][n+1] << " " << pr[3][n+1] ;
    //return 0 ;
    int q ;
    cin >> q;
    while(q--)
    {
        int l , r ;
        cin >> l >> r;

        if(l == r) cout << s[l-1] << "\n" ;
        else
        {
            r--;
            int f = (8 - ((n+1 - r)%4))%4 ;

            cout << (pr[f][l] - pr[f][r+1] + s[r]- '0' + 10)%10 << "\n" ;
        }
    }

    return 0;
}
Copy
Binarithm MuhammadHassan
GNU G++17
122 ms
7.3 MB
Accepted