Source Code
#include <bits/stdc++.h>
using namespace std;
#define f(i, x, n) for (int i = x; i < (int)(n); ++i)
#define fi(it, x) for (auto it = x.begin(); it != x.end(); ++it)
#define ll long long
#ifdef LOCAL
	#include "../cp-library/files/debug.h"
#else
	#define dug(...) 0
#endif

template <int M>
struct Mod{
	int x;
	Mod():x(){}
	template <class U>
	Mod(const U &o){
		if (o >= M || o < -M)x = o % M;
		else x = o;
		if (x < 0)x += M;
	}
	Mod& operator +=(const Mod &o) { if ((x += o.x) >= M)x -= M; return *this; }
	Mod& operator -=(const Mod &o) { if ((x -= o.x) <  0 )x += M; return *this; }
	Mod& operator *=(const Mod &o) { x = (ll)x * o.x % M; return *this; }
	Mod& operator /=(const Mod &o) { x /= o. x; return *this; }
	Mod& operator ++() { return *this += 1; }
	Mod& operator --() { return *this -= 1; }
	Mod  operator ++(int) { Mod an(*this); *this += 1; return an; }
	Mod  operator --(int) { Mod an(*this); *this -= 1; return an; }
	bool operator ==(const Mod &o)const { return x == o.x; }
	bool operator !=(const Mod &o)const { return x != o.x; }
	bool operator < (const Mod &o)const { return x <  o.x; }
	bool operator > (const Mod &o)const { return x >  o.x; }
	bool operator <=(const Mod &o)const { return x <= o.x; }
	bool operator >=(const Mod &o)const { return x >= o.x; }
};

template <int M> Mod<M> operator +(Mod<M> a, const Mod<M> &b) { return a += b; }
template <int M, class U> Mod<M> operator +(Mod<M> a, U b) { return a += b; }
template <int M, class U> Mod<M> operator +(U a, Mod<M> b) { return b += a; }
template <int M> Mod<M> operator -(Mod<M> a, const Mod<M> &b) { return a -= b; }
template <int M, class U> Mod<M> operator -(Mod<M> a, U b) { return a -= b; }
template <int M, class U> Mod<M> operator -(U a, Mod<M> b) { return b -= a; }
template <int M> Mod<M> operator *(Mod<M> a, const Mod<M> &b) { return a *= b; }
template <int M, class U> Mod<M> operator *(Mod<M> a, U b) { return a *= b; }
template <int M, class U> Mod<M> operator *(U a, Mod<M> b) { return b *= a; }
template <int M> Mod<M> operator /(Mod<M> a, const Mod<M> &b) { return a /= b; }
template <int M, class U> Mod<M> operator /(Mod<M> a, U b) { return a /= b; }
template <int M, class U> Mod<M> operator /(U a, const Mod<M> &b) { return Mod<M>(a) /= b; }
template <int M, class U> bool operator ==(U a, const Mod<M> &b) { return b == a; }
template <int M, class U> bool operator !=(U a, const Mod<M> &b) { return b != a; }
template <int M, class U> bool operator < (U a, const Mod<M> &b) { return b <  a; }
template <int M, class U> bool operator > (U a, const Mod<M> &b) { return b >  a; }
template <int M, class U> bool operator <=(U a, const Mod<M> &b) { return b <= a; }
template <int M, class U> bool operator >=(U a, const Mod<M> &b) { return b >= a; }
template <int M> istream& operator >>(istream &s, Mod<M> &x) { return s >> x.x; }
template <int M> ostream& operator <<(ostream &s, const Mod<M> &x) { return s << x.x; }

using Mint = Mod<10>;

//-----------------------------Mod-----------------------------------

int const N = 200000;
Mint pw[N + 1], x[N + 1];

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);

	string s;
	int q;
	cin >> s >> q;
	int n = s.size();
	s = string(1, 0) + s;
	pw[0] = 1;
	f(i, 1, n + 1)pw[i] = pw[i - 1] * 2;
	f(i, 1, n + 1)x[i] = x[i - 1] * 2 + (s[i] - '0');
	while (q--){
		int l, r;
		cin >> l >> r;
		cout << x[r] - x[l - 1] * pw[r - l + 1] << '\n';
	}
}
Copy
Binarithm Ma7moud.7amdy
GNU G++17
108 ms
2.6 MB
Accepted