#24-26 TLE
查看原帖
#24-26 TLE
226686
Herobrine6265楼主2023/11/16 10:20

rt

实在想不到怎么优化了

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct qujian
{
    int l, r;
};
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, q;
    cin >> n >> q;
    int a[n + 5];
    vector<qujian> point;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        if (a[i] <= 0)
            continue;
        int num = a[i];
        for (int j = i - 1;; j--)
        {
            num += a[j];
            if (num <= 0 || a[j] > 0 || j < 1)
            {
                point.push_back(qujian{j + 1, i});
                break;
            }
        }
    }
    int l, r, size = point.size();
    while (q--)
    {
        cin >> l >> r;
        unsigned long long ans = 0;
        for (int i = 0; i < size; i++)
        {
            if (point[i].r < l)
                continue;
            if (point[i].r > r)
                break;
            ans += point[i].r - max(point[i].l, l) + 1;
        }
        cout << ans << endl;
    }
    return 0;
}
2023/11/16 10:20
加载中...