BoxerTC

SRM 199 DIV1 Problem 1000

Oct 26th, 2014
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.62 KB | None | 0 0
  1. #include <vector>
  2. #include <list>
  3. #include <map>
  4. #include <set>
  5. #include <queue>
  6. #include <deque>
  7. #include <stack>
  8. #include <bitset>
  9. #include <algorithm>
  10. #include <functional>
  11. #include <numeric>
  12. #include <utility>
  13. #include <sstream>
  14. #include <iostream>
  15. #include <iomanip>
  16. #include <cstdio>
  17. #include <cmath>
  18. #include <cstdlib>
  19. #include <ctime>
  20. #include <cstdio>
  21. #include <climits>
  22. #include <cstring>
  23. using namespace std;
  24.  
  25. #define REP( i , n ) for( int i = 0 ; i < n ; ++i )
  26. #define clr( t , val ) memset( t , val , sizeof( t ) )
  27.  
  28. #define pb push_back
  29. #define all( v ) v.begin() , v.end()
  30. #define SZ( v ) ((int)v.size())
  31.  
  32. #define mp make_pair
  33. #define fi first
  34. #define se second
  35.  
  36. typedef long long ll;
  37. typedef long double ld;
  38. typedef vector< int > vi;
  39. typedef pair< int , int > pii;
  40.  
  41. typedef struct node_t{
  42.     int p;
  43.     struct node_t *lc , *rc;
  44. }node;
  45. struct point{
  46.     ll x , y , z;
  47.     bool operator < ( const point &rhs ) const {
  48.         if( x < rhs.x ) return 1;
  49.         if( x > rhs.x ) return 0;
  50.         if( y < rhs.y ) return 1;
  51.         if( y > rhs.y ) return 0;
  52.         return z < rhs.z;
  53.     }
  54.     bool operator == ( const point &rhs ) const{
  55.         return x == rhs.x && y == rhs.y && z == rhs.z;
  56.     }
  57. };
  58. #define sqr(A) ((A)*(A))
  59.  
  60. node freeList[150100];
  61. int p[ 150100 ];
  62. point points[ 150100 ];
  63.  
  64. int __cmp0_coord = 0;
  65. inline bool cmp0( int a , int b ){
  66.     if( __cmp0_coord == 0 ) return points[ a ].x < points[ b ].x;
  67.     if( __cmp0_coord == 1 ) return points[ a ].y < points[ b ].y;
  68.     return points[ a ].z < points[ b ].z;
  69. }
  70. int freeCount = 0;
  71. inline node *getNode( int p ){
  72.     freeList[ freeCount ].p = p;
  73.     freeList[ freeCount ].lc = freeList[ freeCount ].rc = NULL;
  74.     return &freeList[ freeCount ++ ];
  75. }
  76.  
  77. inline node *build( int from , int to , int cd ){
  78.     if( to - from == 0 ) return NULL;
  79.     if( to - from == 1 ) return getNode( p[ from ] );
  80.     int mid = to + from >> 1;
  81.     __cmp0_coord = cd;
  82.     nth_element( p + from , p + mid , p + to , cmp0 );
  83.     node *r = getNode( p[ mid ] );
  84.     r -> lc = build( from , mid , (cd + 1)%3 );
  85.     r -> rc = build( mid + 1 , to , (cd + 1)%3 );
  86.     return r;
  87. }
  88.  
  89. ll dist , cnt;
  90. point test;
  91. inline void query( node *t , int cd ){
  92.     ll mydist = sqr( points[ t -> p ].x - test.x ) + sqr( points[ t -> p ].y - test.y ) + sqr( points[ t -> p ].z - test.z );
  93.     if( mydist == 0 ) mydist = 1LL << 60;
  94.     if( mydist < dist ){
  95.         cnt = 1;
  96.         dist = mydist;
  97.     }else if( mydist == dist ){
  98.         cnt ++;
  99.     }
  100.     ll cp = (cd == 0) ? points[ t -> p ].x : ( cd == 1 ) ? points[ t -> p ].y : points[ t -> p ].z;
  101.     ll ca = (cd == 0) ? test.x : (cd == 1 ) ? test.y : test.z;
  102.     if( t -> lc != NULL )if( ca <= cp || (ca >= cp && sqr( cp - ca ) <= dist ) ){
  103.         query( t -> lc , (cd + 1)%3 );
  104.     }
  105.     if( t -> rc != NULL )if( ca >= cp || (ca <= cp && sqr( cp - ca ) <= dist ) ){
  106.         query( t -> rc , (cd + 1)%3 );
  107.     }
  108. }
  109.  
  110. class ClosestPoints {
  111. public:
  112.     ll seed , range;
  113.     ll getNext(){
  114.         seed = ( seed * 16807LL ) % ((1LL<<31) - 1LL);
  115.         ll rand = (seed % (2LL * range)) - range;
  116.         return rand;
  117.     }
  118.     vector <int> distance(int N, int range, int seed) {
  119.         this -> seed = seed;
  120.         this -> range = range;
  121.         for( int i = 0 ; i < N ; ++i ){
  122.             points[ i ].x = getNext();
  123.             points[ i ].y = getNext();
  124.             points[ i ].z = getNext();
  125.         }
  126.         sort( points , points + N );
  127.         N = unique( points , points + N ) - points;
  128.         for( int i = 0 ; i < N ; ++i ) p[ i ] = i;
  129.        
  130.         node *root = build( 0 , N , 0 );
  131.        
  132.         dist = 1LL << 60;
  133.         for( int i = 0 ; i < N ; ++i ){
  134.             test.x = points[ i ].x;
  135.             test.y = points[ i ].y;
  136.             test.z = points[ i ].z;
  137.             query( root , 0 );
  138.         }
  139.         vi r( 2 , 0 );
  140.         r[ 0 ] = dist;
  141.         r[ 1 ] = cnt / 2;
  142.         return r;
  143.     }
  144. };
  145.  
  146.  
  147. // BEGIN KAWIGIEDIT TESTING
  148. // Generated by KawigiEdit 2.1.4 (beta) modified by pivanof
  149. bool KawigiEdit_RunTest(int testNum, int p0, int p1, int p2, bool hasAnswer, vector <int> p3) {
  150.     cout << "Test " << testNum << ": [" << p0 << "," << p1 << "," << p2;
  151.     cout << "]" << endl;
  152.     ClosestPoints *obj;
  153.     vector <int> answer;
  154.     obj = new ClosestPoints();
  155.     clock_t startTime = clock();
  156.     answer = obj->distance(p0, p1, p2);
  157.     clock_t endTime = clock();
  158.     delete obj;
  159.     bool res;
  160.     res = true;
  161.     cout << "Time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl;
  162.     if (hasAnswer) {
  163.         cout << "Desired answer:" << endl;
  164.         cout << "\t" << "{";
  165.         for (int i = 0; int(p3.size()) > i; ++i) {
  166.             if (i > 0) {
  167.                 cout << ",";
  168.             }
  169.             cout << p3[i];
  170.         }
  171.         cout << "}" << endl;
  172.     }
  173.     cout << "Your answer:" << endl;
  174.     cout << "\t" << "{";
  175.     for (int i = 0; int(answer.size()) > i; ++i) {
  176.         if (i > 0) {
  177.             cout << ",";
  178.         }
  179.         cout << answer[i];
  180.     }
  181.     cout << "}" << endl;
  182.     if (hasAnswer) {
  183.         if (answer.size() != p3.size()) {
  184.             res = false;
  185.         } else {
  186.             for (int i = 0; int(answer.size()) > i; ++i) {
  187.                 if (answer[i] != p3[i]) {
  188.                     res = false;
  189.                 }
  190.             }
  191.         }
  192.     }
  193.     if (!res) {
  194.         cout << "DOESN'T MATCH!!!!" << endl;
  195.     } else if (double(endTime - startTime) / CLOCKS_PER_SEC >= 2) {
  196.         cout << "FAIL the timeout" << endl;
  197.         res = false;
  198.     } else if (hasAnswer) {
  199.         cout << "Match :-)" << endl;
  200.     } else {
  201.         cout << "OK, but is it right?" << endl;
  202.     }
  203.     cout << "" << endl;
  204.     return res;
  205. }
  206. int main() {
  207.     bool all_right;
  208.     all_right = true;
  209.    
  210.     int p0;
  211.     int p1;
  212.     int p2;
  213.     vector <int> p3;
  214.    
  215.     {
  216.     // ----- test 0 -----
  217.     p0 = 3;
  218.     p1 = 100;
  219.     p2 = 1;
  220.     int t3[] = {9163,1};
  221.             p3.assign(t3, t3 + sizeof(t3) / sizeof(t3[0]));
  222.     all_right = KawigiEdit_RunTest(0, p0, p1, p2, true, p3) && all_right;
  223.     // ------------------
  224.     }
  225.    
  226.     {
  227.     // ----- test 1 -----
  228.     p0 = 10000;
  229.     p1 = 1;
  230.     p2 = 7;
  231.     int t3[] = {1,12};
  232.             p3.assign(t3, t3 + sizeof(t3) / sizeof(t3[0]));
  233.     all_right = KawigiEdit_RunTest(1, p0, p1, p2, true, p3) && all_right;
  234.     // ------------------
  235.     }
  236.    
  237.     {
  238.     // ----- test 2 -----
  239.     p0 = 25;
  240.     p1 = 1;
  241.     p2 = 12;
  242.     int t3[] = {1,9};
  243.             p3.assign(t3, t3 + sizeof(t3) / sizeof(t3[0]));
  244.     all_right = KawigiEdit_RunTest(2, p0, p1, p2, true, p3) && all_right;
  245.     // ------------------
  246.     }
  247.    
  248.     {
  249.     // ----- test 3 -----
  250.     p0 = 15;
  251.     p1 = 5;
  252.     p2 = 504;
  253.     int t3[] = {5,2};
  254.             p3.assign(t3, t3 + sizeof(t3) / sizeof(t3[0]));
  255.     all_right = KawigiEdit_RunTest(3, p0, p1, p2, true, p3) && all_right;
  256.     // ------------------
  257.     }
  258.    
  259.     {
  260.     // ----- test 4 -----
  261.     p0 = 50000;
  262.     p1 = 1000000;
  263.     p2 = 75;
  264.     int t3[] = {1252249,1};
  265.             p3.assign(t3, t3 + sizeof(t3) / sizeof(t3[0]));
  266.     all_right = KawigiEdit_RunTest(4, p0, p1, p2, true, p3) && all_right;
  267.     // ------------------
  268.     }
  269.    
  270.     if (all_right) {
  271.         cout << "You're a stud (at least on the example cases)!" << endl;
  272.     } else {
  273.         cout << "Some of the test cases had errors." << endl;
  274.     }
  275.     return 0;
  276. }
  277. // END KAWIGIEDIT TESTING
  278. //Powered by KawigiEdit 2.1.4 (beta) modified by pivanof!
Advertisement
Add Comment
Please, Sign In to add comment