zoukankan      html  css  js  c++  java
  • LOJ #6279. 数列分块入门 3

    (color{#0066ff}{题目描述})

    给出一个长为 n 的数列,以及 n 个操作,操作涉及区间加法,询问区间内小于某个值 x 的前驱(比其小的最大元素)。

    (color{#0066ff}{输入格式})

    第一行输入一个数字 n。

    第二行输入 n 个数字,第 i 个数字为 (a_i),以空格隔开。

    接下来输入 n 行询问,每行输入四个数字 (mathrm{opt}、l、r、c),以空格隔开。

    (mathrm{opt} = 0),表示将位于 ([l,r]) 的之间的数字都加 c。

    (mathrm{opt} = 1),表示询问 ([l,r]) 中 c 的前驱的值(不存在则输出 -1)。

    (color{#0066ff}{输出格式})

    对于每次询问,输出一行一个数字表示答案。

    (color{#0066ff}{输入样例})

    4
    1 2 2 3
    0 1 3 1
    1 1 4 4
    0 1 2 2
    1 1 2 4
    

    (color{#0066ff}{输出样例})

    3
    -1
    

    (color{#0066ff}{题解})

    开一个数组记录序列,保证块内有序

    对于区间加,如果是整块,打标记,因为相对大小不变,所以不用管数组

    如果是散块,暴力修改,同时改变数组,并重新排序

    对于询问,初始定为极小值

    对于整块,直接lowerbound-1找到区间内的前驱

    注意,如果找不到,返回的是l-1,所以特盘返回极小值

    因为找的时候无法让整个序列加上标记,所以lowerbound的时候要找x-标记

    最后返回答案的时候要把标记加上

    #include<cstdio>
    #include<queue>
    #include<vector>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<cctype>
    #include<cmath>
    #define _ 0
    #define LL long long
    #define Space putchar(' ')
    #define Enter putchar('
    ')
    #define fuu(x,y,z) for(int x=(y),x##end=z;x<=x##end;x++)
    #define fu(x,y,z)  for(int x=(y),x##end=z;x<x##end;x++)
    #define fdd(x,y,z) for(int x=(y),x##end=z;x>=x##end;x--)
    #define fd(x,y,z)  for(int x=(y),x##end=z;x>x##end;x--)
    #define mem(x,y)   memset(x,y,sizeof(x))
    #ifndef olinr
    inline char getc()
    {
    	static char buf[100001],*p1=buf,*p2=buf;
    	return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,100001,stdin),p1==p2)? EOF:*p1++;
    }
    #else
    #define getc() getchar()
    #endif
    template<typename T>inline void in(T &x)
    {
    	int f=1; char ch; x=0;
    	while(!isdigit(ch=getc()))(ch=='-')&&(f=-f);
    	while(isdigit(ch)) x=x*10+(ch^48),ch=getc();
    	x*=f;
    }
    const int inf=0x7fffffff;
    struct K
    {
    	int l,r,tag;
    	K() {l=inf,r=-inf;}
    }e[150505];
    struct seq
    {
    	int val,bel;
    }a[105050];
    int s[105050];
    int n,num;
    inline int lob(int l,int r,int c,int p)
    {
    	int t=std::lower_bound(s+l,s+r+1,c)-s-1;
    	if(t==l-1) return -inf;
    	return s[t]+e[p].tag;
    }
    inline void init()
    {
    	num=std::sqrt(n);
    	fuu(i,1,n)
    	{
    		in(a[i].val),a[i].bel=(i-1)/num+1,s[i]=a[i].val;
    		e[a[i].bel].l=std::min(e[a[i].bel].l,i);
    		e[a[i].bel].r=std::max(e[a[i].bel].r,i);
    	}
    	for(int i=1;i<=n;i+=num) std::sort(s+e[a[i].bel].l,s+e[a[i].bel].r+1);
    }
    inline void add(int l,int r,int c)
    {
    	fuu(i,a[l].bel+1,a[r].bel-1) e[i].tag+=c;
    	fuu(i,l,std::min(r,e[a[l].bel].r)) a[i].val+=c;
    	fuu(i,e[a[l].bel].l,e[a[l].bel].r) s[i]=a[i].val;
    	std::sort(s+e[a[l].bel].l,s+e[a[l].bel].r+1);
    	if(a[l].bel!=a[r].bel)
    	{
    		fuu(i,std::max(l,e[a[r].bel].l),r) a[i].val+=c;
    		fuu(i,e[a[r].bel].l,e[a[r].bel].r) s[i]=a[i].val;
    		std::sort(s+e[a[r].bel].l,s+e[a[r].bel].r+1);
    	} 
    }
    inline int query(int l,int r,int c)
    {
    	int ans=-inf;
    	fuu(i,a[l].bel+1,a[r].bel-1) ans=std::max(ans,lob(e[i].l,e[i].r,c-e[i].tag,i));
    	fuu(i,l,std::min(r,e[a[l].bel].r)) if(a[i].val+e[a[i].bel].tag<c) ans=std::max(ans,a[i].val+e[a[i].bel].tag);
    	if(a[l].bel!=a[r].bel) 	fuu(i,std::max(l,e[a[r].bel].l),r) if(a[i].val+e[a[i].bel].tag<c) ans=std::max(ans,a[i].val+e[a[i].bel].tag);
    	return ans==-inf? -1:ans;
    }
    int main()
    {
    	in(n);
    	int p,l,r,c;
    	init();
    	while(n--)
    	{
    		in(p),in(l),in(r),in(c);
    		if(p==0) add(l,r,c);
    		else printf("%d
    ",query(l,r,c));
    	}
    	return ~~(0^_^0);
    }
    
  • 相关阅读:
    idea--不能启动问题
    linux--mysql5.7安装
    vmware
    debezium
    java枚举
    springfox
    日志级别
    lombok--知识点
    es6--箭头函数
    网址访问过慢
  • 原文地址:https://www.cnblogs.com/olinr/p/10066659.html
Copyright © 2011-2022 走看看