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 <=