Source Code
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define fixed(n) cout << fixed << setprecision(n);
#define mod 1000000007
#define cin(v) for (auto&i:v) cin >> i;
#define cout(v) for (auto&i:v) cout << i << " ";
#define ceil(n,m) (n / m + (n%m != 0))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

void lil_codi_vert(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    #endif
}

//int dx[] = {1, 1, 1, 0, 0, -1, -1, -1};
//int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};


int main(){
    lil_codi_vert();
    string s; cin >> s;
    ll q; cin >> q;
    map<pair<ll,ll>, ll> ma;
    while(q--){
        ll l, r; cin >> l >> r;
        if(ma.find({l,r})!= ma.end()){ cout << ma[{l,r}] << "\n";  continue;}
        l--;
        string k = s.substr(l, r-l);
        reverse(all(k));
        ll res = 0;
        for(int i = 0; i < k.size(); i++){
            if(i == 0){
                if(k[i] == '1') res += 1;
            }else{
                if(k[i] == '1'){
                    if(i%4 == 1) res += 2;
                    if(i%4 == 2) res += 4;
                    if(i%4 == 3) res += 8;
                    if(i%4 == 0) res += 6;
                }
                res %= 10;
            }
        }
        ma[{l+1,r}] = res;
        cout << res << "\n";

        
    }    
    return 0;
}
Copy
Binarithm aboelsoudJr
GNU G++17
2043 ms
968 KB
Time Limit Exceeded