网址: https://cspjs.online/contest/660/problem/4
题解: https://b1bznc26af6.feishu.cn/file/QXq8bBcUSoxP5IxnzExciT2invg?from=from_copylink
#include <bits/stdc++.h>
using namespace std;
int n, m, f[3010][3010][3];
string s;
void init() {
cin >> n >> m >> s;
s = "s" + s;
return;
}
void ini() {
int i = 1;
while (i < n) {
int j = i, cnt0 = 0, cnt1 = 0, cntr = 0;
while (j <= n) {
if (s[j] == '0') {
f[i][j][1] += (cnt0 + cntr);
f[i][j][2] += cnt1;
++cnt0;
} else {
if (s[j] == '1') {
f[i][j][0] += (cntr + cnt1);
f[i][j][2] += cnt0;
++cnt1;
} else {
f[i][j][0] += (cntr + cnt1);
f[i][j][1] += cnt0;
++cntr;
}
}
f[i][j][0] += f[i][j - 1][0];
f[i][j][1] += f[i][j - 1][1];
f[i][j][2] += f[i][j - 1][2];
++j;
}
++i;
}
i = 1;
return;
}
void solve() {
init();
ini();
while (m--) {
int l, r, k;
cin >> l >> r >> k;
if (k > 2 || l == r)
cout << 0 << '\n';
else
cout << f[l][r][k] << '\n';
}
return;
}
int main() {
freopen("interval.in", "r", stdin);
freopen("interval.out", "w", stdout);
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}