标签:ring 查询 tree hdu int amp cstring 间隔 ref
对于每个查询,都会给您一个间隔[L,R]和两个数字p和K。您的目标是找到p和aL,aL+1,…,aR之间的第K个最近距离。
p和ai之间的距离等于| p-ai |。
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<map> #include<vector> #include<queue> #include<cmath> #define ll long long using namespace std; const int N=1e5+10; const int M=1e6; struct node { int l, r; int val; }tree[N*55]; int root[N], tot; int n,q,a[N]; int update(int pre, int pl, int pr, int val) { int cur = ++tot; tree[cur] = tree[pre]; tree[cur].val ++; if(pl == pr) return cur; int mid = (pl + pr) >> 1; if(val <= mid) tree[cur].l = update(tree[pre].l, pl, mid, val); else tree[cur].r = update(tree[pre].r, mid + 1, pr, val); return cur; } int k, cnt; int query(int pl, int pr, int l, int r, int rt, int lt) { if(pl <= l && r <= pr) { return tree[rt].val - tree[lt].val; } int mid = (l + r) >> 1; int res = 0; if(pl <= mid) res += query(pl, pr, l, mid, tree[rt].l, tree[lt].l); if(pr > mid) res += query(pl, pr, mid + 1, r, tree[rt].r, tree[lt].r); return res; } int main() { int T, ans; int l, r, p; int pl, pr, mid; scanf("%d", &T); while(T--) { scanf("%d%d", &n, &q); for(int i = 1; i <= n; i++) { scanf("%d", &a[i]); root[i] = update(root[i - 1], 1, M, a[i]); } ans = 0; while(q--) { scanf("%d%d%d%d", &l, &r, &p, &k); l = l ^ ans; r = r ^ ans; p = p ^ ans; k = k ^ ans; pl = 0, pr = M; while(pl<=pr) { mid = (pl + pr) >> 1; if(query(max(1, p - mid), min(M, p + mid), 1, M, root[r], root[l - 1]) >= k) { ans = mid; pr = mid - 1; } else pl = mid + 1; } printf("%d\n", ans); } } return 0; }
标签:ring 查询 tree hdu int amp cstring 间隔 ref
原文地址:https://www.cnblogs.com/2462478392Lee/p/11931271.html