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

    内存限制:256 MiB时间限制:500 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] 中,小于 c2c^2c2​​ 的数字的个数。

    输出格式

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

    样例

    样例输入

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

    样例输出

    3
    0
    2

    数据范围与提示

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

    对于区间和我们可以按照正常思路做。

    对于第二个询问,我们可以用vector维护每个块内的有序表,

    零散的块直接暴力,否则在vector内二分

    注意vector的编号是从0开始的

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<vector>
    using namespace std;
    const int MAXN=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[1001];//用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=0;
        for(int i=l;i<=min(r,R[l]);i++) 
            if(a[i]+tag[belong[l]]<val) ans++;
        if(belong[l]!=belong[r])
            for(int i=L[r];i<=r;i++)
                if(a[i]+tag[belong[r]]<val) ans++;
        for(int i=belong[l]+1;i<=belong[r]-1;i++)
        {
            int x=val-tag[i];
            ans+=lower_bound(v[i].begin(),v[i].end(),x)-v[i].begin();
        }
        return ans;
    }
    int main()
    {
        #ifdef WIN32
        freopen("a.in","r",stdin);
        freopen("b.out","w",stdout);
        #else
        #endif
        N=read();block=sqrt(N);
        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*c));
        }
        return 0;
    }
  • 相关阅读:
    JVM内存区域与内存溢出异常
    蓄水池抽样算法应用
    InnoDB引擎的索引和存储结构
    ASP.NET Core Web API 集成测试中使用 Bearer Token
    ASP.NET Core Web API 集成测试
    测试 ASP.NET Core API Controller
    使用 coverlet 查看.NET Core应用的测试覆盖率
    使用 Moq 测试.NET Core 应用 -- 其它
    使用 Moq 测试.NET Core 应用 -- Mock 行为
    使用 Moq 测试.NET Core 应用 -- Mock 属性
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/8445216.html
Copyright © 2011-2022 走看看