校内模拟-双面间谍(主席树)

2019-04-09 14:53:31 浏览数 (1)

题意

题目链接

给出两个数组(A, B),每次询问(l, r)。需要最小化(sum_{i=l}^r max{|a-A_i|, |b-B_i| })

Sol

Orzzzzbq

不想写题解了。放一个SovietPower大爷的题解。。

代码语言:javascript复制
#include<bits/stdc  .h> 
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second 
#define LL long long
//#define int long long  
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 1e6   10;
const LL lim = 2e9;
const double eps = 1e-9;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A> inline void debug(A a){cout << a << 'n';}
template <typename A> inline LL sqr(A x){return 1ll * x * x;}
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10   c - '0', c = getchar();
    return x * f;
}
int N, Q, top;
LL A[MAXN], B[MAXN];
struct ZBQtxdy {
#define SS MAXN * 5
    int rt[MAXN], ls[SS], rs[SS], siz[SS], tot;
    LL sum[SS], ans;
    void update(int k) {
        sum[k] = sum[ls[k]]   sum[rs[k]];
    }
    void insert(int &k, int p, LL l, LL r, LL pos) {
        k =   tot;
        ls[k] = ls[p]; rs[k] = rs[p]; sum[k] = sum[p]; siz[k] = siz[p]   1;
        if(l == r) {sum[k]  = pos; return ;}
        LL mid = l   r >> 1;
        if(pos <= mid) insert(ls[k], ls[p], l, mid, pos);
        if(pos  > mid) insert(rs[k], rs[p], mid   1, r, pos);
        update(k);
    }
    LL Query(int rt, int p, LL l, LL r, int K) {
        if(l == r) {ans = l; return 0;}
        LL mid = l   r >> 1, lsiz = siz[ls[rt]] - siz[ls[p]];
        LL ret = 0;
        if(lsiz >= K) {
            ret = Query(ls[rt], ls[p], l, mid, K);
            return ret   (sum[rs[rt]] - sum[rs[p]]) - ans * (siz[rs[rt]] - siz[rs[p]]);
        }
        else {
            ret = Query(rs[rt], rs[p], mid   1, r, K - lsiz);
            return ret   (siz[ls[rt]] - siz[ls[p]]) * ans - (sum[ls[rt]] - sum[ls[p]]);
        }
    }
#undef SS
}T[2];
signed main() {
    N = read(); Q = read();
    for(int i = 1; i <= N; i  ) A[i] = read();
    for(int i = 1; i <= N; i  ) {
        B[i] = read();
        T[0].insert(T[0].rt[i], T[0].rt[i - 1], -lim, lim, A[i]   B[i]);
        T[1].insert(T[1].rt[i], T[1].rt[i - 1], -lim, lim, A[i] - B[i]);
    }
    while(Q--) {
        int l = read(), r = read(), kk = (r - l   1) / 2   1;
        LL ans = T[0].Query(T[0].rt[r], T[0].rt[l - 1], -lim, lim, kk); 
        ans  = T[1].Query(T[1].rt[r], T[1].rt[l - 1], -lim, lim, kk);
        printf("%.2lfn", (double) ans / 2);
    }
    return 0;
}

0 人点赞