Codeforces Round #307 (Div. 2) E. GukiZ and GukiZiana 分块
E. GukiZ and GukiZiana
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/551/problem/E
Description
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 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 分块的更多相关文章
- 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 ...
- Codeforces Round #307 (Div. 2) E. GukiZ and GukiZiana (分块)
题目地址:http://codeforces.com/contest/551/problem/E 将n平均分成sqrt(n)块,对每一块从小到大排序,并设置一个总体偏移量. 改动操作:l~r区间内,对 ...
- 水题 Codeforces Round #307 (Div. 2) A. GukiZ and Contest
题目传送门 /* 水题:开个结构体,rk记录排名,相同的值有相同的排名 */ #include <cstdio> #include <cstring> #include < ...
- 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/ ...
- 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/ ...
- 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 ...
- 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 ...
- Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations (矩阵高速幂)
题目地址:http://codeforces.com/contest/551/problem/D 分析下公式能够知道,相当于每一位上放0或者1使得最后成为0或者1.假设最后是0的话,那么全部相邻位一定 ...
- Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations
得到k二进制后,对每一位可取得的方法进行相乘即可,k的二进制形式每一位又分为2种0,1,0时,a数组必定要为一长为n的01串,且串中不出现连续的11,1时与前述情况是相反的. 且0时其方法总数为f(n ...
随机推荐
- android edittext不弹出软键盘
方法一: 在AndroidMainfest.xml中选择哪个activity,设置windowSoftInputMode属性为adjustUnspecified|stateHidden 例如:< ...
- selenium python (五)打印信息及设置等待时间
#!/usr/bin/python# -*- coding: utf-8 -*-__author__ = 'zuoanvip' #一般情况下我们要验证打开的页面是否正确,可通过网页的Title和Cur ...
- 基于opencv的手写数字识别(MFC,HOG,SVM)
参考了秋风细雨的文章:http://blog.csdn.net/candyforever/article/details/8564746 花了点时间编写出了程序,先看看效果吧. 识别效果大概都能正确. ...
- ANSI
为了扩充ASCII编码,以用于显示本国的语言,不同的国家和地区制定了不同的编码标准,由此产生了GB2312.BIG5.JIS等各自的编码标准.这些使用两个字节来代表一个字符的各种汉字延伸编码方式被称为 ...
- Name-based virtual servers 给予名称的虚拟服务
nginx first decides which server should process the request. Let’s start with a simple configuration ...
- Linux下U盘的挂载和文件的拷贝
把文件通过U盘拷贝到linux系统下插好U盘后,查看磁盘情况fdisk -l正常情况下有 Disk /dev/sda:2045 MB,2045247488 bytes47 heads,46 secto ...
- LeetCode(9) - Palindrome Number
题目要求判断一个整数是不是回文数,假设输入是1234321,就返回true,输入的是123421,就返回false.题目要求in-place,思路其实很简单,在LeetCode(7)里面我们刚好做了r ...
- String - 兴趣解读
个优点: . 以下代码的HashCode是否相同,它们是否是同个对象: . 以下代码的HashCode是否相同,他们是否是同个对象: . 以下代码的HashCode是否相同,他们是否是同 ...
- 新工程软连接到原来的工程的out目录后,可以直接编译模块
P508B_App_old_developer/alps$ ln -s ../../P508B_App/alps/out 连接后,第一次编译后要加分支 ./mk hedy89_we_jb2 mm p ...
- 第三百三十三天 how can I 坚持
为什么感觉那么累呢,老想睡觉. 晚上勇江请吃饭,其实内心是拒绝的,好伐,老想回家尽快睡觉. 今天说是要搞oauth认证的东西,看的一头雾水. 想的太简单了.其实也不难.apache的东西,都封装好的. ...