CodeForces 551E GukiZ and GukiZiana 【分块】
来源:互联网 发布:数据库中的模式是 编辑:程序博客网 时间:2024/04/29 22:51
题目:点击打开链接
题意:对于给定数列,操作一是把[l,r]区间内的数加x,操作二是求出一个值,该值为数x出现的最右位置减最左位置,若不存在x输出为-1。
思路:对于这种迷之询问,用分块大法。将n个数分成sqrt(n)块,初始化将a数组赋值给d数组,将d数组分块从小到大排序,新建一个数组p储存每一块所加的和,更新时,如果l,r都在同一块,暴力更新a并再排序d,若不在同一块,对于l所在块,暴力找出所有本来位置在a数组l和l之后的数加上x再给d排序,对于r所在块,暴力找出所有本来位置在a数组r和r之前的数加上x再给d排序,对于中间的块,则p数组加上x。二分查询时,将x减去p数组的对应的值,对于d从左往右每一块进行二分查找该数的最左端位置,找到后再继续二分查找每一块的该数的最右端位置。若找不到最左端的数则输出-1,否则输出两位置之差。
代码:
#include <stdio.h>#include <cmath>#include <algorithm>#include <queue>#include <set>#include <vector>#include <map>#define PR pair<int,int>#define MP make_pair#define fi first#define se second#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define sqr(x) ((x)*(x))#define LL long longconst LL INF = 1e18;const int inf=0x3f3f3f3f;const int M=18;const int N=500100;const int MOD=100000;const double eps=1e-10;const double pi=acos(-1.0);using namespace std;int n,m,belong[N],l[N],r[N],block,num,op,x,y,z;LL p[N];struct node{ LL v; int id; bool operator < (const node t) const { if(v!=t.v) return v<t.v; return id<t.id; }}a[N],d[N];void build(){ int i,j; block=sqrt(n); num=n/block;if(n%block) num++; for(i=1;i<=num;i++) l[i]=(i-1)*block+1,r[i]=i*block; r[num]=n; for(i=1;i<=n;i++) belong[i]=(i-1)/block+1,d[i]=a[i]; for(i=1;i<=num;i++) sort(d+l[i],d+r[i]+1);}void update(int L,int R,int z){ int i,ll=belong[L],rr=belong[R]; if(ll==rr) { for(i=L;i<=R;i++) a[i].v+=z; for(i=l[ll];i<=r[ll];i++) d[i]=a[i]; sort(d+l[ll],d+r[ll]+1); return; } for(i=L;i<=r[ll];i++) a[i].v+=z; for(i=l[ll];i<=r[ll];i++) d[i]=a[i]; sort(d+l[ll],d+r[ll]+1); for(i=ll+1;i<rr;i++) p[i]+=z; for(i=l[rr];i<=R;i++) a[i].v+=z; for(i=l[rr];i<=r[rr];i++) d[i]=a[i]; sort(d+l[rr],d+r[rr]+1);}int bisearch1(int L,int R,LL x){ int l=L,r=R; while(l<r) { int m=l+(r-l)/2; if(d[m].v<x) l=m+1; else r=m; } return d[r].v==x?d[r].id:-1;}int bisearch2(int L,int R,LL x){ int l=L,r=R; while(l<r) { int m=l+(r-l+1)/2; if(d[m].v<=x)l=m; else r=m-1; } return d[l].v==x?d[l].id:-1;}int query(int x){ int i,j,lpos=-1,rpos=-1; for(i=1;i<=num;i++) { if(lpos==-1) lpos=bisearch1(l[i],r[i],x-p[i]); rpos=max(rpos,bisearch2(l[i],r[i],x-p[i])); } if(lpos==-1||rpos==-1) return -1; else return rpos-lpos;}int main(){ int i; scanf("%d%d",&n,&m); for(i=1;i<=n;i++)scanf("%I64d",&a[i].v),a[i].id=i; build(); while(m--) { scanf("%d",&op); if(op==1) scanf("%d%d%d",&x,&y,&z),update(x,y,z); else scanf("%d",&x),printf("%d\n",query(x)); }}
0 0
- Codeforces 551E GukiZ and GukiZiana (分块)
- codeforces 551E GukiZ and GukiZiana 分块
- Codeforces 551E GukiZ and GukiZiana 分块
- CodeForces 551E GukiZ and GukiZiana 【分块】
- codeforces 551E GukiZ and GukiZiana 分块
- codeforces 551E GukiZ and GukiZiana 分块
- Codeforces 551 E. GukiZ and GukiZiana (分块搜索)
- CodeForces 551E GukiZ and GukiZiana (分块)
- codeforces 551 E. GukiZ and GukiZiana (分块)
- CodeForces 551E GukiZ and GukiZiana(分块算法)
- 【分块】 CF 551 E GukiZ and GukiZiana
- codeforces 551 E. GukiZ and GukiZiana
- codeforces #307 E. GukiZ and GukiZiana(分块+二分查找)
- codeforces #307 E. GukiZ and GukiZiana (分块)
- Codeforces Round #307 (Div. 2) 551E - GukiZ and GukiZiana 分块
- Codeforces Round #307 (Div. 2)E. GukiZ and GukiZiana(分块)
- Codeforces Round #307 (Div. 2) E. GukiZ and GukiZiana (分块)
- 分块法 CF551E GukiZ and GukiZiana
- 为什么设计泛型?
- ALGORITHMIC COMPOSITION AS A MODEL OF CREATIVITY
- 52. N-Queens II
- const 的简要介绍
- 汉诺塔游戏中的递归算法
- CodeForces 551E GukiZ and GukiZiana 【分块】
- 单片机中用lcd显示电池充电
- hdu 5573 Binary Tree
- 以生活例子说明单线程与多线程
- Linux_Hi3518 SDK 安装以及编译出错分析
- java小知识点(1)
- 欢迎使用CSDN-markdown编辑器
- Android 小项目之---- 计算器 使用同一个事件监听完成 加减乘
- 打不死的redis集群