分块入门 LOJ

LOJ  分块入门:

 

1. 分块入门 1

#include<bits/stdc++.h>
using namespace std;
const int N = 5e4 + 100;
int n, blo, a[N], b[N], tag[N];
void modify(int l, int r, int c) {
    for (int i = l; i <= min(r, b[l]*blo); ++i)
        a[i] += c;
    if (b[l] != b[r])
        for (int i = (b[r] - 1) * blo + 1; i <= r; ++i)
            a[i] += c;
    for (int i = b[l] + 1; i <= b[r] - 1; ++i)
        tag[i] += c;
}
int main() {
    int opt, l, r, c;
    scanf("%d", &n);
    blo = sqrt(n);
    for (int i = 1; i <= n; ++i)
        scanf("%d", &a[i]);
    for (int i = 1; i <= n; ++i)
        b[i] = (i - 1) / blo + 1;

    for (int i = 0; i < n; ++i) {
        scanf("%d%d%d%d", &opt, &l, &r, &c);
        if (opt == 0) {
            modify(l, r, c);
        } else {
            printf("%d\n", a[r] + tag[b[r]]);
        }
    }

    return 0;
}

1. 分块入门2

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100;
int n, m, blo, bl[N], a[N], tag[N], id[N];
int ans;
vector<int>vec[510];
void reset(int x) {
    vec[x].clear();
    for (int i = (x - 1) * blo + 1; i <= min(x * blo, n); ++i)
        vec[x].push_back(a[i]);
    sort(vec[x].begin(), vec[x].end());
}

void modify(int x, int y, int z) {
    for (int i = x; i <= min(y, bl[x]*blo); ++i)
        a[i] += z;
    reset(bl[x]);
    if (bl[x] != bl[y]) {
        for (int i = (bl[y] - 1) * blo + 1; i <= y; ++i)
            a[i] += z;
        reset(bl[y]);
    }
    for (int i = bl[x] + 1; i <= bl[y] - 1; ++i)
        tag[i] += z;
}

int query(int x, int y, int z) {
    int ans = 0;
    for (int i = x; i <= min(y, (bl[x]*blo)); ++i)
        if (a[i] + tag[bl[x]] < z) ans++;
    if (bl[x] != bl[y])
        for (int i = (bl[y] - 1) * blo + 1; i <= y; ++i)
            if (a[i] + tag[bl[y]] < z) ans++;

    for (int i = bl[x] + 1; i <= 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值