LOJ block entry columns 5

LOJ block entry columns 5

topic:

answer:

  • Prescribing . After prescribing 2.1 billion times becomes 1.
  • So for the discrete pieces, prescribing violence, violence statistics.
  • For the piece, just 1/0 block inside is not all, violence is prescribing, val statistics.
  • Seemingly high complexity, but in reality each radicand 5, O (5n + x). 5n only one more than the complexity of the range to find
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#define N 50005
using namespace std;

struct Blo {int l, r, tag, val;} blo[N];
int n, size, num;
int a[N], bel[N];

int read() {
    int x = 0, f = 1; char c = getchar();
    while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
    while (c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
    return x *= f; 
}

void build() {
    size = (int)sqrt(n), num = n / size;
    if (n % size) num++;
    for (int i = 1; i <= n; i++) bel[i] = (i - 1) / size + 1;
    for (int i = 1; i <= num; i++) {
        blo[i].l = (i - 1) * size + 1;
        blo[i].r = i * size;
    }
    blo[num].r = n;
    for (int i = 1; i <= num; i++)
        for (int j = blo[i].l; j <= blo[i].r; j++)
            blo[i].val += a[j];
}

void reset1(int x, int l, int r) {
    for (int i = l; i <= r; i++) {
        blo[x].val -= a[i];
        a[i] = (int)sqrt(a[i]);
        blo[x].val += a[i];
    }
}

void update(int l, int r) {
    if (bel[l] == bel[r]) {reset1(bel[l], l, r); return;}
    reset1(bel[l], l, blo[bel[l]].r);
    reset1(bel[r], blo[bel[r]].l, r);
    for (int i = bel[l] + 1; i < bel[r]; i++)
        if (!blo[i].tag) {
            int tot = 0;
            reset1(i, blo[i].l, blo[i].r);
            for (int j = blo[i].l; j <= blo[i].r; j++)
                if (a[j] == 1 || a[j] == 0) tot++;
            if(tot == blo[i].r - blo[i].l + 1) blo[i].tag = 1;
        }
}

int reset2(int x, int l, int r) {
    int ans = 0;
    for (int i = l; i <= r; i++) ans += a[i];
    return ans;
}

int ask(int l, int r) {
    int ans = 0;
    if (bel[l] == bel[r]) return reset2(bel[l], l, r);
    ans += reset2(bel[l], l, blo[bel[l]].r);
    ans += reset2(bel[r], blo[bel[r]].l, r);
    for (int i = bel[l] + 1; i < bel[r]; i++) ans += blo[i].val;
    return ans;
}

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) a[i] = read();
    build();
    for (int i = 1; i <= n; i++) {
        int op = read(), l = read(), r = read(), c = read();
        if (!op) update(l, r);
        else printf("%d\n", ask(l, r));
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/BigYellowDog/p/11243092.html