MnZN初学分块,求调
查看原帖
MnZN初学分块,求调
905268
Underage_potato楼主2024/12/21 10:29
#include<bits/stdc++.h>
#define rep(i,st,ed,val) for(int i=st;i<=ed;i+=val)
#define Rep(i,st,ed,val) for(int i=st;i>=ed;i-=val)
#define int long long
using namespace std;
static constexpr int N=1e5+10;
int n,m;
int a[N];
int blo,cnt;
int L[N],R[N],bel[N];
int lz[N],b[N];
int p[N];

inline bool cmp(int x,int y){
	return a[x]<a[y];
}

inline void build(){
	blo=sqrt(n);
	cnt=(n+blo-1)/blo;
	rep(i,1,cnt,1){
		L[i]=(i-1)*blo+1;
		R[i]=i*blo;
	}
	R[cnt]=n;
	rep(i,1,n,1){
		bel[i]=(i-1)/blo+1;
		lz[bel[i]]=0;
	}
	rep(i,1,cnt,1){
		sort(p+L[i],p+R[i]+1,cmp);
	}
	rep(i,1,n,1){
		b[i]=a[p[i]];
	}
	return ;
}

inline void modify(int l,int r,int val){
	if(bel[l]==bel[r]){
		rep(i,l,r,1){
			a[i]+=val;
		}
		vector<int> v1,v2;
		rep(i,L[bel[l]],R[bel[l]],1){
			if(l<=p[i] && p[i]<=R[bel[l]]){
				v1.emplace_back(p[i]);
			}
			else{
				v2.emplace_back(p[i]);
			}
		}
		int tot1=0,tot2=0;
		rep(i,L[bel[l]],R[bel[l]],1){
			if(v2.size()==0){
				p[i]=v1[tot1];
				tot1++;
				continue;
			}
			if(a[v1[tot1]]<a[v2[tot2]]){
				p[i]=v1[tot1];
				tot1++;
			}
			else{
				p[i]=v2[tot2];
				tot2++;
			}
		}
		rep(i,L[bel[l]],R[bel[l]],1){
			b[i]=a[p[i]];
		}
	}
	vector<int> v1,v2;
	rep(i,l,R[bel[l]],1){
		a[i]+=val;
	}
	rep(i,L[bel[l]],R[bel[l]],1){
		if(l<=p[i] && p[i]<=R[bel[l]]){
			v1.emplace_back(p[i]);
		}
		else{
			v2.emplace_back(p[i]);
		}
	}
//	rep(i,0,v1.size()-1,1){
//		cout<<v1[i]<<" ";
//	}
//	cout<<'\n';
	/*rep(i,0,v2.size()-1,1){
		cout<<v2[i]<<" ";
	}
	cout<<'\n';*/
	int tot1=0,tot2=0;
	rep(i,L[bel[l]],R[bel[l]],1){
		if(v2.size()==0){
			p[i]=v1[tot1];
			tot1++;
			continue;
		}
		if(a[v1[tot1]]<a[v2[tot2]]){
			p[i]=v1[tot1];
			tot1++;
		}
		else{
			p[i]=v2[tot2];
			tot2++;
		}
	}
	rep(i,L[bel[l]],R[bel[l]],1){
		b[i]=a[p[i]];
	}
	v1.clear(),v2.clear();
	Rep(i,r,L[bel[r]],1){
		a[i]+=val;
	}
	rep(i,L[bel[r]],R[bel[r]],1){
		if(L[bel[r]]<=p[i] && p[i]<=r){
			v1.emplace_back(p[i]);
		}
		else{
			v2.emplace_back(p[i]);
		}
	}
	tot1=0,tot2=0;
	rep(i,L[bel[r]],R[bel[r]],1){
		if(v2.size()==0){
			p[i]=v1[tot1];
			tot1++;
			continue;
		}
		if(a[v1[tot1]]<a[v2[tot2]]){
			p[i]=v1[tot1];
			tot1++;
		}
		else{
			p[i]=v2[tot2];
			tot2++;
		}
	}
	rep(i,L[bel[r]],R[bel[r]],1){
		b[i]=a[p[i]];
	}
	rep(i,bel[l]+1,bel[r]-1,1){
		lz[bel[i]]+=val;
	}
	return ;
}

inline int query(int l,int r,int k){
	int ans=0;
	if(bel[l]==bel[r]){
		rep(i,l,r,1){
			ans+=((a[i]+lz[bel[i]])>=k);
		}
		return ans;
	}
	rep(i,l,R[bel[l]],1){
		ans+=((a[i]+lz[bel[i]])>=k);
	}
	Rep(i,r,L[bel[r]],1){
		ans+=((a[i]+lz[bel[i]])>=k);
	}
	rep(i,bel[l]+1,bel[r]-1,1){
		cerr<<k-lz[i]<<'\n';
		ans+=(upper_bound(b+L[i],b+R[i]+1,k-lz[i])-(b+L[i]));
		//cout<<(upper_bound(b+L[i],b+R[i],k-lz[i])-(b+L[i]))<<"\n";
	}
	return ans;
}

inline void doit(){
/*	ios::sync_with_stdio(NULL);
	cin.tie(NULL),cout.tie(NULL);*/
	cin>>n>>m;
	rep(i,1,n,1){
		cin>>a[i];
		p[i]=i;
	}
	build();
	rep(i,1,m,1){
		char opt;
		int l,r,k;
		cin>>opt>>l>>r>>k;
		if(opt=='M'){
			modify(l,r,k);
		}
		else{
			cout<<query(l,r,k-1)<<'\n';
//			rep(i,1,n,1){
//				cout<<a[i]<<" ";
//			}
//			cout<<"\n";
//			rep(i,1,cnt,1){
//				cout<<lz[i]<<" ";
//			}
//			cout<<"\n";
//			rep(i,1,n,1){
//				cout<<b[i]<<" ";
//			}
//			cout<<"\n";
		}
	}
}
signed main(){
	doit();
	return 0;
}
2024/12/21 10:29
加载中...