分享一个思路 int main() { int l = 0, r = 5, x = 1; vector<int> input = {0,1,2,1,3,1000}; int lastindex = l - 1; int ans = 0; for(int i = l; i <= r; ++i) { if(input[i] == x) { ans += (i - lastindex) * (r - i + 1); lastindex = i; } } ...