Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <list>
- #include <map>
- #include <set>
- #include <queue>
- #include <deque>
- #include <stack>
- #include <bitset>
- #include <algorithm>
- #include <functional>
- #include <numeric>
- #include <utility>
- #include <sstream>
- #include <iostream>
- #include <iomanip>
- #include <cstdio>
- #include <cmath>
- #include <cstdlib>
- #include <ctime>
- #include <cstdio>
- #include <climits>
- #include <cstring>
- using namespace std;
- #define REP( i , n ) for( int i = 0 ; i < n ; ++i )
- #define clr( t , val ) memset( t , val , sizeof( t ) )
- #define pb push_back
- #define all( v ) v.begin() , v.end()
- #define SZ( v ) ((int)v.size())
- #define mp make_pair
- #define fi first
- #define se second
- typedef long long ll;
- typedef long double ld;
- typedef vector< int > vi;
- typedef pair< int , int > pii;
- typedef struct node_t{
- int p;
- struct node_t *lc , *rc;
- }node;
- struct point{
- ll x , y , z;
- bool operator < ( const point &rhs ) const {
- if( x < rhs.x ) return 1;
- if( x > rhs.x ) return 0;
- if( y < rhs.y ) return 1;
- if( y > rhs.y ) return 0;
- return z < rhs.z;
- }
- bool operator == ( const point &rhs ) const{
- return x == rhs.x && y == rhs.y && z == rhs.z;
- }
- };
- #define sqr(A) ((A)*(A))
- node freeList[150100];
- int p[ 150100 ];
- point points[ 150100 ];
- int __cmp0_coord = 0;
- inline bool cmp0( int a , int b ){
- if( __cmp0_coord == 0 ) return points[ a ].x < points[ b ].x;
- if( __cmp0_coord == 1 ) return points[ a ].y < points[ b ].y;
- return points[ a ].z < points[ b ].z;
- }
- int freeCount = 0;
- inline node *getNode( int p ){
- freeList[ freeCount ].p = p;
- freeList[ freeCount ].lc = freeList[ freeCount ].rc = NULL;
- return &freeList[ freeCount ++ ];
- }
- inline node *build( int from , int to , int cd ){
- if( to - from == 0 ) return NULL;
- if( to - from == 1 ) return getNode( p[ from ] );
- int mid = to + from >> 1;
- __cmp0_coord = cd;
- nth_element( p + from , p + mid , p + to , cmp0 );
- node *r = getNode( p[ mid ] );
- r -> lc = build( from , mid , (cd + 1)%3 );
- r -> rc = build( mid + 1 , to , (cd + 1)%3 );
- return r;
- }
- ll dist , cnt;
- point test;
- inline void query( node *t , int cd ){
- ll mydist = sqr( points[ t -> p ].x - test.x ) + sqr( points[ t -> p ].y - test.y ) + sqr( points[ t -> p ].z - test.z );
- if( mydist == 0 ) mydist = 1LL << 60;
- if( mydist < dist ){
- cnt = 1;
- dist = mydist;
- }else if( mydist == dist ){
- cnt ++;
- }
- ll cp = (cd == 0) ? points[ t -> p ].x : ( cd == 1 ) ? points[ t -> p ].y : points[ t -> p ].z;
- ll ca = (cd == 0) ? test.x : (cd == 1 ) ? test.y : test.z;
- if( t -> lc != NULL )if( ca <= cp || (ca >= cp && sqr( cp - ca ) <= dist ) ){
- query( t -> lc , (cd + 1)%3 );
- }
- if( t -> rc != NULL )if( ca >= cp || (ca <= cp && sqr( cp - ca ) <= dist ) ){
- query( t -> rc , (cd + 1)%3 );
- }
- }
- class ClosestPoints {
- public:
- ll seed , range;
- ll getNext(){
- seed = ( seed * 16807LL ) % ((1LL<<31) - 1LL);
- ll rand = (seed % (2LL * range)) - range;
- return rand;
- }
- vector <int> distance(int N, int range, int seed) {
- this -> seed = seed;
- this -> range = range;
- for( int i = 0 ; i < N ; ++i ){
- points[ i ].x = getNext();
- points[ i ].y = getNext();
- points[ i ].z = getNext();
- }
- sort( points , points + N );
- N = unique( points , points + N ) - points;
- for( int i = 0 ; i < N ; ++i ) p[ i ] = i;
- node *root = build( 0 , N , 0 );
- dist = 1LL << 60;
- for( int i = 0 ; i < N ; ++i ){
- test.x = points[ i ].x;
- test.y = points[ i ].y;
- test.z = points[ i ].z;
- query( root , 0 );
- }
- vi r( 2 , 0 );
- r[ 0 ] = dist;
- r[ 1 ] = cnt / 2;
- return r;
- }
- };
- // BEGIN KAWIGIEDIT TESTING
- // Generated by KawigiEdit 2.1.4 (beta) modified by pivanof
- bool KawigiEdit_RunTest(int testNum, int p0, int p1, int p2, bool hasAnswer, vector <int> p3) {
- cout << "Test " << testNum << ": [" << p0 << "," << p1 << "," << p2;
- cout << "]" << endl;
- ClosestPoints *obj;
- vector <int> answer;
- obj = new ClosestPoints();
- clock_t startTime = clock();
- answer = obj->distance(p0, p1, p2);
- clock_t endTime = clock();
- delete obj;
- bool res;
- res = true;
- cout << "Time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl;
- if (hasAnswer) {
- cout << "Desired answer:" << endl;
- cout << "\t" << "{";
- for (int i = 0; int(p3.size()) > i; ++i) {
- if (i > 0) {
- cout << ",";
- }
- cout << p3[i];
- }
- cout << "}" << endl;
- }
- cout << "Your answer:" << endl;
- cout << "\t" << "{";
- for (int i = 0; int(answer.size()) > i; ++i) {
- if (i > 0) {
- cout << ",";
- }
- cout << answer[i];
- }
- cout << "}" << endl;
- if (hasAnswer) {
- if (answer.size() != p3.size()) {
- res = false;
- } else {
- for (int i = 0; int(answer.size()) > i; ++i) {
- if (answer[i] != p3[i]) {
- res = false;
- }
- }
- }
- }
- if (!res) {
- cout << "DOESN'T MATCH!!!!" << endl;
- } else if (double(endTime - startTime) / CLOCKS_PER_SEC >= 2) {
- cout << "FAIL the timeout" << endl;
- res = false;
- } else if (hasAnswer) {
- cout << "Match :-)" << endl;
- } else {
- cout << "OK, but is it right?" << endl;
- }
- cout << "" << endl;
- return res;
- }
- int main() {
- bool all_right;
- all_right = true;
- int p0;
- int p1;
- int p2;
- vector <int> p3;
- {
- // ----- test 0 -----
- p0 = 3;
- p1 = 100;
- p2 = 1;
- int t3[] = {9163,1};
- p3.assign(t3, t3 + sizeof(t3) / sizeof(t3[0]));
- all_right = KawigiEdit_RunTest(0, p0, p1, p2, true, p3) && all_right;
- // ------------------
- }
- {
- // ----- test 1 -----
- p0 = 10000;
- p1 = 1;
- p2 = 7;
- int t3[] = {1,12};
- p3.assign(t3, t3 + sizeof(t3) / sizeof(t3[0]));
- all_right = KawigiEdit_RunTest(1, p0, p1, p2, true, p3) && all_right;
- // ------------------
- }
- {
- // ----- test 2 -----
- p0 = 25;
- p1 = 1;
- p2 = 12;
- int t3[] = {1,9};
- p3.assign(t3, t3 + sizeof(t3) / sizeof(t3[0]));
- all_right = KawigiEdit_RunTest(2, p0, p1, p2, true, p3) && all_right;
- // ------------------
- }
- {
- // ----- test 3 -----
- p0 = 15;
- p1 = 5;
- p2 = 504;
- int t3[] = {5,2};
- p3.assign(t3, t3 + sizeof(t3) / sizeof(t3[0]));
- all_right = KawigiEdit_RunTest(3, p0, p1, p2, true, p3) && all_right;
- // ------------------
- }
- {
- // ----- test 4 -----
- p0 = 50000;
- p1 = 1000000;
- p2 = 75;
- int t3[] = {1252249,1};
- p3.assign(t3, t3 + sizeof(t3) / sizeof(t3[0]));
- all_right = KawigiEdit_RunTest(4, p0, p1, p2, true, p3) && all_right;
- // ------------------
- }
- if (all_right) {
- cout << "You're a stud (at least on the example cases)!" << endl;
- } else {
- cout << "Some of the test cases had errors." << endl;
- }
- return 0;
- }
- // END KAWIGIEDIT TESTING
- //Powered by KawigiEdit 2.1.4 (beta) modified by pivanof!
Advertisement
Add Comment
Please, Sign In to add comment