[codeforces551E]GukiZ and GukiZiana

本文针对CodeForces平台上的551E题目——GukiZ and GukiZiana,提供了一种有效的解决方案。该方案通过分块、排序和二分查找的方式处理数组查询,包括增加指定范围内的元素值以及寻找特定值的最大跨度。

[codeforces551E]GukiZ and GukiZiana

试题描述

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 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 y and asks you to find value of GukiZiana(a, y).

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

输入

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 l r x (1 ≤ l ≤ r ≤ n0 ≤ x ≤ 109), first type query.

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

输出

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

输入示例

4 3
1 2 3 4
1 1 2 1
1 1 1 1
2 3

输出示例

2

数据规模及约定

见“输入

题解

分块,每块内排序,查找的时候在块内二分。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <cmath>
using namespace std;

const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
	if(Head == Tail) {
		int l = fread(buffer, 1, BufferSize, stdin);
		Tail = (Head = buffer) + l;
	}
	return *Head++;
}
int read() {
	int x = 0, f = 1; char c = Getchar();
	while(!isdigit(c)){ if(c == '-') f = -1; c = Getchar(); }
	while(isdigit(c)){ x = x * 10 + c - '0'; c = Getchar(); }
	return x * f;
}

#define maxn 500010
#define maxbl 710
#define maxlen 710
#define LL long long

int n, blid[maxn], cb, st[maxbl], en[maxbl], len[maxbl];
LL A[maxn], Ord[maxbl][maxlen], addv[maxbl];

void build_sort(int b) {
	for(int i = st[b]; i <= en[b]; i++) Ord[b][i-st[b]] = A[i];
	sort(Ord[b], Ord[b] + len[b]);
	return ;
}
void pushdown(int b) {
	for(int i = st[b]; i <= en[b]; i++) A[i] += addv[b];
	addv[b] = 0;
	return ;
}

void add(int ql, int qr, int v) {
	if(blid[ql] == blid[qr]) {
		int b = blid[ql];
		pushdown(b);
		for(int i = ql; i <= qr; i++) A[i] += v;
		build_sort(b);
		return ;
	}
	int bl = blid[ql], br = blid[qr];
	pushdown(bl); pushdown(br);
	for(int i = ql; i <= en[bl]; i++) A[i] += v;
	for(int i = st[br]; i <= qr; i++) A[i] += v;
	build_sort(bl); build_sort(br);
	for(int i = bl + 1; i <= br - 1; i++) addv[i] += v;
	return ;
}
bool Find(int b, LL y) {
	int p = lower_bound(Ord[b], Ord[b] + len[b], y - addv[b]) - Ord[b];
	return y - addv[b] == Ord[b][p];
}

int main() {
	n = read(); int q = read();
	int m = sqrt(n + .5);
	for(int i = 1; i <= n; i++) {
		A[i] = read();
		int bl = (i - 1) / m + 1; cb = max(cb, bl);
		blid[i] = bl;
		if(!st[bl]) st[bl] = i; en[bl] = i;
	}
	
	for(int i = 1; i <= cb; i++) len[i] = en[i] - st[i] + 1, build_sort(i);
	while(q--) {
		int tp = read();
		if(tp == 1) {
			int ql = read(), qr = read(), v = read();
			add(ql, qr, v);
		}
		else {
			int y = read(), al = -1, ar = -1;
			for(int i = 1; i <= cb; i++) if(Find(i, y)) {
				pushdown(i); build_sort(i);
				for(int j = st[i]; j <= en[i]; j++) if(y == A[j]) {
					al = j; break;
				}
				break;
			}
			if(al == -1){ puts("-1"); continue; }
			for(int i = cb; i; i--) if(Find(i, y)) {
				pushdown(i); build_sort(i);
				for(int j = en[i]; j >= st[i]; j--) if(y == A[j]) {
					ar = j; break;
				}
				break;
			}
			printf("%d\n", ar - al);
		}
	}
	
	return 0;
}

 

转载于:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/6803936.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值