基础模板(区间修改,求区间和)
#include "bits/stdc++.h"
using namespace std;
#define int long long
const int N = 100010;int sum[N],add[N],a[N],b[N];
int l[N],r[N];void change(int l1,int r1,int k)
{int p=b[l1],q=b[r1];if(p==q){for (int i=l1;i<=r1;i++){a[i]+=k;sum[p]+=(r1-l1+1)*k;}}else {for (int i=p+1;i<=q-1;i++){add[i]+=k;}for (int i=l1;i<=r[p];i++){a[i]+=k;}sum[p]+=(r[p]-l1+1)*k;for (int i=l[q];i<=r1;i++){a[i]+=k;}sum[q]+=(r1-l[q]+1)*k; }}void ask(int l1,int r1)
{int p=b[l1],q=b[r1];int ans=0;if(p==q){for (int i=l1;i<=r1;i++){ans+=a[i];}ans+=add[p]*(r1-l1+1);}else {for (int i=l1;i<=r[p];i++){ans+=a[i];}ans+=add[p]*(r[p]-l1+1);for (int i=l[q];i<=r1;i++){ans+=a[i];}ans+=add[q]*(r1-l[q]+1);for (int i=p+1;i<=q-1;i++){ans+=sum[i];ans+=add[i]*(r[i]-l[i]+1);}}cout<<ans<<endl;}signed main()
{int n,m;cin>>n>>m;int len=sqrt(n);//长度为sqrt(n);for (int i=1;i<=n;i++){cin>>a[i];}for (int i=1;i<=len;i++){l[i]=(i-1)*len+1;r[i]=i*len;}if(r[len]<n){len++;l[len]=r[len-1]+1;r[len]=n;}for (int i=1;i<=len;i++){for (int j=l[i];j<=r[i];j++){b[j]=i;sum[i]+=a[j];}}while(m--){int o,x,y,k;cin>>o;if(o==1){cin>>x>>y>>k;change(x,y,k);}else {cin>>x>>y;ask(x,y);}}}
区间修改,区间大于某值的数量
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int n,q,m,block;
int a[1000001],b[1000001],pos[1000001],add[1000001];
void reset(int x)
{int l=(x-1)*block+1,r=min(x*block,n);for(int i=l;i<=r;i++)b[i]=a[i];sort(b+l,b+r+1);
}
int find(int x,int v)
{int l=(x-1)*block+1,r=min(x*block,n);int last=r;while(l<=r){int mid=(l+r)>>1;if(b[mid]<v)l=mid+1;else r=mid-1;}return last-l+1;
}
void update(int x,int y,int v)
{if(pos[x]==pos[y]){for(int i=x;i<=y;i++)a[i]=a[i]+v;}else {for(int i=x;i<=pos[x]*block;i++)a[i]=a[i]+v;for(int i=(pos[y]-1)*block+1;i<=y;i++)a[i]=a[i]+v;}reset(pos[x]);reset(pos[y]);for(int i=pos[x]+1;i<pos[y];i++)add[i]+=v;
}
int query(int x,int y,int v)
{int sum=0;if(pos[x]==pos[y]){for(int i=x;i<=y;i++)if(a[i]+add[pos[i]]>=v)sum++;}else {for(int i=x;i<=pos[x]*block;i++)if(a[i]+add[pos[i]]>=v)sum++;for(int i=(pos[y]-1)*block+1;i<=y;i++)if(a[i]+add[pos[i]]>=v)sum++;}for(int i=pos[x]+1;i<pos[y];i++)sum+=find(i,v-add[i]);return sum;
}
int main()
{scanf("%d%d",&n,&q);block=int(sqrt(n));for(int i=1;i<=n;i++){scanf("%d",&a[i]);pos[i]=(i-1)/block+1;b[i]=a[i];}if(n%block)m=n/block+1;else m=n/block;for(int i=1;i<=m;i++)reset(i);for(int i=1;i<=q;i++){char ch[5];int x,y,v;scanf("%s%d%d%d",ch,&x,&y,&v);if(ch[0]=='M')update(x,y,v);else printf("%d\n",query(x,y,v));}return 0;
}