悬关!求大佬调调 调了一个多小时没调出来
查看原帖
悬关!求大佬调调 调了一个多小时没调出来
526922
coool楼主2023/8/31 11:47
#include <bits/stdc++.h>
using namespace std;

int n;

stack<long long> s;
stack<long long> sl;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> n;
    while (n--)
    {
        int op, ans = 0;
        cin >> op;
        if (op == 1)
        {
            int l, r;
            cin >> l >> r;
            sl.push(l);
            s.push(r);
        }
        else if (op == 2)
        {
            int k, ll, rr;
            cin >> k;
            ll = sl.top();
            rr = s.top();

            if (!s.empty() && rr - k + 1 >= ll)
            {
                int bg = rr - k + 1;
                int ed = rr;
                ans = (bg + ed) * k / 2;
                s.pop();
                s.push(bg - 1);
            }

            else
            {
                while (!s.empty() && !sl.empty() && rr - k + 1 < ll)
                {
                    ll = sl.top();
                    rr = s.top();
                    int t = (rr - ll + 1);
                    ans += (ll + rr) * t / 2;
                    // cout << ll << " " << rr << " " << t << " " << ans << " ";
                    k -= (rr - ll + 1);
                    sl.pop();
                    s.pop();
                }

                if (!s.empty())
                {
                    rr = s.top();
                    int bg = rr - k + 1;
                    int ed = rr;
                    ans += (bg + ed) * k / 2;

                    s.pop();

                    s.push(bg - 1);
                }
            }

            cout << ans << endl;
        }
    }
    return 0;
}
2023/8/31 11:47
加载中...