2019HDU多校第四场—— HDU 6621

K-th Closest Distance

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

题意:
m次询问,每次询问,问区间[l,r]之间|p-a[i]|第k小的数值是多少,然后得到的答案需要异或下一次所给的l,r,p,k得到真正的l,r,p ,k。

分析:这样的每一次异或就导致了,这题必须是强制在线的,但是一看这道题目竟然是15s的复杂度,那么可不可用线段树呐,每个节点存一个vector,然后排序,这样不就可以在查询的时候之间就找到了(下面的是错误代码QWQ)

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define rep(i,a,b) for(int i=a;i<=b;i++)
typedef long long ll;
using namespace std;
const int N=1e6+10;
const int INF=0x3f3f3f3f;
vector<int>ve[N<<2];
int a[N];
int n,m,prex;
void build(int id,int l,int r){
    ve[id].clear();
    for(int i=l;i<=r;i++)
    ve[id].push_back(a[i]);
    sort(ve[id].begin(),ve[id].end());
    if(l==r)
        return ;
    int mid=(l+r)/2;
    build(id<<1,l,mid);
    build(id<<1|1,mid+1,r);
}
int query(int id,int l,int r,int L,int R,int k,int p){
    if(L<=l && R>=r){
        return abs(ve[id][k-1]-p);
    }
    int mid=(l+r)/2;
    if(R<=mid)
        return query(id<<1,l,mid,L,R,k,p);
    else if(L>mid)
        return query(id<<1|1,mid+1,r,L,R,k,p);
    else{
        return min(query(id<<1,l,mid,L,mid,k,p),query(id<<1|1,mid+1,r,mid+1,R,k,p));
    }
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    int T;
    scanf("%d",&T);
    while(T--){prex=0;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
            build(1,1,n);
        for(int i=1;i<=m;i++){int l,r,p,k;
           scanf("%d%d%d%d",&l,&r,&p,&k);
            l^=prex;r^=prex;p^=prex;k^=prex;
            printf("%d\n",prex=query(1,1,n,l,r,k,p));
        }
    }
    return 0;
}

交上我就后悔了,思路不对啊,这样找的是个啥啊,但是…………TLE了,那怎么做呢,既然是有序的,可不可以二分呢;怎么二分,分谁呢???

分析题目中给出的式子: |p-a[i]|
是找所给的区间第K小的数是多少,那么不就可以查询|p-a[i]|在[l,r]的区间里面a[i]的个数有个不就好了,什么意思呢?就是每次我去二分|p-a[i]| 的绝对值,我们进一步化简|p-a[i]|=mid这个式子,就会得到
p-mid=a[i]=p+mid,那么就用线段树查询在数值[p-mid,p+mid]之间有多少个数不就可以二分了吗(线段树的操作实在是……tsl)

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define rep(i,a,b) for(int i=a;i<=b;i++)
typedef long long ll;
using namespace std;
const int N=1e6+10;
const int INF=0x3f3f3f3f;
vector<int>ve[N<<2];
vector<int>::iterator it;
int a[N];
int n,m,prex;
void build(int id,int l,int r){
    ve[id].clear();
    for(int i=l;i<=r;i++)
    ve[id].push_back(a[i]);
    sort(ve[id].begin(),ve[id].end());
    if(l==r)
        return ;
    int mid=(l+r)/2;
    build(id<<1,l,mid);
    build(id<<1|1,mid+1,r);
}
int query(int id,int l,int r,int L,int R,int p){
    if(L==l && R==r){
    return upper_bound(ve[id].begin(),ve[id].end(),p)-ve[id].begin();
    }
    int mid=(l+r)/2;

    if(R<=mid)
        return query(id<<1,l,mid,L,R,p);
    else if(L>mid)
        return query(id<<1|1,mid+1,r,L,R,p);
    else
        return query(id<<1,l,mid,L,mid,p)+query(id<<1|1,mid+1,r,mid+1,R,p);
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    int T;
    scanf("%d",&T);
    while(T--){prex=0;
        scanf("%d%d",&n,&m);
        int ub=-1,lb=0;
        for(int i=1;i<=n;i++)
           scanf("%d",&a[i]),ub=max(ub,a[i]);
           build(1,1,n);
        int rrrrr=ub;
        for(int i=1;i<=m;i++){int l,r,p,k;
           scanf("%d%d%d%d",&l,&r,&p,&k);
            l^=prex;r^=prex;p^=prex;k^=prex;
            ub=rrrrr;lb=0;
            while(ub>=lb){
                int mid=(lb+ub)/2;
                int cnt=query(1,1,n,l,r,p+mid)-query(1,1,n,l,r,p-mid-1);
                if(cnt<k)
                lb=mid+1;
                else
                ub=mid-1;
            }
            prex=lb;
            printf("%d\n",lb);
        }
    }
    return 0;
}


