codeforces 551 E. GukiZ and GukiZiana

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Codeforces Round 894 (Div. 3) 是一个Codeforces举办的比赛,是第894轮的Div. 3级别比赛。它包含了一系列题目,其中包括题目E. Kolya and Movie Theatre。 根据题目描述,E. Kolya and Movie Theatre问题要求我们给定两个字符串,通过三种操作来让字符串a等于字符串b。这三种操作分别为:交换a中相同位置的字符、交换a中对称位置的字符、交换b中对称位置的字符。我们需要先进行一次预处理,替换a中的字符,然后进行上述三种操作,最终得到a等于b的结果。我们需要计算预处理操作的次数。 根据引用的讨论,当且仅当b[i]==b[n-i-1]时,如果a[i]!=a[n-i-1],需要进行一次操作;否则不需要操作。所以我们可以遍历字符串b的前半部分,判断对应位置的字符是否与后半部分对称,并统计需要进行操作的次数。 以上就是Codeforces Round 894 (Div. 3)的简要说明和题目E. Kolya and Movie Theatre的要求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Codeforces Round #498 (Div. 3) (A+B+C+D+E+F)](https://blog.csdn.net/qq_46030630/article/details/108804114)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Codeforces Round 894 (Div. 3)A~E题解](https://blog.csdn.net/gyeolhada/article/details/132491891)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值