题目:#6278
求助代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n;
int a[N];
int b[N], st[N], ed[N], lazy[N];
void update(int x, int y, int k){
if(b[x] == b[y]){
for(int i = x; i <= y; i++){
a[i] += k;
}
sort(a + st[b[x]], a + st[b[x]] + ed[b[x]]);
}
else{
for(int i = x; i <= ed[b[x]]; i++){
a[i] += k;
}
sort(a + st[b[x]], a + st[b[x]] + ed[b[x]]);
for(int i = st[b[y]]; i <= y; i++){
a[i] += k;
}
sort(a + st[b[y]], a + st[b[y]] + ed[b[y]]);
for(int i = b[x] + 1; i <= b[y] - 1; i++){
lazy[i] += k;
}
}
}
int query(int x, int y, int k){
int sum = 0;
if(b[x] == b[y]){
for(int i = x; i <= y; i++){
if(lazy[i] + a[i] < k){
sum++;
}
}
}
else{
for(int i = x; i <= ed[b[x]]; i++){
if(lazy[i] + a[i] < k){
sum++;
}
}
for(int i = st[b[y]]; i <= y; i++){
if(lazy[i] + a[i] < k){
sum++;
}
}
for(int i = b[x] + 1; i <= b[y] - 1; i++){
sum += lower_bound(a + st[i], a + st[i] + ed[i], k - lazy[i]) - a - st[i];
}
}
return sum;
}
int main(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
int sq = sqrt(n);
for (int i = 1; i <= sq; i++){
st[i] = n / sq * (i - 1) + 1;
ed[i] = n / sq * i;
}
ed[sq] = n;
for(int i = 1; i <= sq; i++){
sort(a + st[i], a + ed[i]);
}
for(int i = 1; i <= sq; i++){
for(int j = st[i]; j <= ed[i]; j++){
b[j] = i;
}
}
while(n--){
int op, l, r, c;
cin >> op >> l >> r >> c;
if(op == 0){
update(l, r, c);
}
else{
cout << query(l, r, c * c) << '\n';
}
}
return 0;
}