加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

【BZOJ3110】【ZJOI2013】【整体二分、树套树模板题】K大数查询

发布时间:2020-12-14 04:51:33 所属栏目:大数据 来源:网络整理
导读:Description 有N个位置,M个操作。操作有两种: 1. 每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c; 2. 如果是2 a b c形式,表示询问从第a个位置到第b个位置,第c大的数是多少。 Solution 2.1 整体二分 看了好久的整体二分,

Description

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

Solution

2.1 整体二分

看了好久的整体二分,一直脑残不能理解,感觉挺晕的,,其实挺好理解的,就是把修改和询问放在一起二分答案,每次把修改和询问分成两个部分递归下去即可,这里可以用线段树维护。

当然实现的时候还是有很多细节需要注意的,具体多看看代码吧。

2.2 树套树

待填坑。。。

Source

/************************************** * Au: Hany01 * Prob: [BZOJ3110][ZJOI2013] K大数查询 * Date: Mar 19th,2018 * Email: hany01@foxmail.com **************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define File(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout)
#define Rep(i,j) for (register LL i = 0,i##_end_ = j; i < i##_end_ ; ++ i)
#define For(i,j,k) for (register LL i = (j),i##_end_ = (k) ; i <= i##_end_ ; ++ i)
#define Fordown(i,i##_end_ = (k) ; i >= i##_end_ ; -- i)
#define Set(a,b) memset(a,b,sizeof(a))
#define SZ(a) ((int)(a.size()))
#define ALL(a) a.begin(),a.end()
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define y1 wozenmezhemecaia 
#ifdef hany01
#define debug(...) fprintf(stderr,__VA_ARGS__)
#else
#define debug(...)
#endif

template<typename T> inline bool chkmax(T &a,T b) { return a < b ? a = b,1 : 0; }
template<typename T> inline bool chkmin(T &a,T b) { return b < a ? a = b,1 : 0; }

inline LL read() {
    register char c_; register LL _,__;
    for (_ = 0,__ = 1,c_ = getchar() ; !isdigit(c_) ; c_ = getchar()) if (c_ == '-')  __ = -1;
    for ( ; isdigit(c_) ; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const LL maxn = 50005;

struct Operator {
    LL type,l,r,v,ans,id;
    bool operator < (const Operator& A) const { return id < A.id; }
}Q[maxn],t[2][maxn];
LL m,n;

struct SegmentTree
{
    LL tr[maxn << 3],tag[maxn << 3];
#define lc (t << 1)
#define rc (lc | 1)
#define mid ((l + r) >> 1)

    inline void pushdown(LL t,LL l,LL r)
    {
        if (!tag[t]) return;
        tr[lc] += (mid - l + 1) * tag[t],tr[rc] += (r - mid) * tag[t];
        tag[lc] += tag[t],tag[rc] += tag[t],tag[t] = 0;
    }

    void update(LL t,LL r,LL x,LL y,LL dt)
    {
        if (x <= l && r <= y) {
            tr[t] += dt * (r - l + 1),tag[t] += dt;
            return ;
        }
        pushdown(t,r);
        if (mid >= x) update(lc,mid,x,y,dt);
        if (y > mid) update(rc,mid + 1,dt);
        tr[t] = tr[lc] + tr[rc];
    }

    LL query(LL t,LL y)
    {
        if (x <= l && r <= y) return tr[t];
        pushdown(t,r);
        if (mid >= y) return query(lc,y);
        if (mid <  x) return query(rc,y);
        return query(lc,y) + query(rc,y);
    }

#undef mid
}tr;

#define ins(ty,i) t[ty][++ cnt[ty]] = Q[i]

//Notice: Though the array has been disordered to a certain extent,they are still ordered in terms of time. That's why we can do these things directly.
void BinarySearch(LL ql,LL qr,LL vl,LL vr)
{
    //Judge special situations
    if (ql > qr) return;
    if (vl == vr) {
        For(i,ql,qr) if (Q[i].type == 2) Q[i].ans = vl;
        return;
    }

    //Divide them into 2 group
    register LL mid = (vl + vr) >> 1,cnt[2] = {0};
    For(i,qr)
        if (Q[i].type - 1) //Query
        {
            register LL tmp = tr.query(1,1,n,Q[i].l,Q[i].r);
            if (tmp >= Q[i].v) ins(1,i);
            else Q[i].v -= tmp,ins(0,i); //Remember to minus 'tmp' because we have ignored the known contributions !!
        }
        else //Update
            if (Q[i].v > mid) //Right
                ins(1,i),tr.update(1,Q[i].r,1);
            else ins(0,i); //Left

    //Restore
    For(i,cnt[0]) Q[ql + i - 1] = t[0][i];
    For(i,cnt[1]) {
        Q[ql + cnt[0] + i - 1] = t[1][i];
        if (t[1][i].type == 1) tr.update(1,t[1][i].l,t[1][i].r,-1);
        //We must clear it one by one to make sure the time complexity is right.(It's the same in CDQ)
    }

    BinarySearch(ql,ql + cnt[0] - 1,vl,mid),BinarySearch(ql + cnt[0],qr,vr);
}

int main()
{
#ifdef hany01
    File("bzoj3110");
#endif

    n = read(),m = read();
    For(i,m)
        Q[i].type = read(),Q[i].l = read(),Q[i].r = read(),Q[i].v = read(),Q[i].id = i;

    BinarySearch(1,m,-n,n);

    sort(Q + 1,Q + 1 + m);
    For(i,m) if (Q[i].type == 2) printf("%lldn",Q[i].ans);

    return 0;
}
//官仓老鼠大如斗,见人开仓亦不走。
//健儿无粮百姓饥,谁遣朝朝入君口。 
// --曹邺《官仓鼠》

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读