【牛客网】Finding Hotel

【牛客网】Finding Hotel

忘记K远点对的剪枝的我有点自闭

事实上我们只要先建一棵KD树出来,维护一下所在的矩形,和子树里的最小值

每次查询的时候如果最小值比查询的值要大的话就退出

当前的答案构成了一个圆,若圆和矩形没有交就退出(不一定很严格,可以认为是以圆心为中心向上下左右延伸半径长度的一个正方形和矩形有交)

然后看当前点在哪个子树的矩形里,先搜那个子树,如果回来后在另一个子树里可能达到的最小值都没有答案大就不搜索另一棵子树

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define ba 47
#define MAXN 200005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
    void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < '0' || c > '9') {
        if(c == '-') f = -1;
        c = getchar();
    }
    while(c >= '0' && c <= '9') {
        res = res * 10 +c - '0';
        c = getchar();
    }
    res *= f;
}
template<class T>
    void out(T x) {
    if(x < 0) {x = -x;putchar('-');}
    if(x >= 10) {
        out(x / 10);
    }
    putchar('0' + x % 10);
}
int N,dimension,M,rt,Ncnt;
struct node {
    int d[2],c,id;
}p[MAXN];
bool cmp(node a,node b) {
    return a.d[dimension] < b.d[dimension];
}
struct KD {
    node p;
    int r[4],lc,rc,mc;
}tr[MAXN];
#define lc(u) tr[u].lc
#define rc(u) tr[u].rc

void build(int &u,int l,int r,int d) {
    u = 0;
    if(l > r) return;
    u = ++Ncnt;
    dimension = d;
    int mid = (l + r) >> 1;
    nth_element(p + l,p + mid,p + r + 1,cmp);
    tr[u].p = p[mid];
    for(int i = 0 ; i < 4 ; ++i) tr[u].r[i] = tr[u].p.d[i & 1];
    build(lc(u),l,mid - 1,d ^ 1);build(rc(u),mid + 1,r,d ^ 1);
    tr[u].mc = min(p[mid].c,min(tr[lc(u)].mc,tr[rc(u)].mc));
    for(int i = 0 ; i < 2 ; ++i) tr[u].r[i] = min(tr[u].r[i],min(tr[lc(u)].r[i],tr[rc(u)].r[i]));
    for(int i = 2 ; i < 4 ; ++i) tr[u].r[i] = max(tr[u].r[i],max(tr[lc(u)].r[i],tr[rc(u)].r[i]));
}
node ans;
int64 o(int64 x) {return x * x;}
int64 dis(node a,node b) {
    return o(a.d[0] - b.d[0]) + o(a.d[1] - b.d[1]);
}
bool checkin(node x,int u) {
    for(int i = 0 ; i <= 1 ; ++i) {
        if(x.d[i] < tr[u].r[i] || x.d[i] > tr[u].r[i | 2]) return false;
    }
    return true;
}
int64 min_possible(node x,int u) {
    for(int i = 0 ; i <= 1 ; ++i) {
        if(x.d[i] < tr[u].r[i] || x.d[i] > tr[u].r[i | 2]) {
            return min(o(tr[u].r[i] - x.d[i]),o(tr[u].r[i | 2] - x.d[i]));
        }
    }
    return 1e18;
}
void Query(int u,node pos) {
    if(!u) return;
    if(tr[u].mc > pos.c) return;
    if(tr[u].p.c <= pos.c) {
        if(dis(tr[u].p,pos) < dis(ans,pos) || (dis(tr[u].p,pos) == dis(ans,pos) && tr[u].p.id < ans.id)) ans = tr[u].p;
    }
    int64 d = dis(ans,pos);
    for(int i = 0 ; i <= 1 ; ++i) {
        if(pos.d[i] < tr[u].r[i] && 1LL * (tr[u].r[i] - pos.d[i]) * (tr[u].r[i] - pos.d[i]) > d) return;
        if(pos.d[i] > tr[u].r[i | 2] && 1LL * (tr[u].r[i | 2] - pos.d[i]) * (tr[u].r[i | 2] - pos.d[i]) > d) return;
    }
    int s = checkin(pos,lc(u)) ? lc(u) : rc(u);
    int t = s == lc(u) ? rc(u) : lc(u);
    Query(s,pos);
    if(min_possible(pos,t) <= dis(pos,ans)) Query(t,pos);
}
void Solve() {
    rt = 0;Ncnt = 0;
    read(N);read(M);
    for(int i = 1 ; i <= N ; ++i) {
        read(p[i].d[0]);read(p[i].d[1]);read(p[i].c);
        p[i].id = i;
    }
    tr[0].mc = 1e9;
    tr[0].r[0] = tr[0].r[1] = 1e9;
    tr[0].r[2] = tr[0].r[3] = -1;
    build(rt,1,N,0);
    node pos;
    for(int i = 1 ; i <= M ; ++i) {
        read(pos.d[0]);read(pos.d[1]);read(pos.c);
        ans.d[0] = 1e9;ans.d[1] = 1e9;ans.c = 0;ans.id = 1e9;
        Query(1,pos);
        out(ans.d[0]);space;out(ans.d[1]);space;out(ans.c);enter;
    }
}
int main(){
    #ifdef ivorysi
    freopen("f1.in","r",stdin);
    #endif
    int T;
    read(T);
    while(T--) Solve();
    return 0;
}

