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

    内存限制:256 MiB时间限制:1500 ms标准输入输出
    题目类型:传统评测方式:文本比较
    上传者: hzwer

    题目描述

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

    输入格式

    第一行输入一个数字 nnn。

    第二行输入 nnn 个数字,第 i 个数字为 aia_iai​​,以空格隔开。

    接下来输入 nnn 行询问,每行输入四个数字 optmathrm{opt}opt、lll、rrr、ccc,以空格隔开。

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

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

    输出格式

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

    样例

    样例输入

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

    样例输出

    3
    -1

    数据范围与提示

    对于 100% 100\%100% 的数据,1≤n≤100000,−231≤others 1 leq n leq 100000, -2^{31} leq mathrm{others}1n100000,231​​others、ans≤231−1 mathrm{ans} leq 2^{31}-1ans231​​1。

    跟上一道题一样。

    改一下二分就好了

    mmp写错了一个地方调了一晚上

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<vector>
    using namespace std;
    const int MAXN=3*1e6+10;
    const int INF=1e8+10;
    inline char nc()
    {
        static char buf[MAXN],*p1=buf,*p2=buf;
        return p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXN,stdin),p1==p2)?EOF:*p1++;
    }
    inline int read()
    {
        char c=nc();int x=0,f=1;
        while(c<'0'||c>'9'){if(c=='-')f=-1;c=nc();}
        while(c>='0'&&c<='9'){x=x*10+c-'0';c=nc();}
        return x*f;
    }
    vector<int>v[10001];//用vector储存分块后块内的有序表
    int block,L[MAXN],R[MAXN],a[MAXN],tag[MAXN],belong[MAXN],N;
    void Sort(int p)
    {
        v[p].clear();
        for(int i=L[p*block];i<=min(R[p*block],N);i++)
            v[p].push_back(a[i]);
        sort(v[p].begin(),v[p].end());
    }
    void IntervalAdd(int l,int r,int val)
    {
        for(int i=l;i<=min(r,R[l]);i++) a[i]+=val;
        Sort(belong[l]);
        if(belong[l]!=belong[r])
        {
            for(int i=L[r];i<=r;i++)     a[i]+=val;
            Sort(belong[r]);
        }
        for(int i=belong[l]+1;i<=belong[r]-1;i++) tag[i]+=val;    
    }
    int Query(int l,int r,int val)
    {
        int ans=-1;
        for(int i=l;i<=min(r,R[l]);i++) 
            if(a[i]+tag[belong[l]]<val) 
                ans=max(ans,a[i]+tag[belong[l]]);
        if(belong[l]!=belong[r])
            for(int i=L[r];i<=r;i++)
                if(a[i]+tag[belong[r]]<val) ans=max(ans,a[i]+tag[belong[r]]);
        for(int i=belong[l]+1;i<=belong[r]-1;i++)
        {
            int x=val-tag[i];
            int pos=lower_bound(v[i].begin(),v[i].end(),x)-v[i].begin();
            if(pos-1>=0) ans=max(ans,v[i][pos-1]+tag[i]);
        }
        return ans;
    }
    int main()
    {
        #ifdef WIN32
        freopen("a.in","r",stdin);
        freopen("b.out","w",stdout);
        #else
        #endif
        N=read();block=sqrt(N+0.5);
        for(int i=1;i<=N;i++) a[i]=read();
        for(int i=1;i<=N;i++) belong[i]=(i-1)/block+1,L[i]=(belong[i]-1)*block+1,R[i]=belong[i]*block;
        for(int i=1;i<=N;i++) v[belong[i]].push_back(a[i]);
        for(int i=1;i<=belong[N];i++) Sort(i);
        for(int i=1;i<=N;i++)
        {
            int opt=read(),l=read(),r=read(),c=read();
            if(opt==0) IntervalAdd(l,r,c);
            else       printf("%d
    ",Query(l,r,c));
        }
        return 0;
    }
  • 相关阅读:
    jquery load加载页面内ajax返回的div不能响应页面js的问题的解决方案
    JQuery跳出each循环的方法(包含数组遍历)
    fpm 配置详解
    curl模拟Http请求
    git 修改commit信息
    git-ssh 配置和使用
    初始化目录,并且添加到远程仓库
    【PHP代码审计】 那些年我们一起挖掘SQL注入
    【PHP代码审计】 那些年我们一起挖掘SQL注入
    【PHP代码审计】 那些年我们一起挖掘SQL注入
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/8445611.html
Copyright © 2011-2022 走看看