Source Code
#include <bits/stdc++.h>
#define ld long double
#define ll long long
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sz(s)    (int)(s.size())
const ll N = 2e5+3, mod = 1000000007;
const ll OO = 6e18;
const auto EPS = 1e-6;
const auto PI = acos(-1);
using namespace std;
int DX[] = { 1, -1, 0, 0, -1, 1, 1, -1 };
int DY[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
int dx[] = { 1,0,-1,0 } ,dy[] = { 0,1,0,-1 };

ll mult(ll x, ll y)
{
	return ((x%mod)*(y%mod))%mod;
}
ll fpow(ll x, ll n)
{
	ll res = 1;
	while(n)
	{
		if(n&1)
		{
			res = mult(res,x);
		}
		n >>= 1LL;
		x = mult(x,x);
	}
	return res;
}

void testCase()
{
	string s;
	cin >> s;
	ll n = s.length();
	vector<ll>suff(n+7);
	for(ll i=n;i>=1;i--)
	{
		suff[i] = suff[i+1] + (s[i-1]-'0' ? fpow(2,n-i): 0);
	}
	ll q;
	cin >> q;
	while(q--)
	{
		ll l, r;
		cin >> l >> r;
		cout << (suff[l] / fpow(2,n-r) )%10<< '\n';
	}
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t = 1;  // cin >> t;
    for(int tc = 1;tc<=t;tc++)
    {
        //cout << "Case #" << tc << ": ";
        testCase();
    }
    return 0;
}
Copy
Binarithm MuhammedKhedr
GNU G++17
127 ms
2.7 MB
Wrong Answer