题目地址: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 (分块)的更多相关文章

  1. 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 ...

  2. 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 ...

  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. 【原创】Linux环境下的图形系统和AMD R600显卡编程(3)——AMD显卡简介

    早期的显卡仅用于显示,后来显卡中加入了2D加速部件,这些部件用于做拷屏,画点,画线等操作.随着游戏.三维模拟以及科学计算可视化等需要,对3D的需求逐渐增加,早期图形绘制工作由CPU来完成,要达到真实感 ...

  2. jQuery的动画方法

    /* animate参数: 参数一:要改变的样式属性值,写成字典的形式 参数二:动画持续的时间,单位为毫秒,一般不写单位 参数三:动画曲线,默认为‘swing’,缓冲运动,还可以设置为‘linear’ ...

  3. Appium+python自动化11-adb必知必会的几个指令【转载】

    前言 学android测试,adb是必学的,有几个常用的指令需要熟练掌握 一.检查设备 1.如何检查手机(或模拟器)是连上电脑的,在cmd输入: >adb devices

  4. JDK7集合框架源码阅读(三) HashMap

    基于版本jdk1.7.0_80 java.util.HashMap 代码如下 /* * Copyright (c) 1997, 2010, Oracle and/or its affiliates. ...

  5. react this.props.form异步执行问题

    最近在做一个选择器联动时,碰到this.props.form的异步执行问题,导致选择器一直没有办法联动 如图,选择公司名称后,应该同步刷新门店选择默认值, 但同时又要清空门店选择的上一次记录 就用到了 ...

  6. hadoop之linux常用命令

    Linux的命令后面会有命令选项,有的选项还有选项值.选项的前面有短横线“-”,命令.选项.选项值之间使用空格隔开.有的命令没有选项,会有参数.选项是命令内置的功能,参数是用户提供的符合命令格式的内容 ...

  7. (转)Limboy:自学 iOS 开发的一些经验

    不知不觉作为 iOS 开发也有两年多的时间了,记得当初看到 OC 的语法时,愣是被吓了回去,隔了好久才重新耐下心去啃一啃.啃了一阵,觉得大概有了点概念,看到 Cocoa 那么多的 Class,又懵了, ...

  8. Union与UnionAll

    UNION指令的目的是将两个SQL语句的结果合并起来.从这个角度来看, 我们会产生这样的感觉,UNION跟JOIN似乎有些许类似,因为这两个指令都可以由多个表格中撷取资料. UNION的一个限制是两个 ...

  9. linux命令和工具

    环境搭建 lnmp环境搭建 命令 uname -a 查看linux版本 lsof -i:80 查看端口被那个程序占用 lsof -p pid号 查看引用的文件 netstat -apn|grep 80 ...

  10. Codeforces Round #535 (Div. 3) [codeforces div3 难度测评]

    hhhh感觉我真的太久没有接触过OI了 大约是前天听到JK他们约着一起刷codeforces,假期里觉得有些颓废的我忽然也心血来潮来看看题目 今天看codeforces才知道居然有div3了,感觉应该 ...