蒟蒻调不出来了,能不能帮忙调一下
查看原帖
蒟蒻调不出来了,能不能帮忙调一下
796389
xiansen楼主2024/9/29 01:17
#include <bits/stdc++.h>
using ll = long long;
#define int ll
#define lc u << 1
#define rc u << 1 | 1
#define endl '\n'
#define all(v) v.begin(), v.end()
using namespace std;
typedef pair<int, int> PII;
const int N = 1e5 + 5, mod = 1e9 + 7;
constexpr ll inf = 1e9;
template <class Info, class Tag>
struct LazySegmentTree
{
    const int n;
    vector<Info> info;
    vector<Tag> tag;
    LazySegmentTree(int n) : n(n), info((4 << __lg(n) + 1) + 5), tag((4 << __lg(n) + 1) + 5) {}
    LazySegmentTree(vector<Info> init) : LazySegmentTree(init.size() - 1)
    {
        function<void(int, int, int)> build = [&](int u, int l, int r)
        {
            if (r - l == 0)
            {
                info[u] = init[l];
                return;
            }
            int m = (l + r) / 2;
            build(lc, l, m);
            build(rc, m + 1, r);
            pull(u);
        };
        build(1, 0, n);
    }
    void pull(int u)
    {
        info[u] = info[lc] + info[rc];
    }
    void apply(int u, const Tag &v)
    {
        info[u].apply(v);
        tag[u].apply(v);
    }
    void push(int u)
    {
        apply(lc, tag[u]);
        apply(rc, tag[u]);
        tag[u] = Tag();
    }
    Info rangeQuery(int l, int r)
    {
        return rangeQuery(1, 0, n, l, r);
    }
    Info rangeQuery(int u, int l, int r, int x, int y)
    {
        if (l > y || r < x)
        {
            return Info();
        }
        if (l >= x && r <= y)
        {
            return info[u];
        }
        int m = (l + r) / 2;
        push(u);
        if (x <= m && y > m)
            return rangeQuery(lc, l, m, x, y) + rangeQuery(rc, m + 1, r, x, y);
        else if (x <= m)
            return rangeQuery(lc, l, m, x, y);
        else
            return rangeQuery(rc, m + 1, r, x, y);
    }
    void rangeApply(int l, int r, const Tag &v)
    {
        return rangeApply(1, 0, n, l, r, v);
    }
    void rangeApply(int u, int l, int r, int x, int y, const Tag &v)
    {
        if (l > y || r < x)
        {
            return;
        }
        if (l >= x && r <= y)
        {
            apply(u, v);
            return;
        }
        int m = (l + r) / 2;
        push(u);
        rangeApply(lc, l, m, x, y, v);
        rangeApply(rc, m + 1, r, x, y, v);
        pull(u);
    }
};
struct Tag
{
    ll add = -mod;
    void apply(Tag t)
    {
        if (t.add != -mod)
        {
            add = t.add;
        }
    }
};
struct Info
{
    ll sum = 0;
    ll act = 0;
    void apply(Tag t)
    {
        if (t.add != -mod)
        {
            sum = t.add * act;
        }
    }
};
Info operator+(Info a, Info b)
{
    Info c;
    c.sum = a.sum + b.sum;
    c.act = a.act + b.act;
    return c;
}
void solve()
{
    int n;
    cin >> n;
    vector<Info> a(n + 1, {-mod, 1});
    for (int i = 1; i <= n; i++)
    {
        int x;
        cin >> x;
        x -= i;
        a[i] = {x, 1};
    }
    LazySegmentTree<Info, Tag> tree(a);
    int q;
    cin >> q;
    ll ans = 0;
    while (q--)
    {
        int sta, end;
        cin >> sta >> end;
        int l, r;
        int loc = tree.rangeQuery(sta, sta).sum;
        if (loc <= end)
        {
            l = sta, r = n;
        }
        else
        {
            l = 0, r = sta;
        }
        while (l < r)
        {
            int mid = l + r + 1 >> 1;
            int tem = tree.rangeQuery(mid, mid).sum;
            if (tem < end - sta)
                l = mid;
            else
                r = mid - 1;
        }
        if (loc <= end)
        {
            ans += (l - sta + 1) * (end - sta) - tree.rangeQuery(sta, l).sum;
            tree.rangeApply(sta, l, {end - sta});
        }
        else
        {
            l++;
            ans += tree.rangeQuery(l, sta).sum - (sta - l + 1) * (end - sta);
            tree.rangeApply(l, sta, {end - sta});
        }
    }
    cout << ans << endl;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    // int ttt;
    // cin >> ttt;
    // while (ttt--)
    solve();
    return 0;
}
2024/9/29 01:17
加载中...