E. GukiZ and GukiZiana

http://codeforces.com/contest/551/problem/E

分块入门题.......

#include <bits/stdc++.h>
#define N 500005
#define INF 0x3f3f3f3f
#define ll long long
using namespace std;
ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
ll a[N],flag[1005];int p[N];
int n,q,size;
vector<pair<ll,int> >vec[1005];
void push(int x){
	if(flag[x]==0) return ;
	for(int i=(x-1)*size+1;i<=min(n,size*x);i++) a[i]+=flag[x];
	flag[x]=0;
}
void add(int l,int r,ll vul){
	push(p[l]);
	for(int i=l;i<=min(r,size*p[l]);i++) a[i]+=vul;
	vec[p[l]].clear();
	for(int i=(p[l]-1)*size+1;i<=min(n,size*p[l]);i++) vec[p[l]].push_back(make_pair(a[i],i));
	sort(vec[p[l]].begin(),vec[p[l]].end());
	if(p[l]!=p[r]){
		push(p[r]);
		for(int i=(p[r]-1)*size+1;i<=r;i++) a[i]+=vul;
		vec[p[r]].clear();
		for(int i=(p[r]-1)*size+1;i<=min(n,size*p[r]);i++) vec[p[r]].push_back(make_pair(a[i],i));
		sort(vec[p[r]].begin(),vec[p[r]].end());
	}
	for(int i=p[l]+1;i<=p[r]-1;i++) flag[i]+=vul;
}
int querty(ll x){
	int mx=-1*INF;int mn=INF;
	for(int i=1;i<=p[n];i++){
		int l=1;int r=vec[i].size();int ans=0;
		ll xx=x-flag[i];
		while(l<=r){
			int mid=(l+r)>>1;
			if(vec[i][mid-1].first>=xx){
				ans=mid;r=mid-1;
			}
			else l=mid+1;
		}
		if(ans==0||vec[i][ans-1].first!=xx) continue;
		mn=min(mn,vec[i][ans-1].second);
		l=1;r=vec[i].size();
		while(l<=r){
			int mid=(l+r)>>1;
			if(vec[i][mid-1].first<=xx){
				ans=mid;l=mid+1;
			}
			else r=mid-1;
		}
		mx=max(mx,vec[i][ans-1].second);
	}
	if(mn==INF) return -1;
	return mx-mn;
}
int main(){
	ios::sync_with_stdio(false);
	n=read();q=read();size=(int)sqrt(n);
	for(int i=1;i<=n;i++){
		a[i]=read();p[i]=(i-1)/size+1;
		vec[(i-1)/size+1].push_back(make_pair(a[i],i));
	}
	for(int i=1;i<=p[n];i++) sort(vec[i].begin(),vec[i].end());
	int op,l,r;ll vul;
	for(int i=1;i<=q;i++){
		op=read();
		if(op==1){
			l=read();r=read();vul=read();
			add(l,r,vul);
		}
		else{
			vul=read();
			printf("%d\n",querty(vul));
		}
	}
	return 0;
}

  

posted @ 2018-02-11 01:08  wang9897  阅读(133)  评论(0编辑  收藏  举报