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