HDU Multi-University Training Contest 4 HDU 6621 K-th Closest Distance

Hang electric multi-school Game 4 of 08 questions.

Chairman of the binary tree +

See the topic should be with the President of trees. At that time only to see the maximum 169 k, p rankings to think first find in the interval, and then look for violence, to write for a long time, the code is a bit difficult to adjust, to pay up is RE, should be WA, resulting in different or wrong that step , which led to RE.

Interpretations Chairman tree plus two points, two points answer mid, and then the President tree query interval LR, range number in the [p-mid, p + mid] in a point, to explain our mid If the number is greater than the k, large selection, then it would reduce mid, or increased mid, two points can be.

#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
const int M = 1e6 + 10;
//const int mx = 1e6;
struct tree{
    int cnt, lc, rc;
}T[(M << 6)];
int root[N], a[N], b[N];
int n, m, q, tot;
void build(int &rt, int l, int r)//建立一颗空树
{
    rt = ++tot;
    T[rt].cnt = 0;
    T[rt].lc = T[rt].rc = 0;
    if (l == r) return;
    int mid = (l + r) >> 1;
    build(T[rt].lc, l, mid);
    build(T[rt].rc, mid + 1, r);
}
void insert(int l, int r, int &cur, int pre, int pos)
{
    T[++tot] = T[pre];
    cur = tot;
    T[cur].cnt++;
    if (l == r) return;
    int mid = (l + r) >> 1;
    if (pos <= mid) insert(l, mid, T[cur].lc, T[pre].lc, pos);
    else insert(mid + 1, r, T[cur].rc, T[pre].rc, pos);
}
int query(int l, int r, int x, int y, int k)
{
    if (r <= k) return T[y].cnt - T[x].cnt;
    if (l == r){
        return T[y].cnt - T[x].cnt;
    }
    int s = T[T[y].lc].cnt - T[T[x].lc].cnt;
    int mid = (l + r) >> 1;
    if (k <= mid){
        return query(l, mid, T[x].lc, T[y].lc, k);
    }
    else{
        return s + query(mid + 1, r, T[x].rc, T[y].rc, k);
    }
}
int max(int a, int b)
{
    return a > b ? a : b;
}
int main()
{
    int T;
    scanf("%d", &T);
    for (int t = 1; t <= T; t++)
    {
        int mx = 0;
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; i++)
            scanf("%d", &a[i]),mx=max(mx,a[i]);
        tot = 0;
        build(root[0], 1, mx);
        for (int i = 1; i <= n; i++)
            insert(1, mx, root[i], root[i - 1], a[i]);
        int lst=0;
        while (m--)
        {
            int l, r, p, k;
            scanf("%d%d%d%d", &l, &r, &p, &k);
            l ^= lst, r ^= lst, k ^= lst, p ^= lst;
            int L=0, R = 1e6;
            while (L < R)
            {
                int mid = (L + R) >> 1;
                int cnt = query(1, mx, root[l - 1], root[r], p + mid);
                if (p - mid - 1>0) cnt -= query(1, mx, root[l - 1], root[r], p - mid - 1);
                if (cnt >= k) R = mid;
                else L = mid + 1;
            }
            printf("%d\n", lst = L);
        }
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/xiaoguapi/p/11278478.html