Source Code
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <map>
#define ll long long
#define endl '\n'
#define Pi 3.1415926535898
const int N = 2e6 + 5;
#define watch(x) cout << #x << " = " << x << endl;
#define all(v) (v).begin(),(v).end()
using namespace std;
// cout << fixed << setprecision(9);
void fast() {
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
}
void file() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
}
int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 }; // dir array
int dy[] = { 1, -1, 0, 0, 1, -1, -1, 1 };

int main() {
	fast();
	file();
	// cout << (1 << 5);
	//bitset<32> bits("01110101");
	// cout << bits.to_ulong() << endl;
	string s;
	cin >> s;
	int sz = s.size();
	vector <ll> v(sz + 1);
	v[0] = 0;
	for (int i = 0;i < sz;i++) {
		if (s[i] == '0') {
			v[i + 1] = (v[i] * 2) % 10;
		}
		else {
			v[i + 1] = (((v[i] * 2) % 10) + 1) % 10;
		}
	}
	//for (auto it : v) cout << it << " "; cout << endl; 
	int q;
	cin >> q;
	while (q--) {
		ll l, r;
		cin >> l >> r;
		if (l == 1)
			cout << v[r] << endl;
		else {
			ll dis = r - l + 1;
			ll a = 0;
			if (dis % 4 == 1) a = 2;
			else if (dis % 4 == 2) a = 4;
			else if (dis % 4 == 3) a = 8;
			else if (dis % 4 == 0) a = 6;
			ll x = (v[l - 1] * a) % 10;
			if (v[r] - x < 0) {
				cout << ((v[r] - x) + 10) % 10 << endl;
			}
			else {
				cout << v[r] - x << endl; 
			}
		}
	}
	return 0;
}
Copy
Binarithm Marah Mohamed
GNU G++17
113 ms
2.6 MB
Accepted