codeforces 551 E. GukiZ and GukiZiana (分块)

本文介绍了一个名为 GukiZiana 的函数,该函数用于处理数组查询问题,包括增加指定范围内元素的值及查找特定值的最大跨度。通过分块算法优化了查询效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

E. GukiZ and GukiZiana
time limit per test
10 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Professor GukiZ was playing with arrays again and accidentally discovered new function, which he called GukiZiana. For given array a, indexed with integers from 1 to n, and number yGukiZiana(a, y) represents maximum value of j - i, such that aj = ai = y. If there is no y as an element in a, then GukiZiana(a, y) is equal to  - 1. GukiZ also prepared a problem for you. This time, you have two types of queries:

  1. First type has form 1 l r x and asks you to increase values of all ai such that l ≤ i ≤ r by the non-negative integer x.
  2. Second type has form 2 y and asks you to find value of GukiZiana(a, y).

For each query of type 2, print the answer and make GukiZ happy!

Input

The first line contains two integers nq (1 ≤ n ≤ 5 * 105, 1 ≤ q ≤ 5 * 104), size of array a, and the number of queries.

The second line contains n integers a1, a2, ... an (1 ≤ ai ≤ 109), forming an array a.

Each of next q lines contain either four or two numbers, as described in statement:

If line starts with 1, then the query looks like 1 l r x (1 ≤ l ≤ r ≤ n0 ≤ x ≤ 109), first type query.

If line starts with 2, then th query looks like 2 y (1 ≤ y ≤ 109), second type query.

Output

For each query of type 2, print the value of GukiZiana(a, y), for y value for that query.

Examples
input
4 3
1 2 3 4
1 1 2 1
1 1 1 1
2 3
output
2
input
2 3
1 2
1 2 2 1
2 3
2 4
output
0
-1


题解:分块

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#define N 500003
#define LL long long
using namespace std;
int belong[N],l[N],r[N];
LL val[N],delta[N];
struct point
{
	LL x; int pos;
};
struct data{
	point a[720];
}c[720];
int n,m;
int cmp(point a,point b)
{
	return a.x<b.x||a.x==b.x&&a.pos<b.pos;
}
void build(int x)
{
	int cnt=0;
	for (int i=l[x];i<=r[x];i++) c[x].a[++cnt].x=val[i],c[x].a[cnt].pos=i;
	sort(c[x].a+1,c[x].a+cnt+1,cmp);
}
void calc(int &x,int &y,int k,LL v)
{
	LL vl=v-delta[k];
	if (vl<=0) return;
	int ls=1; int rs=r[k]-l[k]+1; int ans=n+1;
	while (ls<=rs) {
		int mid=(ls+rs)/2;
		if (c[k].a[mid].x>=vl) ans=min(ans,mid),rs=mid-1;
		else ls=mid+1; 
	}
	if (c[k].a[ans].x==vl&&ans!=n+1) 
	 x=min(x,c[k].a[ans].pos),y=max(y,c[k].a[ans].pos);
	ls=1; rs=r[k]-l[k]+1;  ans=0;
	while (ls<=rs) {
		int mid=(ls+rs)/2;
		if (c[k].a[mid].x<=vl) ans=max(ans,mid),ls=mid+1;
		else rs=mid-1;
	}
	if (c[k].a[ans].x==vl&&ans!=0) 
	  x=min(x,c[k].a[ans].pos),y=max(y,c[k].a[ans].pos);
}
int main()
{
	freopen("a.in","r",stdin);
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++) scanf("%I64d",&val[i]);
	int block=sqrt(n);
	int t=ceil(n*1.0/block);
	for (int i=1;i<=n;i++) belong[i]=(i-1)/block+1;
    for (int i=1;i<=t;i++) l[i]=n,r[i]=0;
    for (int i=1;i<=n;i++) {
    	int k=belong[i];
    	l[k]=min(i,l[k]);
    	r[k]=max(r[k],i);
	}
	for (int i=1;i<=t;i++) build(i);
	for (int i=1;i<=m;i++) {
		int opt,x,y; LL k;
		scanf("%d",&opt);
		if (opt==1) {
			scanf("%d%d%I64d",&x,&y,&k);
			if (belong[x]==belong[y]) {
				for (int i=x;i<=y;i++) val[i]+=k;
				build(belong[x]); continue;
			}
			for (int i=x;i<=r[belong[x]];i++) val[i]+=k;
			build(belong[x]);
			for (int i=l[belong[y]];i<=y;i++) val[i]+=k;
			build(belong[y]);
			for (int i=belong[x]+1;i<=belong[y]-1;i++) delta[i]+=k;
		}
		else {
			scanf("%I64d",&k);
			int x=n+1; int y=0;
			for (int i=1;i<=t;i++) 
			 calc(x,y,i,k);
			printf("%d\n",max(-1,y-x));
		}
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值