在这里插入图片描述
接近九秒,QWQ,还有说用主席树来敲的,同样也是二分来实现的,自己也照着来了一发,也可以当模板了

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define rep(i,a,b) for(int i=a;i<=b;i++)
typedef long long ll;
using namespace std;
const int N=1e6+10;
const int INF=0x3f3f3f3f;
int a[N],root[N];
int n,m,prex,cnt;
struct node{
    int sum,l,r;
}tr[N*30];
void update(int l,int r,int &now,int last,int pos){
    tr[++cnt]=tr[last];
    now=cnt;
    tr[now].sum++;
    if(l==r) return ;
    int mid=(l+r)/2;
    if(pos<=mid)
        update(l,mid,tr[now].l,tr[last].l,pos);
    else
        update(mid+1,r,tr[now].r,tr[last].r,pos);
}
int query(int L,int R,int l,int r,int x,int y){
    if(L<=l&& r<=R)
        return tr[y].sum-tr[x].sum;
    int mid=(l+r)/2;
//    int ans=0;
//    if(L<=mid) ans+=query(L,R,l,mid,tr[x].l,tr[y].l);
//    if(R>mid) ans+=query(L,R,mid+1,r,tr[x].r,tr[y].r);
//    return ans;
    if(R<=mid) return query(L,R,l,mid,tr[x].l,tr[y].l);
    else if(L>mid) return query(L,R,mid+1,r,tr[x].r,tr[y].r);
    else return query(L,mid,l,mid,tr[x].l,tr[y].l)+query(mid+1,R,mid+1,r,tr[x].r,tr[y].r);
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    int T;
    scanf("%d",&T);
    while(T--){prex=0;cnt=0;
        scanf("%d%d",&n,&m);
        memset(tr,0,sizeof tr);
        memset(root,0,sizeof root);
        int ub=-1,lb=0;
        for(int i=1;i<=n;i++)
           scanf("%d",&a[i]),ub=max(ub,a[i]);

        for(int i=1;i<=n;i++)
            update(1,ub,root[i],root[i-1],a[i]);

        int maxv=ub;
        for(int i=1;i<=m;i++){int l,r,p,k;
           scanf("%d%d%d%d",&l,&r,&p,&k);
            l^=prex;r^=prex;p^=prex;k^=prex;
            ub=1e6;lb=0;
            while(ub>=lb){
                int mid=(lb+ub)/2;
                int cnt=query(max(1,p-mid),min(p+mid,maxv),1,maxv,root[l-1],root[r]);
                if(cnt<k)
                lb=mid+1;
                else
                ub=mid-1;
            }
            prex=lb;
            printf("%d\n",lb);
        }
    }
    return 0;
}

在这里插入图片描述
2019HDU多校第四场—— HDU 6621_第1张图片

在这里插入图片描述
2019HDU多校第四场—— HDU 6621_第2张图片
两种查询,发现第一种查询会快一点;

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