题目地址: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. 小Z爱图论(NOIP信(sang)心(bin)赛)From FallDream

    题目: 小Z最近喜欢上了图论,于是他研究了一下图的连通性问题.但是他遇到了一个难题. 给定一个n个点的有向图,求有多少点对(i,j)满足从i点出发能到达点j ? 小Z仅会简单的朴素算法,所以他想问问你 ...

  2. shell编程学习笔记【原创】

    本文为本人学习笔记,如有转载请注明出处,谢谢 一.Bourne Shell 有如下四种变量: 用户自定义变量 位置变量,即命令行参数 预定义变量 环境变量 二.位置变量 $ 与键入的命令行一样,包含脚 ...

  3. 关于SpringMVC的全局异常处理器

    近几天又温习了一下SpringMVC的运行机制以及原理 我理解的springmvc,是设计模式MVC中C层,也就是Controller(控制)层,常用的注解有@Controller.@RequestM ...

  4. [译]java8新特性:函数式编程(functional programming)的优点

    Java8引入了函数式编程,他对java是一个极大的扩展.Java从此不在是一个单纯的面向对象语言,现在他同时混合了函数式编程.这是巨大的改变,需要我们调整面对对象的编程习惯,以适应这些变化. 但是为 ...

  5. 常用Mysql查询语句

    1.查询数据表中重复记录 select user_name,count(*) as count from user_table group by user_name having count>1 ...

  6. (12)oracle事务

    事物 http://www.cnblogs.com/linjiqin/archive/2012/02/06/2340637.htm 在当前的事务中设置保存点 savepoint  名字; 保存点回滚 ...

  7. Jmeter插件之PerfMon监控插件使用说明

    PerfMon是Jmeter用来监控系统资源的一款插件,可以用来监控系统的cpu.内存.I/O等性能指标. 首先是Jmeter的插件安装,需要先下载JMeter Plugins Manager这个插件 ...

  8. codevs 1025 选菜——01背包

    时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 在小松宿舍楼下的不远处,有PK大学最不错的一个食堂—— ...

  9. iOS开发 Swift开发数独游戏(三) 选关界面

    一.选关界面涉及到的功能点 1)需要UITableView以及相应数据代理.协议的实现 2)读取plist文件并转化成模型 3)在单元格点击后进入数独游戏,涉及到把数据经segue在UIViewCon ...

  10. Android Developer -- Bluetooth篇 开发实例之四 API详解

    http://www.open-open.com/lib/view/open1390879771695.html 这篇文章将会详细解析BluetoothAdapter的详细api, 包括隐藏方法, 每 ...