codeforces contest 551 problem E(分块)
2017-10-08 22:52
399 查看
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 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.
Examples
Input
Output
Input
Output
裸题
#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
const int N = 1e6+10;
LL a
, maxt
, kx;
int lx
, rx
, id
;
struct node
{
LL x;
int px;
bool operator <(const node &A)const
{
if(x!=A.x) return x<A.x;
return px<A.px;
}
};
vector<node>p
;
void solve(int l,int r,LL h)
{
int l1=id[l], r1=id[r];
if(l1==r1)
{
for(int i=l; i<=r; i++) a[i]+=h;
for(int i=0; i<p[l1].size(); i++)
if(p[l1][i].px>=l&&p[l1][i].px<=r) p[l1][i].x+=h;
sort(p[l1].begin(),p[l1].end());
return ;
}
for(int i=l; i<=rx[l1]; i++) a[i]+=h;
for(int i=0; i<p[l1].size(); i++)
if(p[l1][i].px>=l&&p[l1][i].px<=r) p[l1][i].x+=h;
sort(p[l1].begin(),p[l1].end());
for(int i=lx[r1]; i<=r; i++) a[i]+=h;
for(int i=0; i<p[r1].size(); i++)
if(p[r1][i].px>=l&&p[r1][i].px<=r) p[r1][i].x+=h;
sort(p[r1].begin(),p[r1].end());
for(int i=l1+1; i<r1; i++) maxt[i]+=h;
return ;
}
LL get(LL v)
{
LL l=-1,r=-1;
for(LL i=1; i<=kx; i++)
{
int pos=lower_bound(p[i].begin(),p[i].end(),node {v-maxt[i],-1})-p[i].begin();
if(p[i][pos].x!=v-maxt[i]) continue;
for(int j=pos; j<p[i].size(); j++)
{
if(p[i][j].x!=v-maxt[i]) break;
if(l==-1||l>p[i][j].px) l=p[i][j].px;
if(r==-1||r<p[i][j].px) r=p[i][j].px;
}
}
if(l==-1) return -1;
return r-l;
}
int main()
{
int n, q;
scanf("%d %d", &n, &q);
for(int i=1; i<=n; i++) scanf("%I64d", &a[i]);
LL k=(LL)sqrt(1.0*n),i;
for(i=1; i*k<n; i++)
{
lx[i]=(i-1)*k+1,rx[i]=i*k,p[i].clear();
maxt[i]=0;
for(int j=lx[i]; j<=rx[i]; j++) p[i].push_back(node {a[j],j}),id[j]=i;
sort(p[i].begin(),p[i].end());
}
lx[i]=(i-1)*k+1,rx[i]=n,p[i].clear();
maxt[i]=0,kx=i;
for(int j=lx[i]; j<=rx[i]; j++) p[i].push_back(node {a[j],j}),id[j]=i;
sort(p[i].begin(),p[i].end());
while(q--)
{
int x;
LL h;
scanf("%d", &x);
if(x==1)
{
int l, r;
scanf("%d %d %I64d", &l, &r, &h);
solve(l,r,h);
}
else
{
scanf("%I64d", &h);
printf("%I64d\n",get(h));
}
}
return 0;
}
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 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.
Examples
Input
4 3 1 2 3 4 1 1 2 1 1 1 1 1 2 3
Output
2
Input
2 3
1 21 2 2 1
2 3
2 4
Output
0 -1
裸题
#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
const int N = 1e6+10;
LL a
, maxt
, kx;
int lx
, rx
, id
;
struct node
{
LL x;
int px;
bool operator <(const node &A)const
{
if(x!=A.x) return x<A.x;
return px<A.px;
}
};
vector<node>p
;
void solve(int l,int r,LL h)
{
int l1=id[l], r1=id[r];
if(l1==r1)
{
for(int i=l; i<=r; i++) a[i]+=h;
for(int i=0; i<p[l1].size(); i++)
if(p[l1][i].px>=l&&p[l1][i].px<=r) p[l1][i].x+=h;
sort(p[l1].begin(),p[l1].end());
return ;
}
for(int i=l; i<=rx[l1]; i++) a[i]+=h;
for(int i=0; i<p[l1].size(); i++)
if(p[l1][i].px>=l&&p[l1][i].px<=r) p[l1][i].x+=h;
sort(p[l1].begin(),p[l1].end());
for(int i=lx[r1]; i<=r; i++) a[i]+=h;
for(int i=0; i<p[r1].size(); i++)
if(p[r1][i].px>=l&&p[r1][i].px<=r) p[r1][i].x+=h;
sort(p[r1].begin(),p[r1].end());
for(int i=l1+1; i<r1; i++) maxt[i]+=h;
return ;
}
LL get(LL v)
{
LL l=-1,r=-1;
for(LL i=1; i<=kx; i++)
{
int pos=lower_bound(p[i].begin(),p[i].end(),node {v-maxt[i],-1})-p[i].begin();
if(p[i][pos].x!=v-maxt[i]) continue;
for(int j=pos; j<p[i].size(); j++)
{
if(p[i][j].x!=v-maxt[i]) break;
if(l==-1||l>p[i][j].px) l=p[i][j].px;
if(r==-1||r<p[i][j].px) r=p[i][j].px;
}
}
if(l==-1) return -1;
return r-l;
}
int main()
{
int n, q;
scanf("%d %d", &n, &q);
for(int i=1; i<=n; i++) scanf("%I64d", &a[i]);
LL k=(LL)sqrt(1.0*n),i;
for(i=1; i*k<n; i++)
{
lx[i]=(i-1)*k+1,rx[i]=i*k,p[i].clear();
maxt[i]=0;
for(int j=lx[i]; j<=rx[i]; j++) p[i].push_back(node {a[j],j}),id[j]=i;
sort(p[i].begin(),p[i].end());
}
lx[i]=(i-1)*k+1,rx[i]=n,p[i].clear();
maxt[i]=0,kx=i;
for(int j=lx[i]; j<=rx[i]; j++) p[i].push_back(node {a[j],j}),id[j]=i;
sort(p[i].begin(),p[i].end());
while(q--)
{
int x;
LL h;
scanf("%d", &x);
if(x==1)
{
int l, r;
scanf("%d %d %I64d", &l, &r, &h);
solve(l,r,h);
}
else
{
scanf("%I64d", &h);
printf("%I64d\n",get(h));
}
}
return 0;
}
相关文章推荐
- codeforces contest 13 problem E(分块)
- 【分块】MIPT-2016 Pre-Finals Workshop, Taiwan NTU Contest, Sunday, March 27, 2016 Problem A. As Easy As Possible
- codeforces contest 785 problem E(分块)
- 【codeforces】2014 Asia Xian Regional Contest G The Problem to Slow Down You 【Palindromic Tree】
- codeforces contest 855 problem C(树形DP)
- Codeforces contest 883 problem L. Berland.Taxi(Treap+优先队列)
- codeforces 551 E. GukiZ and GukiZiana (分块)
- Codeforces 551 E. GukiZ and GukiZiana (分块搜索)
- codeforces contest 383 problem C(树状数组+dfs序)
- codeforces contest 869 problem C(组合数)
- codeforces contest 855 problem B(前缀后缀)
- 2014-2015 ACM-ICPC, Asia Xian Regional Contest C – The Problem Needs 3D Arrays(最大密度子图)
- codeforces 742B - Arpa’s obvious problem and Mehrdad’s terrible solution
- 2017 Wuhan University Programming Contest 现场赛 I. A simple math problem(矩阵快速幂)
- USACO Section 1.1 : Programming Contest Problem Types
- CodeForces 201D - Brand New Problem
- Codeforces 579 C. A Problem about Polyline(Codeforces Round #320 (Div. 2) )
- Codeforces_714E:Sonya_and_Problem_Wihtout_a_Legend(DP+想法题)
- contest: Codeforces Round #300, problem: (A) Cutting Banner
- 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem H. Hometask 水题