转载于:https://www.cnblogs.com/ivorysi/p/11101878.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. Are there any memory errors in the following programs? If so, list all of them. Assume that the user enters in correct input, and that the sizes entered are at least one. Write your solution in a text or Word file and submit it below. void main() { char *str, *input; int *ilist; int i, size1, size2; printf("Number of letters in word: "); scanf("%d", &size1;); /* user inputs an integer */ printf("Number of integers: "); scanf("%d", &size2;); /* user inputs an integer */ str = (char *) malloc(size1); ilist = (int *) malloc(size2); printf("Word: "); scanf("%s", str); /* user inputs a string */ for(i = 0; i < size2; i++) { printf("Number %d of %d: ", i + 1, size2); scanf("%d", ilist + i); /* user inputs an integer */ } } 2. Are there any memory errors in the following program? If so, list all of them. Write your solution in a text or Word file and submit it below. /* return 1 if str is "1", 0 otherwise */ int checkIf1(char *str) { char *newstr = malloc(strlen(str) + 1); strcpy(newstr, str); /* set newstr to str */ if (strcmp(newstr, "1") == 0) { /* newstr is "1" */ return 1; } free(newstr); return 0; } void main() { char *strArr[4] = {"1", "2", "3", "4"}; int i; for(i = 0; i < 4; i++) { printf("%d\n", checkIf1(strArr[i])); } } 3. Are there any memory errors in the following program? If so, list all of them. Write your solution in a text or Word file and submit it below. struct data { char *str1, *str2; }; /* returns two strings concatenated if they are not the same, NULL otherwise */ char *mergeSingleIfDifferent(char *s1, char *s2) { char *str = (char *) malloc(strlen(s1) + strlen(s2) + 1); if (strcmp(s1, s2) == 0) { /* strings are equal */ str = NULL; } else { strcpy(str, s1); strcat(str, s2); } return str; } /* copies merged strings (or NULL) into array of strings passed in (results) */ void mergeArrayIfDifferent(char *results[], char *strA1[], char *strA2[], int size) { int i; for(i = 0; i < size; i++) { results[i] = mergeSingleIfDifferent(strA1[i], strA2[i]); } } void printAndFree(int c, char *str) { if (str != NULL) { printf("%d: %s\n", c, str); free(str); } } void main() { char *strArr1[8] = {"1", "2", "3", "4", "5", "6", "7", "8"}; char *strArr2[8] = {"a", "2", "c", "4", "e", "6", "g", "8"}; char *results[8]; int i; mergeArrayIfDifferent(results, strArr1, strArr2, 8); for(i = 0; i < 8; i++) { printAndFree(i, results); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值