二分解法
#include <bits/stdc++.h>
using namespace std;
vector<int> sum;
struct node
{
int x, y;
};
vector<node> ans1, ans2;
int minans = 1000000;
int main()
{
int n, m;
cin >> n >> m;
sum.resize(n + 1);
for(int i = 1; i <= n; i++)
{
cin >> sum[i];
sum[i] += sum[i - 1];
}
for(int i = 1; i <= n; i++)
{
int l = 1, r = n, mid;
while(l < r)
{
mid = l + (r - l >> 1);
if(sum[mid] - sum[i - 1] >= m)
r = mid;
else
l = mid + 1;
}
if(sum[l] - sum[i-1] == m)
ans1.push_back({i, l});
else if(sum[l] - sum[i-1] > m)
{
if(sum[l] - sum[i-1] <= minans)
{
if(sum[l] - sum[i-1] < minans)
{
minans = sum[l] - sum[i-1];
ans2.clear();
}
ans2.push_back({i,l});
}
}
}
if(!ans1.empty())
for(int i = 0; i < ans1.size(); i++)
cout << ans1[i].x << "-" << ans1[i].y << endl;
else
for(int i = 0; i < ans2.size(); i++)
cout << ans2[i].x << "-" << ans2[i].y << endl;
return 0;
}