Source Code
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define FAST ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define INF INT_MAX
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
#define nl "\n"
using namespace std;
const ll MOD  = 1e9+7;
const int mxN = 2e5+1;

int main()
{
    FAST
    //==================
    ll t = 1 ;
    //cin >> t;
    while(t--){
      string s; cin >> s;
      s = '9' + s;
      ll n = s.length()-1;
      ll a[4][n+2];
      memset(a , 0 , sizeof(a));
      for(int i=0;i<4;i++){
        for(int j=n;j>0;j--){
            if(s[j] == '1'){
                if(j == n)a[i][j] = 1 ;
                else{
                    int k =(n - j +i +1);
                    if(k%4 == 1){
                        a[i][j] = 6;
                    }else if(k%4 == 2){
                        a[i][j] = 2;
                    }else if (k%4 == 3){
                        a[i][j] = 4 ;
                    }else{
                        a[i][j] = 8;
                    }
                }
            }
        }
      }

      /*
      11111111111111111111
      */
      ll pref[4][n+1];
      memset(pref , 0 , sizeof(pref));
      for(int i=0;i<4;i++){
        pref[i][n+1] = 0;
      }
      for(int i=0;i<4;i++){
        for(int j=n ;j>0;j--){
            if(j == n){
                pref[i][j] = a[i][j];
            }else{
                pref[i][j] =a[i][j] + pref[i][j+1];
            }
        }
      }
    /*
      for(int i=0;i<4;i++){
        for(int j=1;j<=n;j++){
            cout << a[i][j] << " ";
        }cout << nl;
      }
      for(int i=0;i<4;i++){
        for(int j=1;j<=n;j++){
            cout << pref[i][j] << " ";
        }cout << nl;
      }
    */
      ll q; cin >> q;
      while(q--){
        ll  l , r; cin >> l >> r;
        ll p = (n - r)%4;
        if(p == 3) p =1 ;
        if(p ==1 )p =3;
        ll ans = 0 ;
        if(s[r] == '1'){
            ans-= pref[p][r] ;
            ans++;
        }else{
            ans = -pref[p][r+1];
        }
        cout << (pref[p][l] + ans)  %10 << nl;
      }


    }

}
Copy
Binarithm Imam Droubi
GNU G++17
127 ms
13.4 MB
Wrong Answer