54a6dac9a8 2014-06-19 kinaba: #include <iostream> 54a6dac9a8 2014-06-19 kinaba: #include <sstream> 54a6dac9a8 2014-06-19 kinaba: #include <iomanip> 54a6dac9a8 2014-06-19 kinaba: #include <vector> 54a6dac9a8 2014-06-19 kinaba: #include <string> 54a6dac9a8 2014-06-19 kinaba: #include <map> 54a6dac9a8 2014-06-19 kinaba: #include <set> 54a6dac9a8 2014-06-19 kinaba: #include <algorithm> 54a6dac9a8 2014-06-19 kinaba: #include <numeric> 54a6dac9a8 2014-06-19 kinaba: #include <iterator> 54a6dac9a8 2014-06-19 kinaba: #include <functional> 54a6dac9a8 2014-06-19 kinaba: #include <complex> 54a6dac9a8 2014-06-19 kinaba: #include <queue> 54a6dac9a8 2014-06-19 kinaba: #include <stack> 54a6dac9a8 2014-06-19 kinaba: #include <cmath> 54a6dac9a8 2014-06-19 kinaba: #include <cassert> 54a6dac9a8 2014-06-19 kinaba: #include <tuple> 54a6dac9a8 2014-06-19 kinaba: using namespace std; 54a6dac9a8 2014-06-19 kinaba: typedef long long LL; 54a6dac9a8 2014-06-19 kinaba: typedef complex<double> CMP; 54a6dac9a8 2014-06-19 kinaba: 54a6dac9a8 2014-06-19 kinaba: class TreeColoring { public: 54a6dac9a8 2014-06-19 kinaba: long long color(int N, int Q, int startSeed, int threshold, int maxDist) 54a6dac9a8 2014-06-19 kinaba: { 54a6dac9a8 2014-06-19 kinaba: vector<int> parent(N-1), distance(N-1), queryType(Q), queryNode(Q); 54a6dac9a8 2014-06-19 kinaba: { 54a6dac9a8 2014-06-19 kinaba: int curValue = startSeed; 54a6dac9a8 2014-06-19 kinaba: function<int()> genNextRandom = [&]() { 54a6dac9a8 2014-06-19 kinaba: curValue = (curValue * 1999 + 17) % 1000003; 54a6dac9a8 2014-06-19 kinaba: return curValue; 54a6dac9a8 2014-06-19 kinaba: }; 54a6dac9a8 2014-06-19 kinaba: for(int i=0; i<N-1; i++) { 54a6dac9a8 2014-06-19 kinaba: distance[i] = genNextRandom() % maxDist; 54a6dac9a8 2014-06-19 kinaba: parent[i] = genNextRandom(); 54a6dac9a8 2014-06-19 kinaba: if (parent[i] < threshold) 54a6dac9a8 2014-06-19 kinaba: parent[i] = i; 54a6dac9a8 2014-06-19 kinaba: else 54a6dac9a8 2014-06-19 kinaba: parent[i] = parent[i] % (i + 1); 54a6dac9a8 2014-06-19 kinaba: } 54a6dac9a8 2014-06-19 kinaba: for (int i=0; i<Q; i++) { 54a6dac9a8 2014-06-19 kinaba: queryType[i] = genNextRandom() % 2 + 1; 54a6dac9a8 2014-06-19 kinaba: queryNode[i] = genNextRandom() % N; 54a6dac9a8 2014-06-19 kinaba: } 54a6dac9a8 2014-06-19 kinaba: } 54a6dac9a8 2014-06-19 kinaba: 54a6dac9a8 2014-06-19 kinaba: // Doubled parents for LCA and distance to the root 54a6dac9a8 2014-06-19 kinaba: const int INVALID = -1; 54a6dac9a8 2014-06-19 kinaba: vector<vector<int>> p(N, vector<int>(17, INVALID)); 54a6dac9a8 2014-06-19 kinaba: vector<int> d(N); 54a6dac9a8 2014-06-19 kinaba: for(int v=1; v<N; ++v) { 54a6dac9a8 2014-06-19 kinaba: p[v][1] = parent[v-1]; 54a6dac9a8 2014-06-19 kinaba: d[v] = d[p[v][1]] + distance[v-1]; 54a6dac9a8 2014-06-19 kinaba: } 54a6dac9a8 2014-06-19 kinaba: for(int d=2,k=2; k<17; d*=2,k++) 54a6dac9a8 2014-06-19 kinaba: for(int v=0; v<N; ++v) { 54a6dac9a8 2014-06-19 kinaba: int u = p[v][k-1]; 54a6dac9a8 2014-06-19 kinaba: if(u != INVALID) 54a6dac9a8 2014-06-19 kinaba: p[v][k] = p[u][k-1]; 54a6dac9a8 2014-06-19 kinaba: } 54a6dac9a8 2014-06-19 kinaba: 54a6dac9a8 2014-06-19 kinaba: return -1; 54a6dac9a8 2014-06-19 kinaba: } 54a6dac9a8 2014-06-19 kinaba: }; 54a6dac9a8 2014-06-19 kinaba: 54a6dac9a8 2014-06-19 kinaba: // BEGIN CUT HERE 54a6dac9a8 2014-06-19 kinaba: #include <ctime> 54a6dac9a8 2014-06-19 kinaba: double start_time; string timer() 54a6dac9a8 2014-06-19 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 54a6dac9a8 2014-06-19 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 54a6dac9a8 2014-06-19 kinaba: { os << "{ "; 54a6dac9a8 2014-06-19 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 54a6dac9a8 2014-06-19 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 54a6dac9a8 2014-06-19 kinaba: void verify_case(const long long& Expected, const long long& Received) { 54a6dac9a8 2014-06-19 kinaba: bool ok = (Expected == Received); 54a6dac9a8 2014-06-19 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 54a6dac9a8 2014-06-19 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 54a6dac9a8 2014-06-19 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 54a6dac9a8 2014-06-19 kinaba: #define END verify_case(_, TreeColoring().color(N, Q, startSeed, threshold, maxDist));} 54a6dac9a8 2014-06-19 kinaba: int main(){ 54a6dac9a8 2014-06-19 kinaba: 54a6dac9a8 2014-06-19 kinaba: CASE(0) 54a6dac9a8 2014-06-19 kinaba: int N = 4; 54a6dac9a8 2014-06-19 kinaba: int Q = 6; 54a6dac9a8 2014-06-19 kinaba: int startSeed = 15; 54a6dac9a8 2014-06-19 kinaba: int threshold = 2; 54a6dac9a8 2014-06-19 kinaba: int maxDist = 5; 54a6dac9a8 2014-06-19 kinaba: long long _ = 7LL; 54a6dac9a8 2014-06-19 kinaba: END 54a6dac9a8 2014-06-19 kinaba: CASE(1) 54a6dac9a8 2014-06-19 kinaba: int N = 4; 54a6dac9a8 2014-06-19 kinaba: int Q = 5; 54a6dac9a8 2014-06-19 kinaba: int startSeed = 2; 54a6dac9a8 2014-06-19 kinaba: int threshold = 9; 54a6dac9a8 2014-06-19 kinaba: int maxDist = 10; 54a6dac9a8 2014-06-19 kinaba: long long _ = 30LL; 54a6dac9a8 2014-06-19 kinaba: END 54a6dac9a8 2014-06-19 kinaba: CASE(2) 54a6dac9a8 2014-06-19 kinaba: int N = 8; 54a6dac9a8 2014-06-19 kinaba: int Q = 8; 54a6dac9a8 2014-06-19 kinaba: int startSeed = 3; 54a6dac9a8 2014-06-19 kinaba: int threshold = 5; 54a6dac9a8 2014-06-19 kinaba: int maxDist = 7; 54a6dac9a8 2014-06-19 kinaba: long long _ = 6LL; 54a6dac9a8 2014-06-19 kinaba: END 54a6dac9a8 2014-06-19 kinaba: CASE(3) 54a6dac9a8 2014-06-19 kinaba: int N = 14750; 54a6dac9a8 2014-06-19 kinaba: int Q = 50; 54a6dac9a8 2014-06-19 kinaba: int startSeed = 29750; 54a6dac9a8 2014-06-19 kinaba: int threshold = 1157; 54a6dac9a8 2014-06-19 kinaba: int maxDist = 21610; 54a6dac9a8 2014-06-19 kinaba: long long _ = 2537640LL; 54a6dac9a8 2014-06-19 kinaba: END 54a6dac9a8 2014-06-19 kinaba: CASE(4) 54a6dac9a8 2014-06-19 kinaba: int N = 100000; 54a6dac9a8 2014-06-19 kinaba: int Q = 100000; 54a6dac9a8 2014-06-19 kinaba: int startSeed = 123456; 54a6dac9a8 2014-06-19 kinaba: int threshold = 500000; 54a6dac9a8 2014-06-19 kinaba: int maxDist = 474747; 54a6dac9a8 2014-06-19 kinaba: long long _ = 726915029831LL; 54a6dac9a8 2014-06-19 kinaba: END 54a6dac9a8 2014-06-19 kinaba: CASE(5) 54a6dac9a8 2014-06-19 kinaba: int N = 100000; 54a6dac9a8 2014-06-19 kinaba: int Q = 100000; 54a6dac9a8 2014-06-19 kinaba: int startSeed = 654321; 54a6dac9a8 2014-06-19 kinaba: int threshold = 1000003; 54a6dac9a8 2014-06-19 kinaba: int maxDist = 1000003; 54a6dac9a8 2014-06-19 kinaba: long long _ = 562600687570528LL; 54a6dac9a8 2014-06-19 kinaba: END 54a6dac9a8 2014-06-19 kinaba: /* 54a6dac9a8 2014-06-19 kinaba: CASE(6) 54a6dac9a8 2014-06-19 kinaba: int N = ; 54a6dac9a8 2014-06-19 kinaba: int Q = ; 54a6dac9a8 2014-06-19 kinaba: int startSeed = ; 54a6dac9a8 2014-06-19 kinaba: int threshold = ; 54a6dac9a8 2014-06-19 kinaba: int maxDist = ; 54a6dac9a8 2014-06-19 kinaba: long long _ = LL; 54a6dac9a8 2014-06-19 kinaba: END 54a6dac9a8 2014-06-19 kinaba: CASE(7) 54a6dac9a8 2014-06-19 kinaba: int N = ; 54a6dac9a8 2014-06-19 kinaba: int Q = ; 54a6dac9a8 2014-06-19 kinaba: int startSeed = ; 54a6dac9a8 2014-06-19 kinaba: int threshold = ; 54a6dac9a8 2014-06-19 kinaba: int maxDist = ; 54a6dac9a8 2014-06-19 kinaba: long long _ = LL; 54a6dac9a8 2014-06-19 kinaba: END 54a6dac9a8 2014-06-19 kinaba: */ 54a6dac9a8 2014-06-19 kinaba: } 54a6dac9a8 2014-06-19 kinaba: // END CUT HERE