HDU - 6621 K-th Closest Distance 主席树+二分答案

Posted zhuyou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU - 6621 K-th Closest Distance 主席树+二分答案相关的知识,希望对你有一定的参考价值。

K-th Closest Distance

主席树第二波~

题意

给你\(n\)个数\(m\)个询问,问\(i\in [l,r]\)计算每一个\(|a_i-p|\)求出第\(k\)
题目要求强制在线\(l = l \oplus ans、r = r \oplus ans、p = p \oplus ans、k = k \oplus ans\)(ans为上次询问的答案)

思路

  • 二分答案\(ans\),找区间\(i\in[l,r], a_i \in [p-ans, p+ans]\)里的数量\(>= k\)
    \(|a_i - p | = ans\)
    \(a_i - p = ans、p - a_i = ans\)
    \(a_i = ans + p、a_i = p - ans\)
  • 用主席树维护一下就\(ok\)

    AC代码


#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define  lowbit(x)  x & (-x)
#define  mes(a, b)  memset(a, b, sizeof a)
#define  fi         first
#define  se         second
#define  pii        pair<int, int>

typedef unsigned long long int ull;
typedef long long int ll;
const int    maxn = 1e5 + 10;
const int    maxm = 1e5 + 10;
const ll     mod  = 1e9 + 7;
const ll     INF  = 1e18 + 100;
const int    inf  = 0x3f3f3f3f;
const double pi   = acos(-1.0);
const double eps  = 1e-8;
using namespace std;

int n, m;
int cas, tol, T;
struct Node
    int l, r, cnt;
node[maxn*40];
int w[maxn], a[maxn];
vector<int> v;

void init()
    v.clear();
    tol = 0;
    mes(w, 0);


void build(int l, int r, int rt)
    rt = ++tol;
    node[rt].cnt = 0;
    if(l == r)
        return;
    int mid = l+r>>1;
    build(l, mid, node[rt].l);
    build(mid+1, r, node[rt].r);


void update(int l, int r, int &x, int y, int pos)
    x = ++tol;
    node[x] = node[y];
    node[x].cnt++;
    if(l == r)
        return;
    int mid = l+r>>1;
    if(pos <= mid)
        update(l, mid, node[x].l, node[y].l, pos);
    else
        update(mid+1, r, node[x].r, node[y].r, pos);



int query(int L, int R, int l, int r, int x, int y)
    if(L <= l && r <= R)
        return node[y].cnt - node[x].cnt;
    int mid = l+r>>1, ans = 0;
    if(L <= mid)
        ans += query(L, R, l, mid, node[x].l, node[y].l);
    if(R > mid)
        ans += query(L, R, mid+1, r, node[x].r, node[y].r);
    return ans;


int getid(int x)
    return lower_bound(v.begin(), v.end(), x) - v.begin()+1;

int getid_(int x)
    return upper_bound(v.begin(), v.end(), x) - v.begin()+1;


int main() 
    scanf("%d", &T);
    while(T--)
        scanf("%d%d", &n, &m);
        init();
        for(int i = 1; i <= n; i++)
            scanf("%d", &a[i]);
            v.push_back(a[i]);
        
        sort(v.begin(), v.end());
        v.erase(unique(v.begin(), v.end()), v.end());
        int len = v.size()+1;
        build(1, len, 1);
        for(int i = 1; i <= n; i++)
            update(1, n, w[i], w[i-1], getid(a[i]));
        int ans = 0;
        while(m--)
            int L, R, p, k;
            scanf("%d%d%d%d", &L, &R, &p, &k);
            L ^= ans; R ^= ans; p^= ans;k ^= ans;   //要记得^
            int l = 0, r = 1e6;
            while(l <= r)
                int mid = (l+r)>>1;
                int sum = query(getid(p-mid), getid_(p+mid)-1, 1, n, w[L-1], w[R]);     //第一个getid和第二个不一样
                if(sum >= k)
                    ans = mid;
                    r = mid-1;
                
                else
                    l = mid+1;
            
            printf("%d\n", ans);
        
    
    return 0;


以上是关于HDU - 6621 K-th Closest Distance 主席树+二分答案的主要内容,如果未能解决你的问题,请参考以下文章

[hdu-6621]K-th Closest Distance 主席树 线段树 2019 多校4

2019杭电多校赛第四场 HDU6621 K-th Closest Distance 主席树 二分

2019杭电多校第四场hdu6621 K-th Closest Distance(二分答案+主席树)

HDU6621 K-th Closest Distance 第 k 小绝对值(主席树(统计范围的数有多少个)+ 二分 || 权值线段树+二分)

K-th Closest Distance

K-th Closest Distance