Source Code
//#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
  
#define ordered_set tree<pair<int,int>, null_type,less<pair<int,int>>, rb_tree_tag,tree_order_statistics_node_update>
  
void Fast()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}
void File() {
#ifndef ONLINE_JUDGE
    freopen("Input.txt", "r", stdin);
    freopen("Output.txt", "w", stdout);
#endif
}
typedef long long ll;
void justify(int n,int x)
{
    string s=to_string(n);
    for(int i=0;i<x-s.size();i++)cout<<' ';
    cout<<s;
}
ll dp[402][402][2];
int test,d,vis[402][402][2],n,a[402];
ll enh(ll x)
{
    return (x%d+d)%d;
}
ll rec(int i,int j,bool k,int last)
{
    if(i==1&&j==n)return min(d-last,last);
    ll &rt=dp[i][j][k];
    if(vis[i][j][k]==test)
        return rt;
    vis[i][j][k]=test;
    rt=LLONG_MAX;
    
    
    if(i!=1)
    {
        ll x=min(enh(last-a[i-1]),enh(last-a[i-1]-d));
        rt=rec(i-1,j,0,a[i-1])+x*(j-i+1);
    }
    if(j!=n)
    {
        ll y=min(enh(last-a[j+1]),enh(last-a[j+1]-d));
        rt=min(rt,rec(i,j+1,1,a[j+1])+y*(j-i+1));
    }
    
    return rt;
}
void solve()
{
    int q,l,r,pr[200002][4]={0};
    string s;
    cin>>s>>q;
    int res[4]={6,2,4,8};
    reverse(s.begin(),s.end());
    for (int i = 1; i < s.size(); i++)
    {
        if(s[i]=='1')
            pr[i][i%4]=1;
        
        for (int j = 0; j < 4; j++)
            pr[i][j]+=pr[i-1][j];
    }
    
    while (q--)
    {
        int x=0;
        cin>>l>>r;
        x=s.size()-l;
        l=s.size()-r;
        r=x;
        x=0;
        if(s[l]=='1')x++;
        l++;
        if(r>=l)
        {
            for (int j = 0; j < 4; j++)
            {
                int i=(j+l-1)%4;
                x+=(pr[r][i]-pr[l-1][i])*res[j];
            }
        }
        cout<<x%10<<'\n';
    }

    
}
int main()
{
    File();
    Fast();
    
    int t=1;
  //cin>>t;
    for (int i = 1; i <= t;i++ )
    {
        //test=i;
        //cout<<"Case #"<<i<<": ";
        solve();
    }

}
Copy
Binarithm Ahmed_Aladdin
GNU G++17
117 ms
4.3 MB
Accepted