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 ...
随机推荐
- loj 数列分块入门 5 7 8
5 题意 给出一个长为\(n\)的数列,以及\(n\)个操作,操作涉及区间开方,区间求和. 思路 用\(tag\)记录这一块是否已全为\(1\). 除分块外,还可用 树状数组+并查集(链表) 或者 线 ...
- lambda calculus
;;;lambda calculus ;;;booleanstrue = \x.\y.xfalse = \x.\y.yif = \v.\t.\f. v t f ;;;exif true M N = M ...
- 《Linux命令行与shell脚本编程大全 第3版》Shell脚本编程基础---57
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:
- C/51单片机
1. 串口也可以有多根线的,但是各线之间没有协调同步发送,而是各自是独自发送的.并口是同步发送,同步一次8位同时成功同时失败,类比事务. 2. ASCII码的前32位是通讯预留的编码即使现 ...
- Python的功能模块[1] -> struct -> struct 在网络编程中的使用
struct模块 / struct Module 在网络编程中,利用 socket 进行通信时,常常会用到 struct 模块,在网络通信中,大多数传递的数据以二进制流(binary data)存在. ...
- ARC 098 C - Attention
Problem Statement There are N people standing in a row from west to east. Each person is facing east ...
- Loj #6019. 「from CommonAnts」寻找 LCM
给个链接:https://loj.ac/problem/6019 还是一道扩展卢卡斯+中国剩余定理....就当练练手 但是这题怎么这么卡常呢????!!!!! 在LOJ上死也过不去 (为什么要加那么多 ...
- POJ 1769 Minimizing maximizer(DP+zkw线段树)
[题目链接] http://poj.org/problem?id=1769 [题目大意] 给出一些排序器,能够将区间li到ri进行排序,排序器按一定顺序摆放 问在排序器顺序不变的情况下,一定能够将最大 ...
- Netbeans 中部署运行Webservice出错
错误如下 at java.lang.StackTraceElement at public java.lang.StackTraceElement[] java.lang.Throwable.ge ...
- 关于UIWebView设置高度自适应的问题
- (void)viewDidLoad { [super viewDidLoad]; _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMa ...