3110: [Zjoi2013]K大数查询

Description

有N个位置,M个操作。操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c
如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数是多少。

Input

第一行N,M
接下来M行,每行形如1 a b c或2 a b c

Output

输出每个询问的结果

Sample Input

2 5
1 1 2 1
1 1 2 2
2 1 1 2
2 1 1 1
2 1 2 3

Sample Output

1
2

1

思路:线段树套线段树,外层线段树表示权值,内层线段树维护出现的位置及次数

PS:空间动态开。这里懒惰标记可以不PushDown,感觉PushDown会访问一些本来可以不访问也就是不用开辟的节点,省空间。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 50080
#define maxm 30058000
int root[maxn<<2];
int sum[maxm],lazy[maxm],lson[maxm],rson[maxm];
int n,m,cnt;
void scanf_f(int & x)
{
    x = 0;
    char c = getchar();
    while(!(c >= '0' && c <= '9'))
        c = getchar();
    while(c >= '0' && c <= '9')
    {
        x = x*10 + c - '0';
        c = getchar();
    }
}
/*
void PushDown(int node,int l,int r)
{
    if(lazy[node])
    {
        int mid = (l+r) >> 1;
        if(lson[node] == 0) lson[node] = ++cnt;
        if(rson[node] == 0) rson[node] = ++cnt;
        sum[lson[node]] += lazy[node]*(mid-l+1);
        lazy[lson[node]] += lazy[node];
        sum[rson[node]] += lazy[node]*(r-mid);
        lazy[rson[node]] += lazy[node];
    }
    lazy[node] = 0;
}
*/
void PushUp(int node,int l,int r)
{
    sum[node] = sum[lson[node]] + sum[rson[node]] + lazy[node]*(r-l+1);
}
void add(int & node,int L,int R,int l,int r)
{
    if(!node)   node = ++cnt;
    if(L == l && R == r)
    {
        sum[node] += r-l+1;
        lazy[node]++;
        return;
    }
    //PushDown(node,L,R);
    int mid = (L+R) >> 1;
    if(r <= mid)    add(lson[node],L,mid,l,r);
    else if(l > mid)    add(rson[node],mid+1,R,l,r);
    else add(lson[node],L,mid,l,mid),add(rson[node],mid+1,R,mid+1,r);
    PushUp(node,L,R);
}
void Add(int l,int r,int k)
{
    int node = 1;
    int L = 1,R = n;
    while(L < R)
    {
        int mid = (L+R) >> 1;
        add(root[node],1,n,l,r);
        if(k > mid)
        {
            node = node << 1 | 1;
            L = mid+1;
        }
        else
        {
            node = node << 1;
            R = mid;
        }
    }
    add(root[node],1,n,l,r);
}
int query(int node,int L,int R,int l,int r)
{
    if(!node)   return 0;
    if(L == l && R == r)    return sum[node];
    //PushDown(node,L,R);
    int ans = lazy[node]*(r-l+1);
    int mid = (L+R) >> 1;
    if(mid >= r)   return query(lson[node],L,mid,l,r)+ans;
    else if(mid < l)    return query(rson[node],mid+1,R,l,r)+ans;
    else return query(lson[node],L,mid,l,mid) + query(rson[node],mid+1,R,mid+1,r) + ans;
}

int Query(int l,int r,int k)
{
    int node = 1;
    int L = 1,R = n;
    while(L < R)
    {
        int mid = (L+R) >> 1;
        int temp = query(root[node<<1|1],1,n,l,r);
        if(temp >= k)
        {
            L = mid+1;  node = node << 1 | 1;
        }
        else
        {
            R = mid;    node = node << 1;
            k -= temp;
        }
    }
    return L;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i = 1;i <= m;i++)
    {
        int opt,l,r,c;
        //scanf("%d%d%d%d",&opt,&l,&r,&c);
        scanf_f(opt);
        scanf_f(l);
        scanf_f(r);
        scanf_f(c);
        if(opt == 1)
        {
            Add(l,r,c);
        }
        else
        {
            printf("%d\n",Query(l,r,c));
        }
    }
    return 0;
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值