洛谷P3332 K大数查询

整体二分模板题

如果没有修改的询问区间第k小or大,一般把原始值看成赋值操作,这样可以把询问和赋值同时二分,正确性显然。

如果是单点修改,同样可以用树状数组赋值,修改一个数看-1再+1,因为每次增加和修改是成对出现的且二分不改变询问和修改的顺序,所以显然二分也是正确的。

区间插入的话,和普通的整体二分差不多,改成线段树维护区间小于mid的数的个数,把增加一个数看成是线段树上区间的加+1。

// luogu-judger-enable-o2
#include 
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
#define __fastIn ios::sync_with_stdio(false), cin.tie(0)
#define pb push_back
using namespace std;
using LL = long long;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
    int ret = 0, w = 0; char ch = 0;
    while(!isdigit(ch)){
        w |= ch == '-', ch = getchar();
    }
    while(isdigit(ch)){
        ret = (ret << 3) + (ret << 1) + (ch ^ 48);
        ch = getchar();
    }
    return w ? -ret : ret;
}
template 
inline A __lcm(A a, A b){ return a / __gcd(a, b) * b; }
template 
inline A fpow(A x, B p, C lyd){
    A ans = 1;
    for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
    return ans;
}
const int N = 50005;
int n, m, cur, cnt, ans[N], lazy[N<<2], cls[N<<2];
LL sum[N<<2];
struct Query{
    int op, id, x, y;
    LL k;
    Query(){}
    Query(int x, int y, LL k): x(x), y(y), k(k){
        op = 1, id = 0;
    }
    Query(int op, int id, int x, int y, LL k): op(op), id(id), x(x), y(y), k(k){}
}v[N<<1], lq[N<<1], rq[N<<1];

void push_up(int rt){
    sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}

void push_down(int rt, int l, int r){
    if(cls[rt]){
        cls[rt] = 0;
        lazy[rt << 1] = lazy[rt << 1 | 1] = 0;
        sum[rt << 1] = sum[rt << 1 | 1] = 0;
        cls[rt << 1] = cls[rt << 1 | 1] = 1;
    }
    if(lazy[rt]){
        int mid = (l + r) >> 1;
        sum[rt << 1] += lazy[rt] * (mid - l + 1);
        sum[rt << 1 | 1] += lazy[rt] * (r - mid);
        lazy[rt << 1] += lazy[rt], lazy[rt << 1 | 1] += lazy[rt];
        lazy[rt] = 0;
    }
}

void insert(int rt, int l, int r, int il, int ir, int val){
    if(l == il && r == ir){
        sum[rt] += (r - l + 1) * val;
        lazy[rt] += val;
        return;
    }
    push_down(rt, l, r);
    int mid = (l + r) >> 1;
    if(ir <= mid) insert(rt << 1, l, mid, il, ir, val);
    else if(il > mid) insert(rt << 1 | 1, mid + 1, r, il, ir, val);
    else insert(rt << 1, l, mid, il, mid, val), insert(rt << 1 | 1, mid + 1, r, mid + 1, ir, val);
    push_up(rt);
}

LL query(int rt, int l, int r, int ql, int qr){
    if(l == ql && r == qr){
        return sum[rt];
    }
    push_down(rt, l, r);
    int mid = (l + r) >> 1;
    if(qr <= mid) return query(rt << 1, l, mid, ql, qr);
    else if(ql > mid) return query(rt << 1 | 1, mid + 1, r, ql, qr);
    return query(rt << 1, l, mid, ql, mid) + query(rt << 1 | 1, mid + 1, r, mid + 1, qr);
}


void solve(int L, int R, int l, int r){
    if(l > r) return;
    if(L == R){
        for(int i = l; i <= r; i ++){
            if(v[i].op == 2) ans[v[i].id] = L;
        }
        return;
    }
    int mid = (L + R) >> 1;
    int lp = 0, rp = 0;
    for(int i = l; i <= r; i ++){
        if(v[i].op == 1){
            if(v[i].k > mid) insert(1, 1, n, v[i].x, v[i].y, 1), rq[++rp] = v[i];
            else lq[++lp] = v[i];
        }
        else{
            LL ret = query(1, 1, n, v[i].x, v[i].y);
            if(ret >= v[i].k) rq[++rp] = v[i];
            else v[i].k -= ret, lq[++lp] = v[i];
        }
    }
    cls[1] = 1, sum[1] = lazy[1] = 0;
    for(int i = 1; i <= lp; i ++) v[l + i - 1] = lq[i];
    for(int i = 1; i <= rp; i ++) v[l + lp + i - 1] = rq[i];
    solve(L, mid, l, l + lp - 1);
    solve(mid + 1, R, l + lp, r);
}

int main(){

    //freopen("data.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= m; i ++){
        int opt, x, y; LL k;
        scanf("%d%d%d%lld", &opt, &x, &y, &k);
        if(opt == 1){
            v[++cur] = Query(x, y, k);
        }
        else{
            v[++cur] = Query(2, ++ cnt, x, y, k);
        }
    }
    solve(0, n, 1, cur);
    for(int i = 1; i <= cnt; i ++){
        printf("%d\n", ans[i]);
    }
    return 0;
}

转载于:https://www.cnblogs.com/onionQAQ/p/11396791.html

你可能感兴趣的:(洛谷P3332 K大数查询)