Codeforces Round #307 (Div. 2) E. GukiZ and GukiZiana (分块)
题目地址:http://codeforces.com/contest/551/problem/E
将n平均分成sqrt(n)块,对每一块从小到大排序,并设置一个总体偏移量。
改动操作:l~r区间内,对两端的块进行暴力处理,对中间的总体的块用总体偏移量标记添加了多少。时间复杂度: O(2*sqrt(n)+n/sqrt(n)).
查询操作:对每一块二分。查找y-总体偏移量。找到最左边的和最右边的。时间复杂度:O(sqrt(n)*log(sqrt(n)))。
代码例如以下:
#include <iostream>
#include <string.h>
#include <math.h>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <set>
#include <stdio.h>
#include <time.h>
using namespace std;
#define LL __int64
#define pi acos(-1.0)
//#pragma comment(linker, "/STACK:1024000000")
//const int mod=9901;
const int INF=0x3f3f3f3f;
const double eqs=1e-9;
const int MAXN=500000+10;
LL b[MAXN];
struct node
{
LL x;
int id;
}fei[MAXN];
int BS1(int low, int high, LL x)
{
int mid, ans=-1;
while(low<=high){
mid=low+high>>1;
if(fei[mid].x<=x){
ans=mid;
low=mid+1;
}
else high=mid-1;
}
return ans;
}
int BS2(int low, int high, LL x)
{
int mid, ans=-1;
while(low<=high){
mid=low+high>>1;
if(fei[mid].x>=x){
ans=mid;
high=mid-1;
}
else low=mid+1;
}
return ans;
}
bool cmp(node x, node y)
{
if(x.x==y.x) return x.id<y.id;
return x.x<y.x;
}
int main()
{
int n, q, i, j, l, r, k, min1, max1, z, ll, rr, tmp, tot;
LL y, x;
while(scanf("%d%d",&n,&q)!=EOF){
for(i=0;i<n;i++){
scanf("%I64d",&fei[i].x);
fei[i].id=i;
}
k=sqrt(n*1.0);
tot=(n+k-1)/k;
for(i=0;i<tot;i++){
sort(fei+i*k,fei+min((i+1)*k,n),cmp);
}
memset(b,0,sizeof(b));
while(q--){
scanf("%d",&z);
if(z==1){
scanf("%d%d%I64d",&l,&r,&x);
l--;r--;
ll=l/k;rr=r/k;
for(i=ll*k;i<(ll+1)*k&&i<n;i++){
if(fei[i].id>=l&&fei[i].id<=r){
fei[i].x+=x;
}
}
sort(fei+ll*k,fei+min((ll+1)*k,n),cmp);
if(ll!=rr){
for(i=rr*k;i<n&&i<(rr+1)*k;i++){
if(fei[i].id>=l&&fei[i].id<=r){
fei[i].x+=x;
}
}
sort(fei+rr*k,fei+min((rr+1)*k,n),cmp);
}
for(i=ll+1;i<rr;i++){
b[i]+=x;
}
}
else{
scanf("%I64d",&y);
min1=max1=-1;
for(i=0;i<tot;i++){
tmp=BS2(i*k,min((i+1)*k-1,n-1),y-b[i]);
if(tmp!=-1&&fei[tmp].x==y-b[i]){
min1=fei[tmp].id;
break;
}
}
for(i=tot-1;i>=0;i--){
tmp=BS1(i*k,min((i+1)*k-1,n-1),y-b[i]);
if(tmp!=-1&&fei[tmp].x==y-b[i]){
max1=fei[tmp].id;
break;
}
}
if(min1==-1&&max1==-1){
puts("-1");
}
else printf("%d\n",max1-min1);
}
}
}
return 0;
}
Codeforces Round #307 (Div. 2) E. GukiZ and GukiZiana (分块)的更多相关文章
- 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/55 ...
- 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) 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 ...
随机推荐
- Kubernetes DNS安装配置
#镜像在谷歌上需要FQ###################################下载kubernetes#DNS#cd kubernetes/cluster/addons/dns#修改tr ...
- 在线查看PDF文档
http://www.cnblogs.com/morang/p/4598894.html http://78re52.com1.z0.glb.clouddn.com/resource%2Fscenar ...
- 使用Laravel将数据Excel导出的方法
1.copmposer下载maatwebsite/excel 2.在控制器引入:use Excel; 3.将要导出的数据处理成数组,第一组数据为表的字段名,如图 4.导出成表格 Excel::crea ...
- 二分LIS模板
假设存在一个序列d[1..9] = 2 1 5 3 6 4 8 9 7,可以看出来它的LIS长度为5. 下面一步一步试着找出它. 我们定义一个序列B,然后令 i = 1 to 9 逐个考察这个序列. ...
- Codeforces 946G Almost Increasing Array (树状数组优化DP)
题目链接 Educational Codeforces Round 39 Problem G 题意 给定一个序列,求把他变成Almost Increasing Array需要改变的最小元素个数. ...
- nodejs微服务
近来公司增加了nodejs微服务 它的主要任务是接收来自于现场的采集数据:作业记录和流转记录,动态构建一个基地的全景实时数据 暂时不涉及数据库. 如果要进行数据库操作,不建议使用本模块, ...
- FZU-2218 Simple String Problem(状态压缩DP)
原题地址: 题意: 给你一个串和两个整数n和k,n表示串的长度,k表示串只有前k个小写字母,问你两个不含相同元素的连续子串的长度的最大乘积. 思路: 状态压缩DP最多16位,第i位的状态表示第i位 ...
- 10.28 HTML DOM
- Parse error: syntax error, unexpected end of file in *.php on line * 解决方法
Parse error: syntax error, unexpected end of file in *.php on line * 解决方法 这篇文章主要介绍了PHP错误Parse erro ...
- Word中如何设置图片与段落的间距为半行
第一种: 正文为5号,那么图片或者Viso对象前后空一行,设置字号为7号或者更小,这样设置的间距就是那个7号字的间距,比5号小,看着空白不是那么大. 第二种: Visio对象转为jpg,然后选中图片和 ...