[JZOJ4438]K小数查询

15 篇文章 0 订阅
10 篇文章 0 订阅

题目大意

一个数组 a1...n ,有 q 个操作。
操作分成2种:
1 l r x:将第 l 到第r个数全部加上 x
2 l r k:询问第 l 个到第r个数中第 k

1n,q80000
在任何时候,都有 |ai|5×106


题目分析

分块大法好!!!
显然这题要使用分块大法。于是问题来了,怎么查询第 k 小呢?
使用分块大法,我们可以很方便地查询区间内比某个数小的数的个数(块内维护排序,查询时二分)。那么我们就可以二分答案,转化为判定性问题:是否有且仅有k1个数小于二分值,然后再使用上面的方法判定即可。
区间加对于完整的块直接打标记,否则暴力重构。
时间复杂度 O(qlog2anlog2n) 。时限7000ms我怕谁?


代码实现

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cctype>
#include <cmath>

using namespace std;

int read()
{
    int x=0,f=1;
    char ch=getchar();
    while (!isdigit(ch))
    {
        if (ch=='-')
            f=-1;
        ch=getchar();
    }
    while (isdigit(ch))
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}

const int N=80050;
const int B=450;
const int A=5000000;

int tag[B],st[B],en[B];
int a[N],c[N];
int n,q,bs,s;

void build()
{
    s=trunc(sqrt(n))+1;
    bs=n/s;
    int p=1;
    for (int i=1;i<=bs;i++,p+=s)
    {
        st[i]=p,en[i]=p+s-1;
        for (int j=st[i];j<=en[i];j++)
            c[j]=a[j];
        sort(c+st[i],c+en[i]+1);
    }
    if (p<=n)
    {
        bs++;
        st[bs]=p,en[bs]=n;
        for (int j=st[bs];j<=en[bs];j++)
            c[j]=a[j];
        sort(c+st[bs],c+en[bs]+1);
    }
}

void rebuild(int x)
{
    for (int i=st[x];i<=en[x];i++)
        c[i]=a[i];
    sort(c+st[x],c+en[x]+1);
}

void change(int l,int r,int edit)
{
    int lb=(l-1)/s+1,rb=(r-1)/s+1;
    if (lb==rb)
    {
        for (int i=l;i<=r;i++)
            a[i]+=edit;
        rebuild(lb);
    }
    else
    {
        for (int i=l;i<=s*lb;i++)
            a[i]+=edit;
        rebuild(lb);
        for (int i=r;i>=s*(rb-1)+1;i--)
            a[i]+=edit;
        rebuild(rb);
        for (int i=lb+1;i<rb;i++)
            tag[i]+=edit;
    }
}

int count(int x,int y)
{
    int ret=st[x]-1,l=st[x],r=en[x],mid;
    while (l<=r)
    {
        mid=l+r>>1;
        if (c[mid]+tag[x]<y)
            ret=mid,l=mid+1;
        else
            r=mid-1;
    }
    return ret-st[x]+1;
}

int calc(int l,int r,int x)
{
    int lb=(l-1)/s+1,rb=(r-1)/s+1,ret=0;
    if (lb==rb)
    {
        for (int i=l;i<=r;i++)
            ret+=((a[i]+tag[lb])<x);
    }
    else
    {
        for (int i=l;i<=s*lb;i++)
            ret+=((a[i]+tag[lb])<x);
        for (int i=r;i>=s*(rb-1)+1;i--)
            ret+=((a[i]+tag[rb])<x);
        for (int i=lb+1;i<rb;i++)
            ret+=count(i,x);
    }
    return ret;
}

int query(int l,int r,int k)
{
    int ret=0,L=-A,R=A,mid;
    while (L<=R)
    {
        mid=L+R>>1;
        if (calc(l,r,mid)<k)
        {
            ret=mid;
            L=mid+1;
        }
        else
            R=mid-1;
    }
    return ret;
}

int main()
{
    freopen("gs.in","r",stdin);
    freopen("gs.out","w",stdout);
    n=read();
    for (int i=1;i<=n;i++)
        a[i]=read();
    build();
    q=read();
    for (int i=1,t,l,r,x;i<=q;i++)
    {
        t=read(),l=read(),r=read(),x=read();
        if (t==1)
            change(l,r,x);
        else
            printf("%d\n",query(l,r,x));
    }
    fclose(stdin);
    fclose(stdout);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值