codeforces 551 E. GukiZ and GukiZiana
来源:互联网 发布:电脑版淘宝下载安装 编辑:程序博客网 时间:2024/04/28 11:18
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 y, GukiZiana(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:
- 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.
- 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!
The first line contains two integers n, q (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 ≤ n, 0 ≤ x ≤ 109), first type query.
If line starts with 2, then th query looks like 2 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 31 2 3 41 1 2 11 1 1 12 3
2
2 31 21 2 2 12 32 4
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_pairconst 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;}
- Codeforces 551E GukiZ and GukiZiana (分块)
- codeforces 551E GukiZ and GukiZiana 分块
- Codeforces 551E GukiZ and GukiZiana 分块
- codeforces 551 E. GukiZ and GukiZiana
- CodeForces 551E GukiZ and GukiZiana 【分块】
- codeforces 551E GukiZ and GukiZiana 分块
- codeforces 551E GukiZ and GukiZiana 分块
- Codeforces 551 E. GukiZ and GukiZiana (分块搜索)
- CodeForces 551E GukiZ and GukiZiana (分块)
- codeforces 551 E. GukiZ and GukiZiana (分块)
- CodeForces 551E GukiZ and GukiZiana(分块算法)
- 【分块】 CF 551 E GukiZ and GukiZiana
- codeforces #307 E. GukiZ and GukiZiana(分块+二分查找)
- codeforces #307 E. GukiZ and GukiZiana (分块)
- Codeforces Round #307 (Div. 2) 551E - GukiZ and GukiZiana 分块
- Codeforces Round #307 (Div. 2)E. GukiZ and GukiZiana(分块)
- Codeforces Round #307 (Div. 2) E. GukiZ and GukiZiana (分块)
- CF#307-E. GukiZ and GukiZiana-分块法/平方分桶
- inputreader到inputdispacther到channel
- 虚拟机磁盘 centos 扩展空间
- 1.冒泡排序
- JAVA EL表达式详细介绍使用手册
- his系统运维日志----1
- codeforces 551 E. GukiZ and GukiZiana
- __NSCFNumber length
- Android 将asserts文件夹内文件写入SD卡中
- 系统负载
- jersey使用
- android--适配器模式
- ios键盘弹起不遮挡输入框
- android动画-View Animation的一些基本效果
- redis的安装与配置