1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| #include <iostream> #include <algorithm> #include <cstring> #include <cmath> #include <vector> #define int long long using namespace std; int n; const int N = 1e5 + 7; int add[N], a[N], pos[N], L[N], R[N], b[N]; vector<int > arr[N]; void reset(int x) { arr[x].clear();
for (int i = L[x]; i <= R[x]; i ++) arr[x].push_back(a[i]);
sort(arr[x].begin(), arr[x].end()); } void change(int l, int r, int d) { int p = pos[l], q = pos[r];
if (p == q) { for (int i = l; i <= r; i ++) a[i] += d;
reset(q); } else { for (int i = p + 1; i <= q - 1; i ++) add[i] += d;
for (int i = l; i <= R[p]; i ++) a[i] += d;
reset(p);
for (int i = L[q]; i <= r; i ++) a[i] += d;
reset(q); } } int ask(int l, int r, int d) { int ans = 0; int p = pos[l], q = pos[r];
if (p == q) { for (int i = l; i <= r; i ++) if (a[i] + add[p] < d) ans++; } else { for (int i = l; i <= R[p]; i ++) if (a[i] + add[p] < d) ans++;
for (int i = L[q]; i <= r; i ++) if (a[i] + add[q] < d) ans++;
for (int i = p + 1; i <= q - 1; i ++) { int po = lower_bound(arr[i].begin(), arr[i].end(), d - add[i]) - arr[i].begin(); ans += po ; } }
return ans; }
signed main() { cin >> n;
for (int i = 1; i <= n; i ++) scanf("%lld", &a[i]);
int t = sqrt(n); memcpy(b, a, sizeof a);
for (int i = 1; i <= t; i ++) { L[i] = (i - 1) * t + 1; R[i] = i * t; }
if (R[t] < n) t++, L[t] = R[t - 1] + 1, R[t] = n;
for (int i = 1; i <= t; i ++) { for (int j = L[i]; j <= R[i]; j ++) arr[i].push_back(a[j]);
sort(arr[i].begin(), arr[i].end()); }
for (int i = 1; i <= t; i ++) for (int j = L[i]; j <= R[i]; j ++) { pos[j] = i; }
for (int i = 1; i <= n; i ++) { int op, l, r, d; scanf("%lld%lld%lld%lld", &op, &l, &r, &d);
if (op == 0) { change(l, r, d); } else { printf("%lld\n", ask(l, r, d * d)); } } }
|