显示代码纯文本
//http://blog.sina.cn/dpool/blog/s/blog_6f611c300101bysf.html
#include <cstdio>
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef long double LB;
void read(LL &x) {static char c; static bool flag;for (flag = 0; (c=getchar())<'0'||c>'9'; flag |= c=='-');for (x=c-'0'; (c=getchar())>='0'&&c<='9'; x=x*10+c-'0');if(flag) x = -x;}
inline LL F(LL x) {return x*x*x*x;}
#define N 200000
LL D;
struct Data {
LL t,v[5];
int flag;
void in() {
read(t);
for (int i = 0; i < t; i++) read(v[i]);
for (int i = t; i < 5; i++) v[i] = 0;
}
bool operator < (const Data &lhs) const {
return v[D] < lhs.v[D];
}
}tt[N],t[N],q;
priority_queue<LB> ans;
LL C,n,Q;
LB Dis(const Data &a,const Data &b) {
LB t = 0;
for (int i = 0; i < 5; i++)
t += F(a.v[i]-b.v[i]);
return t;
}
void Push(LB v) {
ans.push(v);
if(ans.size()>C) ans.pop();
}
void Out() {
static vector<LB> t;
t.clear();
while(ans.size()) {
t.push_back(ans.top()); ans.pop();
}
for (int i = t.size()-1; i >= 0; i--)
printf("%lld\n",(LL)(t[i]+0.5));
}
///////////
#define L (o<<1)
#define R (o<<1|1)
int rt;
void build(int l,int r,int o,int cas) {
if(l>r) return; D = cas;
t[o].flag = 1; t[L].flag = t[R].flag = -1;
int mid = (l+r)>>1;
nth_element(tt+l,tt+mid,tt+r+1);
t[o] = tt[mid];
cas = (cas+1)%5;
build(l,mid-1,L,cas); build(mid+1,r,R,cas);
}
void query(int o,int cas) {
if(t[o].flag == -1) return;
int x = L,y = R;
if(q.v[cas] > t[o].v[cas]) swap(x,y);
query(x,(cas+1)%5);
if(ans.size() < C) {
Push(Dis(t[o],q));
query(y,(cas+1)%5);
} else {
Push(Dis(t[o],q));
if(F(t[o].v[cas]-q.v[cas]) <= ans.top()) query(y,(cas+1)%5);
}
}
int main() {
freopen("Keller_Deal.in","r",stdin);freopen("Keller_Deal.out","w",stdout);
read(n); read(Q);
for (int i = 1; i <= n; i++) tt[i].in();
build(1,n,1,0);
for (int i = 1; i <= Q; i++) {
read(q.t); read(C);
for (int i = 0; i < q.t; i++) read(q.v[i]);
for (int i = q.t; i < 5; i++) q.v[i] = 0;
printf("the closest %lld strings' distance are:\n",C);
query(1,0);
Out();
}
return 0;
}