分块 WA 0pts 求调
查看原帖
分块 WA 0pts 求调
420692
Leonid楼主2024/11/22 16:04
#include<bits/stdc++.h>

using namespace std;

#define M 100005
#define N 380

int n,m,B;
int a[M];

int id[M],L[N],R[N];
int d[M];
int tag[N];

void init(){
	for(int i=1;i<=n;++i){
		d[i]=a[i];
		id[i]=(i-1)/B+1;
		if(id[i]!=id[i-1]) L[id[i]]=i;
		R[id[i]]=i;
		tag[id[i]]=0;
	}
	for(int i=1;i<=id[n];++i) stable_sort(d+L[i],d+R[i]+1);
}

void update(int l,int r,int x){
	if(id[l]==id[r]){
		for(int i=l;i<=r;++i) a[i]+=x;
		for(int i=L[id[l]];i<=R[id[l]];++i) d[i]=a[i];
		stable_sort(d+L[id[l]],d+R[id[l]]+1);
		return;
	}
	
	for(int i=l;i<=R[id[l]];++i) a[i]+=x;
	for(int i=L[id[l]];i<=R[id[l]];++i) d[i]=a[i];
	stable_sort(d+L[id[l]],d+R[id[l]]+1);
	
	for(int i=r;i>=L[id[r]];--i) a[i]+=x;
	for(int i=L[id[r]];i<=R[id[r]];++i) d[i]=a[i];
	stable_sort(d+L[id[r]],d+R[id[r]]+1);
	
	for(int i=id[l]+1;i<=id[r]-1;++i) tag[i]+=x;
}

int fnd(int l,int r,int x){
	int L=l,R=r,res=l-1;
	while(L<=R){
		int mid=(L+R)>>1;
		if(d[mid]<x) res=mid,L=mid+1;
		else R=mid-1;
	}
	return res-l+1;
}

int getrk(int l,int r,int x){
	if(id[l]==id[r]){
		int res=0;
		for(int i=l;i<=r;++i) res+=(a[i]+tag[id[l]]<x);
		return res+1;
	}
	
	int res=0;
	for(int i=l;i<=R[id[l]];++i) res+=(a[i]+tag[id[l]]<x);
	for(int i=r;i>=L[id[r]];--i) res+=(a[i]+tag[id[r]]<x);
	for(int i=id[l]+1;i<=id[r]-1;++i) res+=fnd(L[i],R[i],x-tag[i]);
	return res+1;
}

int getval(int l,int r,int x){
	if(r-l+1<x) return -1;
	int L=-2e4,R=2e4,res=0;
	while(L<=R){
		int mid=(L+R)/2;
		if(getrk(l,r,mid)<=x) res=mid,L=mid+1;
		else R=mid-1;
	}
	return res;
}

int main(){
	scanf("%d %d",&n,&m); B=264;
	for(int i=1;i<=n;++i) scanf("%d",&a[i]);
	init();
	while(m--){
		int op,l,r,c;
		scanf("%d %d %d %d",&op,&l,&r,&c);
		if(op==1) printf("%d\n",getval(l,r,c));
		else update(l,r,c);
	}
	return 0;
}
2024/11/22 16:04
加载中...