跳到正文

个人记录

【三月份 -- 基础语法组】-- T4 --区间 interval

zhangyuqiTAT约 3 分钟
原文
文章信息与版本

文章信息

发布
2025年4月12日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 3 分钟

版本

  1. v1当前

网址: https://cspjs.online/contest/660/problem/4

题解: https://b1bznc26af6.feishu.cn/file/QXq8bBcUSoxP5IxnzExciT2invg?from=from_copylink

C++
#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;
}
原文