LOJ#6278. 数列分块入门 2
在一个区间上进行操作,一种操作是某个小区间都加上c,另一个查找这个区间内大于c*c的数
我们可以另外开一个数组在保存a中的每个分块内的相对值,然后每次对a加值,并把a的值赋给b,不同的是b内的各个分块需要进行排序,然后通过二分找区间的整块内的小于c*c的值,如果是在区间的边上,不是一个整块,那就直接暴力搜一下
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<map> #include<set> #include<ctime> #include<cmath> #include<stack> #include<queue> #include<string> #include<vector> #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #define first fi #define second se #define lowbit(x) (x & (-x)) typedef unsigned long long int ull; typedef long long int ll; const double pi = 4.0*atan(1.0); const int inf = 0x3f3f3f3f; const int maxn = 51005; const int maxm = 305; using namespace std; int n, m, tol, T; int block; int bel[maxn]; int a[maxn]; int b[maxn]; int add[maxn]; void init() { memset(a, 0, sizeof a); memset(add, 0, sizeof add); memset(bel, 0, sizeof bel); } void sor(int x) { for(int i=(x-1)*block+1; i<=min(x * block, n); i++) { a[i] += add[x]; b[i] = a[i]; } // printf("%d %d\n", (x-1)*block+1, b+min(x * block, n)+1); sort(b+(x-1)*block+1, b+min(x * block, n)+1); add[x] = 0; } void update(int l, int r, int c) { for(int i=l; i<=min(r, bel[l]*block); i++) a[i] += c; sor(bel[l]); if(bel[l] == bel[r]) return ; if(bel[l] != bel[r]) { for(int i=(bel[r]-1) * block+1; i<=r; i++) a[i] += c; sor(bel[r]); } for(int i=bel[l]+1; i<bel[r]; i++) add[i] += c; } int query(int l, int r, int c) { int ans = 0; for(int i=l; i<=min(r, bel[l] * block); i++) if(a[i] + add[bel[i]] < c) ans++; if(bel[l] == bel[r]) return ans; if(bel[l] != bel[r]) for(int i=(bel[r]-1)*block+1; i<=r; i++) if(a[i] + add[bel[i]] < c) ans++; for(int i=bel[l]+1; i<bel[r]; i++) ans += lower_bound(b+(i-1)*block+1, b+min(n, i*block+1), c - add[i]) - (b + (i-1)*block+1); return ans; } int main() { while(~scanf("%d", &n)) { init(); block = sqrt(n); int num = n / block; if(n % block) num++; for(int i=1; i<=n; i++) { scanf("%d", &a[i]); bel[i] = (i-1) / block + 1; } for(int i=1; i<=num; i++) sor(i); // for(int i=1; i<=n; i++) printf("%d%c", a[i], i==n ? '\n' : ' '); m = n; while(m--) { int op, l, r, c; scanf("%d%d%d%d", &op, &l, &r, &c); if(op == 0) { update(l, r, c); // for(int i=1; i<=n; i++) printf("%d%c", a[i], i==n ? '\n' : ' '); } else { int ans = query(l, r, c*c); printf("%d\n", ans); } } } return 0; }