需要思考的问题:
- 完整的块如何处理
- 不完整的块如何处理
- 需要预处理什么东西
题解
-
预处理
- 对于每一块,我们用一个vector保存这个块中所有的值,并且对这个vector排序。目的是为了方便我们后面的二分操作。
-
对于完整的块
- 更新:我们用 l z [ i ] lz[i] lz[i]保存第i块整块要加的值。
- 查询: l o w e r lower lower_ b o u n d bound bound查询这个块中小于c减lz[i]的值的个数,因为本来这个块中所有的数都要加lz[i]。
-
对于不完整的块
- 更新:直接更新,然后把这个块对应的vector清空,然后再加入新的值。
- 查询:直接查询,注意不是小于c,而是小于c-lz[i]
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 5e4 + 10;
int a[maxn];
vector<int> k[305];
int n, block, num;
int belong[maxn], l[305], r[305], lz[305];
void init() {
block = sqrt(n);
num = n / block + (n % block != 0);
for (int i = 1; i <= n; i++) belong[i] = (i - 1) / block + 1;
for (int i = 1; i <= num; i++) {
l[i] = (i - 1) * block + 1;
r[i] = i * block;
}
r[num] = n;
for (int i = 1; i <= n; i++) {
k[belong[i]].push_back(a[i]);
}
for (int i = 1; i <= num; i++) sort(k[i].begin(), k[i].end());
}
void reset(int p) {
k[p].clear();
for (int i = l[p]; i <= r[p]; i++) k[p].push_back(a[i]);
sort(k[p].begin(), k[p].end());
}
void up(int L, int R, int c) {
if (belong[L] == belong[R]) {
for (int i = L; i <= R; i++) a[i] += c;
reset(belong[L]);
return;
}
for (int i = L; i <= r[belong[L]]; i++) a[i] += c;
reset(belong[L]);
for (int i = l[belong[R]]; i <= R; i++) a[i] += c;
reset(belong[R]);
for (int i = belong[L] + 1; i < belong[R]; i++) lz[i] += c;
}
int qu(int L, int R, ll c) {
int ans = 0;
if (belong[L] == belong[R]) {
for (int i = L; i <= R; i++)
if (a[i] + lz[belong[L]] < c)
ans++;
return ans;
}
for (int i = belong[L] + 1; i < belong[R]; i++) {
ans += lower_bound(k[i].begin(), k[i].end(), c - lz[i]) - k[i].begin();
}
for (int i = L; i <= r[belong[L]]; i++)
if (a[i] + lz[belong[L]] < c)
ans++;
for (int i = l[belong[R]]; i <= R; i++)
if (a[i] + lz[belong[R]] < c)
ans++;
return ans;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
init();
for (int i = 1; i <= n; i++) {
int op, L, R, c;
cin >> op >> L >> R >> c;
if (op == 1) {
cout << qu(L, R, c * c) << '\n';
} else {
up(L, R, c);
}
}
}