LOJ6279 block entry columns 3

LOJ6279 number of columns of the block entry 3

label

  • Block entry

Foreword

  • I made some stupid mistake, debug for a long time ~

Concise meaning of the questions

  • Maintenance sequence, supports two modes of operation:
    1. Plus range
    2. The precursor of a number of inquiries

Thinking

  • This problem and the number of columns blocking entry 2 almost all the same, it is time to change the query a little bit on it QAQ
  • Concrete is a direct-half piece, no piece of violence to find, and then take the largest on the line

Precautions

  • We must pay attention to details. Do not forget to add a tag

to sum up

  • Block fun and simple QAQ, why not learn it as soon as possible

AC Code

#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;

const int maxn = 1e5 + 10;

int n, a[maxn];
int pos[maxn], len, tag[maxn];
vector<int> b[maxn];

void reset(int id)
{
    b[id].clear();
    for (int i = (id - 1) * len + 1; i <= min(id * len, n); i++)
        b[id].push_back(a[i]);
    sort(b[id].begin(), b[id].end());
}

void change(int l, int r, int c)
{
    for (int i = l; i <= min(pos[l] * len, r); i++)
        a[i] += c;
    reset(pos[l]);

    if (pos[l] != pos[r])
    {
        for (int i = r; i >= (pos[r] - 1) * len + 1; i--)
            a[i] += c;
        reset(pos[r]);
    }

    for (int i = pos[l] + 1; i <= pos[r] - 1; i++)
        tag[i] += c;
}

int cal(int l, int r, int c)
{
    int ans = -1;

    for (int i = l; i <= min(pos[l] * len, r); i++)
        if (a[i] + tag[pos[i]] < c)
            ans = max(ans, a[i] + tag[pos[i]]);

    if (pos[l] != pos[r])
        for (int i = r; i >= max((pos[r] - 1) * len + 1, l); i--)
            if (a[i] + tag[pos[i]] < c)
                ans = max(ans, a[i] + tag[pos[i]]);

    for (int i = pos[l] + 1; i <= pos[r] - 1; i++)
    {
        auto t = lower_bound(b[i].begin(), b[i].end(), c - tag[i]);
        if (t != b[i].begin() && *(t - 1) + tag[i] < c)
            ans = max(ans, *(t - 1) + tag[i]);
    }

    return ans;
}

void solve()
{
    scanf("%d", &n);
    len = sqrt(n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &a[i]), b[pos[i] = (i - 1) / len + 1].push_back(a[i]);

    //预处理
    for (int i = 1; i <= pos[n]; i++)
        sort(b[i].begin(), b[i].end());

    for (int i = 1; i <= n; i++)
    {
        int opt, l, r, c;
        scanf("%d%d%d%d", &opt, &l, &r, &c);
        if (opt == 0)
            change(l, r, c);
        else
            printf("%d\n", cal(l, r, c));
    }
}

int main()
{
    freopen("Testin.txt", "r", stdin);
    freopen("Testout.txt", "w", stdout);
    solve();
    return 0;
}

Guess you like

Origin www.cnblogs.com/danzh/p/11360365.html