Advertisement
Guest User

Untitled

a guest
Dec 15th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef pair< int,int > ii;
  4. typedef pair<double,ii> dii;
  5.  
  6.  
  7. int N,C,K,asd;
  8. double D;
  9. set<dii> closes;
  10.  
  11. priority_queue<dii> pq;
  12.  
  13. bool cmpX(const ii &a, const ii &b) {
  14. return a.first < b.first;
  15. }
  16.  
  17. bool cmpY(const ii &a, const ii &b) {
  18. return a.second < b.second;
  19. }
  20.  
  21. struct node{
  22.  
  23. int point[2];
  24. node * left , * right;
  25.  
  26. node(){}
  27. node(int * arr, node * l, node * r){
  28. memcpy(point,arr,sizeof point);
  29. left = l; right = r;
  30. }
  31.  
  32. node(int x, int y, node * l, node * r){
  33. point[0] = x; point[1] = y;
  34. left = l; right = r;
  35. }
  36.  
  37. double distNode(int * p){
  38. return hypot(point[0] - p[0],point[1] - p[1]);
  39. }
  40. };
  41.  
  42. // Cria um novo nรณ
  43. node * newNode(int * arr)
  44. {
  45. node * temp = new node(arr,0,0);
  46. return temp;
  47. }
  48.  
  49. node * build(vector<ii> & pontos, int depth, int l, int r)
  50. {
  51. if(l > r) return 0;
  52. if(l == r) return new node(pontos[l].first,pontos[l].second,0,0);
  53.  
  54. node * root = 0;
  55.  
  56. int nivel = depth % 2;
  57. asd = max(asd,depth);
  58.  
  59. int mid = (l+r)/2;
  60.  
  61. if(nivel == 0) sort(pontos.begin() + l , pontos.begin()+ r + 1,cmpX);
  62. else sort(pontos.begin() + l , pontos.begin() + r + 1 , cmpY);
  63.  
  64. node * left = build(pontos , depth+1 , l , mid-1);
  65. node * right = build(pontos , depth+1 , mid+1 , r);
  66.  
  67. return root = new node(pontos[mid].first,pontos[mid].second,left,right);
  68. }
  69.  
  70. node * NNS2(node * root, node * best, int * point, int depth)
  71. {
  72. cout<<"ASDA"<<endl;
  73. if(!root) return best;
  74. if(!best) best = root;
  75.  
  76. if(root->distNode(point) < best->distNode(point))
  77. best = root;
  78.  
  79. int dms = depth % 2;
  80.  
  81. if(point[dms] < (root->point[dms]))
  82. best = NNS2(root->left,best,point,depth+1);
  83. best = NNS2(root->right,best,point,depth+1);
  84.  
  85.  
  86. return best;
  87. }
  88.  
  89. bool find(node * root, int * point, int depth)
  90. {
  91. if(!root)
  92. return 0;
  93. if(!memcmp(point,root->point,sizeof *point))
  94. return 1;
  95.  
  96. int cd = depth % 2;
  97.  
  98. if(point[cd] < (root->point[cd]))
  99. return find(root->left,point,depth+1);
  100. return find(root->right,point,depth+1);
  101. }
  102.  
  103. int main()
  104. {
  105. freopen("in","r",stdin);
  106.  
  107. int p[2];
  108. vector<ii> pontos;
  109. while(scanf("%d%d%d%lf",&N,&C,&K,&D) == 4)
  110. {
  111. int resp = 0;
  112. node * root = 0;
  113. pontos.clear();
  114.  
  115. while(N--)
  116. {
  117. scanf("%d%d",&p[0],&p[1]);
  118. pontos.push_back(ii(p[0],p[1]));
  119. }
  120.  
  121. // construcao da kd tree
  122. // construcao da kd tree
  123. root = build(pontos,0,0,pontos.size()-1);
  124.  
  125. while(C--)
  126. {
  127. scanf("%d%d",&p[0],&p[1]);
  128. cout<<">>>>>>>>>>>>>>>>>>>>>"<<p[0]<<' '<<p[1]<<endl;
  129. node * best = NNS2(root,0,p,0);
  130. cout<<" >> "<<p[0]<<' '<<p[1]<< " >> "<<best->point[0]<<" "<<best->point[1]<< ' '<<best->distNode(p)<<endl;
  131.  
  132. }
  133. printf("%d\n",resp);
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement