Source Code
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<string>
#include<cmath>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#include<fstream>
#include<map>
#include<bitset>
#include<unordered_map>
#include<cstring>
#include<cctype>
using namespace std;
#define  ll long long
#define  ull unsigned long long
#define  ul unsigned long
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define sz(n) (int)n.size()
#define pb(s) push_back(s);
#define format(n) fixed<<setprecision(n)
#define finl "\n"
#define Mohammed_Atef_Hassan fast();
#define md 1000000007
#define INF LLONG_MAX
#define PI acos(-1)
#define clr(dp,n) memset(dp,n,sizeof dp)
#define ex(n) return cout<<n<<finl,0;
int dx[] = { 0, 1, 0, -1, 1, 1, -1, -1 };
int dy[] = { 1, 0, -1, 0, 1, -1, 1, -1 };
ll fix_mod(ll x, ll y)
{
    return (y + x % y) % y;
}
void fast()
{
    std::ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

#ifdef ONLINE_JUDGE
    //freopen("crosses.in", "r", stdin);

    //freopen("crosses.out", "w", stdout);
#endif
#ifndef ONLINE_JUDGE
    freopen("input.in", "r", stdin);
    //freopen("output.out", "w", stdout);
#endif
}
int main()
{
    Mohammed_Atef_Hassan
   string s;
   cin>>s;
   s="a"+s;
   vector<vector<ll>>v(s.size(),vector<ll>(4,0));
   for(int i=1;i<s.size();i++)
   {
       if(s[i]=='1')
       v[i][i%4]++;
       for(int j=0;j<4;j++)
        v[i][j]+=v[i-1][j];
   }
   int q;
   cin>>q;
   while(q--)
   {
       int l,r;
       cin>>l>>r;
       ll ans=0;
       vector<ll>cnt(4,0);
       for(int i=0;i<4;i++)
       {
           cnt[i]=v[r][i]-v[l-1][i];
       }
        ll x=v[r][r%4]-v[r-1][r%4];
        cnt[r%4]-=x;
        ans+=x;
        int cur=r%4;
        vector<ll>val={6,2,4,8};
        for(int i=0;i<4;i++)
        {
            ans+=val[i]*cnt[cur];
            cur--;
            cur+=4;
            cur%=4;
            ans%=10;
        }
        cout<<ans<<finl;
   }
}
Copy
Binarithm MuhammedAtefHassan
GNU G++17
209 ms
15.1 MB
Accepted