codeforces 551 E. GukiZ and GukiZiana

本文介绍了一个名为 GukiZiana 的函数实现方法,该函数用于求解给定数组中特定元素的最大跨度。文章详细解释了两种类型的查询操作:更新数组元素与查询 GukiZiana 函数值,并提供了一段 C++ 代码示例来展示如何通过分块技术高效地处理这些操作。

摘要生成于 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.

Sample test(s)
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

分块

/*======================================================
# Author: whai
# Last modified: 2015-10-22 16:01
# Filename: e.cpp
======================================================*/
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <set>
#include <map>

using namespace std;

#define LL __int64
#define PB push_back
#define P pair<int, int>
#define X first
#define Y second
#define MP make_pair

const int N = 1005;

int unit_sz, unit_tot;
vector< pair<LL, int> > unit[N];
LL add[N];

int unit_id(int x) {
	return x / unit_sz;
}

bool cmp(pair<LL, int> a, pair<LL, int> b) {
	return a.Y < b.Y;
}

void update(int l, int r, LL x) {
	int l_id = unit_id(l);
	int r_id = unit_id(r);
	if(l_id == r_id) {
		sort(unit[l_id].begin(), unit[l_id].end(), cmp);
		int ll = l % unit_sz;
		int rr = r % unit_sz;
		for(int i = ll; i <= rr; ++i) {
			unit[l_id][i].X += x;
		}
		sort(unit[l_id].begin(), unit[l_id].end());
	} else {
		int ll = l % unit_sz;
		int lr = unit_sz - 1;
		int rl = 0;
		int rr = r % unit_sz;
		sort(unit[l_id].begin(), unit[l_id].end(), cmp);
		for(int i = ll; i <= lr; ++i) {
			unit[l_id][i].X += x;
		}
		sort(unit[l_id].begin(), unit[l_id].end());

		sort(unit[r_id].begin(), unit[r_id].end(), cmp);
		for(int i = rl; i <= rr; ++i) {
			unit[r_id][i].X += x;
		}
		sort(unit[r_id].begin(), unit[r_id].end());
		
		for(int i = l_id + 1; i <= r_id - 1; ++i) {
			add[i] += x;
		}
	}
}

int get_l(LL x) {
	for(int i = 0; i < unit_tot; ++i) {
		int p = lower_bound(unit[i].begin(), unit[i].end(), MP(x - add[i], 0)) - unit[i].begin();
		if(p < unit[i].size() && unit[i][p].X + add[i] == x) {
			return unit[i][p].Y;
		}
	}
	return -1;
}

int get_r(LL x) {
	for(int i = unit_tot - 1; i >= 0; --i) {
		int p = lower_bound(unit[i].begin(), unit[i].end(), MP(x - add[i], N * N)) - unit[i].begin();
		--p;
		if(p >= 0 && unit[i][p].X + add[i] == x) {
			return unit[i][p].Y;
		}
	}
	return -1;
}

void print() {
	for(int i = 0; i < unit_tot; ++i) {
		for(int j = 0; j < unit[i].size(); ++j) {
			cout<<unit[i][j].X<<' ';
		}
		cout<<endl;
	}
}

int main() {
	int n, m;
	scanf("%d%d", &n, &m);
	unit_sz = sqrt(n) + 1;
	unit_tot = n / unit_sz;
	if(n % unit_sz) ++unit_tot;

	for(int i = 0; i < n; ++i) {
		LL x;
		scanf("%I64d", &x);
		int id = unit_id(i);
		unit[id].PB(MP(x, i));
	}
	

	for(int i = 0; i < unit_tot; ++i) {
		sort(unit[i].begin(), unit[i].end());
	}

	for(int i = 0; i < m; ++i) {
		int op, l, r;
		LL x;
		scanf("%d", &op);
		if(op == 1) {
			scanf("%d%d%I64d", &l, &r, &x);
			update(l - 1, r - 1, x);
		} else {
			scanf("%I64d", &x);
			int R = get_r(x);
			int L = get_l(x);
			if(L == -1) puts("-1");
			else printf("%d\n", R - L);
		}
		//print();
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值