HDU 6621 (2019杭电第四场 1008) K-th Closest Distance (主席树 + 二分, 求第 k 小绝对值)

K-th Closest Distance

Time Limit: 20000/15000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 654    Accepted Submission(s): 205

Problem Description

You have an array: a1, a2, , an and you must answer for some queries.
For each query, you are given an interval [L, R] and two numbers p and K. Your goal is to find the Kth closest distance between p and aL, aL+1, ..., aR.
The distance between p and ai is equal to |p - ai|.
For example:
A = {31, 2, 5, 45, 4 } and L = 2, R = 5, p = 3, K = 2.
|p - a2| = 1, |p - a3| = 2, |p - a4| = 42, |p - a5| = 1.
Sorted distance is {1, 1, 2, 42}. Thus, the 2nd closest distance is 1.

 

Input

The first line of the input contains an integer T (1 <= T <= 3) denoting the number of test cases.
For each test case:
冘The first line contains two integers n and m (1 <= n, m <= 10^5) denoting the size of array and number of queries.
The second line contains n space-separated integers a1, a2, ..., an (1 <= ai <= 10^6). Each value of array is unique.
Each of the next m lines contains four integers L', R', p' and K'.
From these 4 numbers, you must get a real query L, R, p, K like this: 
L = L' xor X, R = R' xor X, p = p' xor X, K = K' xor X, where X is just previous answer and at the beginning, X = 0.
(1 <= L < R <= n, 1 <= p <= 10^6, 1 <= K <= 169, R - L + 1 >= K).

 

Output

For each query print a single line containing the Kth closest distance between p and aL, aL+1, ..., aR.

 

Sample Input

1 5 2 31 2 5 45 4 1 5 5 1 2 5 3 2

 

Sample Output

0 1

题意:给一个数组,每次给 l ,r, p, k,问区间 [l, r] 的数与 p 的绝对值的第 k 小,这个绝对值是多少。。

思路:其实这个一个很简单的问题,比赛的时候不知道脑子在瞎xx想啥,一定要给自己带个169,emmmmmm(mdzz)

二分绝对值 ans , 那么可以确定一个值域 [p - ans, p + ans],可以利用主席树(查询区间<= k 的数的个数),然后分别查询这两个值,做差就得到了 [p - ans, p + ans] 中有多少个数,如果个数 >= k,这个ans就是合法的。

Code:

#include
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define ull unsigned LL
#define ls i << 1
#define rs (i << 1) + 1
#define fir first
#define sec second
#define CLR(a) while(!(a).empty()) a.pop()

using namespace std;

const int maxn = 1e5 + 10;
const int M = maxn * 40;
int T[maxn];///T[i]表示第i颗线段树的顶结点
int lson[M],rson[M],c[M];///C[i]就代表第i个点的权值,即上述权值线段树的sum
int X[maxn],K[maxn];     /// lson[i], rson[i] 表示第 i 个点的左儿子,右儿子是谁
int tot = 0,en;          /// tot 用于动态开点

inline int read() {
    int X = 0, w = 0;
    char ch = 0;
    while(!isdigit(ch)) {
        w |= ch == '-';
        ch = getchar();
    }
    while(isdigit(ch))
        X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
    return w ? -X : X;
}

void build(int &i,int l,int r){
    i = ++ tot;
    c[i] = 0;
    if(l == r) return ;
    int mid = (l + r) >> 1;
    build(lson[i],l,mid);
    build(rson[i],mid + 1,r);
}

void update(int l,int r,int &i,int last,int pos,int val){
    c[++ tot] = c[last];
    lson[tot] = lson[last];
    rson[tot] = rson[last];
    i = tot;
    c[i] += val;
    if(l == r) return ;
    int mid = (l + r) >> 1;
    if(pos <= mid) update(l,mid,lson[i],lson[last],pos,val);
    else update(mid + 1,r,rson[i],rson[last],pos,val);
}

int query(int l,int r,int lrt,int rrt,int k){
    if(l == r){
        if(X[l] <= k) return c[rrt] - c[lrt];
        return 0;
    }
    int ans = 0;
    int mid = (l + r) >> 1;
    if(X[mid] <= k)
        ans += c[lson[rrt]] - c[lson[lrt]] + query(mid + 1,r,rson[lrt],rson[rrt],k);
    else
        ans += query(l,mid,lson[lrt],lson[rrt],k);
    return ans;
}

int solve(int ll,int rr,int p,int kth){
    int l = 0,r = maxn * 10;
    int ans = r;
    while(l <= r){
        int mid = (l + r) >> 1;
        int lef = p - mid,rig = p + mid;
        if(query(1,en,T[ll - 1],T[rr],rig) - query(1,en,T[ll - 1],T[rr],lef - 1) >= kth){
            r = mid - 1;
            ans = min(ans,mid);
        }
        else l = mid + 1;
    }
    printf("%d\n",ans);
    return ans;
}

int main() {
    int t = read();
    while(t --){
        int n = read(),m = read();
        for(int i = 1;i <= n;++ i){
            K[i] = read();
            X[i] = K[i];
        }
        sort(X + 1,X + 1 + n);
        en = unique(X + 1,X + 1 + n) - X - 1;
        tot = 0;
        build(T[0],1,en);
        for(int i = 1;i <= n;++ i){
            int pos = lower_bound(X + 1,X + 1 + en,K[i]) - X;
            update(1,en,T[i],T[i - 1],pos,1);
        }
        int x = 0;
        while(m --){
            int l = read(),r = read(),p = read(),k = read();
            l ^= x; r ^= x; p ^= x; k ^= x;
            x = solve(l,r,p,k);
        }
    }
    return 0;
}

 

你可能感兴趣的:(#,主席树)