E. GukiZ and GukiZiana

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/551/problem/E

Description

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!

Input

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.

Output

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

Sample Input

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

Sample Output

2

HINT

题意

一堆数,俩操作

[l,r]区间的数都加 v

查询最大的j-i,要求满足a[j]=a[i]=v

题解:

时间10s,这就是明摆着告诉我们这道题是分块,然后我们就分块乱搞就好了……

二分写挂的都是傻逼(没错就是我!

代码:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 2000001
#define mod 10007
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************** ll n,q,m,block;
ll a[],b[],pos[],add[];
void reset(ll x)
{
ll l=(x-)*block+,r=min(x*block,n);
for(ll i=l;i<=r;i++)
b[i]=a[i];
sort(b+l,b+r+);
}
ll find(ll x,ll v)
{
ll l=(x-)*block+,r=min(x*block,n);
ll last=r;
while(l<r)
{
ll mid=(l+r)>>;
if(b[mid]<v)l=mid+;
else
r=mid;
if(b[mid]==v)
return ;
}
if(b[l]==v||b[r]==v)
return ;
return ;
}
void update(ll x,ll y,ll v)
{
if(pos[x]==pos[y])
{
for(int i=x;i<=y;i++)a[i]=a[i]+v;
}
else
{
for(int i=x;i<=pos[x]*block;i++)a[i]=a[i]+v;
for(int i=(pos[y]-)*block+;i<=y;i++)a[i]=a[i]+v;
}
reset(pos[x]);reset(pos[y]);
for(int i=pos[x]+;i<pos[y];i++)
add[i]+=v;
}
ll query(ll x,ll y,ll v)
{
ll sum=;
if(pos[x]==pos[y])
{
for(int i=x;i<=y;i++)if(a[i]+add[pos[i]]==v)sum++;
}
else
{
for(int i=x;i<=pos[x]*block;i++)
if(a[i]+add[pos[i]]==v)sum++;
for(int i=(pos[y]-)*block+;i<=y;i++)
if(a[i]+add[pos[i]]==v)sum++;
}
for(int i=pos[x]+;i<pos[y];i++)
sum+=find(i,v-add[i]); //cout<<sum<<endl;
return sum;
}
ll deall(ll x,ll y,ll v)
{
ll sum=;
if(pos[x]==pos[y])
{
for(int i=x;i<=y;i++)if(a[i]+add[pos[i]]==v)return i;
}
else
{
for(int i=x;i<=pos[x]*block;i++)
if(a[i]+add[pos[i]]==v)return i;
for(int i=pos[x]+;i<pos[y];i++)
{
int d=find(i,v-add[i]);
if(d>)
{
for(int j=i*block-block+;j<=i*block;j++)
if(a[j]+add[pos[j]]==v)
return j;
}
}
for(int i=(pos[y]-)*block+;i<=y;i++)
if(a[i]+add[pos[i]]==v)return i;
}
}
ll dealr(ll x,ll y,ll v)
{
ll sum=;
if(pos[x]==pos[y])
{
for(int i=y;i>=x;i--)if(a[i]+add[pos[i]]==v)return i;
}
else
{
for(int i=y;i>=(pos[y]-)*block+;i--)
if(a[i]+add[pos[i]]==v)
return i; for(int i=pos[y];i>=pos[x]+;i--)
{
int d=find(i,v-add[i]);
if(d>)
{
for(int j=i*block;j>=i*block-block;j--)
if(a[j]+add[pos[j]]==v)
return j;
}
}
for(int i=pos[x]*block;i>=x;i--)
if(a[i]+add[pos[i]]==v)return i; }
}
int ask(ll cc)
{
int aa=query(,n,cc);
if(aa==)
return -;
return dealr(,n,cc)-deall(,n,cc);
}
int main()
{
//test;
n=read(),q=read();
block=int(sqrt(n));
for(int i=;i<=n;i++)
{
a[i]=read();
pos[i]=(i-)/block+;
b[i]=a[i];
}
int x,y,v,ch;
if(n%block)m=n/block+;
else m=n/block;
for(int i=;i<=m;i++)reset(i);
for(int i=;i<=q;i++)
{
ch=read();
if(ch==)
{
x=read(),y=read(),v=read();
update(x,y,v);
}
else
{
int p;
p=read();
printf("%d\n",ask(p));
}
}
return ;
}

Codeforces Round #307 (Div. 2) E. GukiZ and GukiZiana 分块的更多相关文章

  1. Codeforces Round #307 (Div. 2) E. GukiZ and GukiZiana(分块)

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

  2. Codeforces Round #307 (Div. 2) E. GukiZ and GukiZiana (分块)

    题目地址:http://codeforces.com/contest/551/problem/E 将n平均分成sqrt(n)块,对每一块从小到大排序,并设置一个总体偏移量. 改动操作:l~r区间内,对 ...

  3. 水题 Codeforces Round #307 (Div. 2) A. GukiZ and Contest

    题目传送门 /* 水题:开个结构体,rk记录排名,相同的值有相同的排名 */ #include <cstdio> #include <cstring> #include < ...

  4. Codeforces Round #307 (Div. 2) C. GukiZ hates Boxes 贪心/二分

    C. GukiZ hates Boxes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/551/ ...

  5. Codeforces Round #307 (Div. 2) A. GukiZ and Contest 水题

    A. GukiZ and Contest Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/551/ ...

  6. Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations 矩阵快速幂优化dp

    D. GukiZ and Binary Operations time limit per test 1 second memory limit per test 256 megabytes inpu ...

  7. Codeforces Round #307 (Div. 2) C. GukiZ hates Boxes 二分

    C. GukiZ hates Boxes time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  8. Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations (矩阵高速幂)

    题目地址:http://codeforces.com/contest/551/problem/D 分析下公式能够知道,相当于每一位上放0或者1使得最后成为0或者1.假设最后是0的话,那么全部相邻位一定 ...

  9. Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations

    得到k二进制后,对每一位可取得的方法进行相乘即可,k的二进制形式每一位又分为2种0,1,0时,a数组必定要为一长为n的01串,且串中不出现连续的11,1时与前述情况是相反的. 且0时其方法总数为f(n ...

随机推荐

  1. go 应用程序性能测试

    runtime/pprof 我们要加入对pprof包里的方法调用,程序才能将运行时候程序的堆内存分配状态记录到文件(也可以是写到其他地方,例如网络等)中,以便进一步的分析. 如果你的go程序只是一个应 ...

  2. Dyslexic Gollum

    题意: 求长度是n的二进制串中,不含长度大于等于k的回文串的个数 分析: dp[i][j][k]表示长度i,后11位状态是j不含长度大于等于k的回文串的个数(因为k最大是10,所把后11位状态压缩,d ...

  3. Http相应代码及获取方法

    1xx(临时响应)用于表示临时响应并需要请求者执行操作才能继续的状态代码. 代码 说明 100(继续) 请求者应当继续提出请求.服务器返回此代码则意味着,服务器已收到了请求的第一部分,现正在等待接收其 ...

  4. TV端产品设计法则和分析

    对TV端产品设计的分析太特么少了.翻遍网络,大多也是针对UI设计的分析,这篇从产品设计的角度,梳理下现有的TV端产品设计法则,顺道做点分析.(前方多图,高能预警) 目录: 1. TV端产品使用场景 2 ...

  5. 在Python中怎么表达True

    在Python中怎么表达True   为False的几种情况 0为False,其他所有数值皆为True 空串("")为False,其他所有字符串皆为True 空list([])为F ...

  6. XML转换为Map通用算法实现 Java版本(Stax实现)

    目前项目中需要将XML转换为Map,下面给出了自己的代码实现. 后续将为大家提供Dom版本的实现. 请各路大神给予各种优良实现. 场景: 在项目中需要解析XML文本字符串,需要将XML文本字符串映射为 ...

  7. AIR 3.0针对移动设备的高性能渲染方案

    转自:http://blog.domlib.com/articles/242.html 当我们一边正在等待Stage3D的发布时,很多开发者似乎还停留在这个印象中:即使AIR 3.0也无法在移动设备上 ...

  8. 原生JS默认设置默认值的写法

    json=json||{};json.type=json.type||'get';json.data=json.data||{};json.time=json.time||2000;

  9. Hadoop MapReduce概念学习系列之mr程序组件全貌(二十)

    其实啊,spilt是,控制Apache Hadoop Mapreduce的map并发任务数,详细见http://www.cnblogs.com/zlslch/p/5713652.html map,是m ...

  10. Django 的 CSRF 保护机制(转)

    add by zhj:假设用户登录了网站A,而在网站B中有一个CSRF攻击标签,点击这个标签就会访问网站A,如果前端数据(包括sessionid)都放在本地存储的话, 当在网站B点击CSRF攻击标签时 ...