Source Code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;

#define forr(_a, _b, _c) for(_a = (_b); _a <= (_c); ++_a)
#define ford(_a, _b, _c) for(_a = (_b) + 1; _a --> (_c);)
#define forf(_a, _b, _c) for(_a = (_b); _a < (_c); ++_a)
#define mp make_pair
#define fi first
#define se second
#define vi vector
#define sz(_v) _v.begin(), _v.end()
#define mask(_x) (1ll << (_x))
#define bit(_x,_y) (((_x) >> (_y)) & 1)

string yes[2] = {"NO\n","YES\n"};
const ld eps = ld(1e-7);
const ll inf = ll(1e16) + 1;
const ll mod = ll(1e9) + 7;

const int N = 2e5 + 5;
int n, i, q, x, y, mu[N] = {1}, it[N << 2];
string s;

void build(int vt, int l, int r) {
	if(l == r) {
		it[vt] = (s[l] == '1');
		return;
	}
	int m = (l + r) >> 1;
	build(vt << 1,l,m);
	build(vt << 1 | 1,m + 1,r);
	it[vt] = (it[vt << 1] * mu[r - m] + it[vt << 1 | 1]) % 10;
}

int get(int vt, int l, int r, int u, int v) {
	if(l > r || r < u || v < l || u > v) return 0;
	if(u <= l && r <= v) return (it[vt] * mu[v - r]) % 10;
	int m = (l + r) >> 1;
	int L = get(vt << 1,l,m,u,v);
	int R = get(vt << 1 | 1,m + 1,r,u,v);
	return (L + R) % 10;
}

void solve() {
	cin >> s >> q;
	n = s.size();
	s = ' ' + s;
	forr(i,1,n) {
		mu[i] = (mu[i - 1] << 1) % 10;
	}
	build(1,1,n);
	while(q--) {
		cin >> x >> y;
		cout << get(1,1,n,x,y) << "\n";
	}
}

void precalc() {
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	#ifdef umf
		freopen("test.inp","r",stdin);
		freopen("test.out","w",stdout);
	#endif

	int tc = 1;
	// cin >> tc;
	precalc();
	while(tc --> 0) {
		solve();
	}

	return 0;
}
Copy
Binarithm thqdothabit
GNU G++17
153 ms
4.7 MB
Accepted