LOJ6278 block entry number of columns 2

LOJ6278 number of columns of block entry 2

label

  • Block entry

Foreword

Concise meaning of the questions

  • To a sequence, the need to support two operations:
    1. Plus range and
    2. C query interval is less than the number of the number of

Thinking

  • Well block of this type of process issues ~
  • First block, and then sorts each block within. A vector can save per piece.
  • For query: half-piece look, do not look at statistical piece of violence.
  • For modify operation: one-piece marking can not monolithic direct violence can modify the original array, then put the value of this piece re-import

Precautions

  • Note that the last element of a number! = Len. When dealing with the last piece to look special sentence.

to sum up

  • no

AC Code

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

const int maxn = 5e4 + 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 cnt = 0;
    for (int i = l; i <= min(pos[l] * len, r); i++)
        cnt += (a[i] + tag[pos[i]]) < c;

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

    for (int i = pos[l] + 1; i <= pos[r] - 1; i++)
        cnt += lower_bound(b[i].begin(), b[i].end(), c - tag[i]) - b[i].begin();

    return cnt;
}

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 * 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/11359842.html