40c16bd858 2012-04-11 kinaba: #include <iostream> 40c16bd858 2012-04-11 kinaba: #include <sstream> 40c16bd858 2012-04-11 kinaba: #include <iomanip> 40c16bd858 2012-04-11 kinaba: #include <vector> 40c16bd858 2012-04-11 kinaba: #include <string> 40c16bd858 2012-04-11 kinaba: #include <map> 40c16bd858 2012-04-11 kinaba: #include <set> 40c16bd858 2012-04-11 kinaba: #include <algorithm> 40c16bd858 2012-04-11 kinaba: #include <numeric> 40c16bd858 2012-04-11 kinaba: #include <iterator> 40c16bd858 2012-04-11 kinaba: #include <functional> 40c16bd858 2012-04-11 kinaba: #include <complex> 40c16bd858 2012-04-11 kinaba: #include <queue> 40c16bd858 2012-04-11 kinaba: #include <stack> 40c16bd858 2012-04-11 kinaba: #include <cmath> 40c16bd858 2012-04-11 kinaba: #include <cassert> 40c16bd858 2012-04-11 kinaba: #include <cstring> 40c16bd858 2012-04-11 kinaba: #ifdef __GNUC__ 40c16bd858 2012-04-11 kinaba: #include <ext/hash_map> 40c16bd858 2012-04-11 kinaba: #define unordered_map __gnu_cxx::hash_map 40c16bd858 2012-04-11 kinaba: #else 40c16bd858 2012-04-11 kinaba: #include <unordered_map> 40c16bd858 2012-04-11 kinaba: #endif 40c16bd858 2012-04-11 kinaba: using namespace std; 40c16bd858 2012-04-11 kinaba: typedef long long LL; 40c16bd858 2012-04-11 kinaba: typedef complex<double> CMP; 40c16bd858 2012-04-11 kinaba: 40c16bd858 2012-04-11 kinaba: class Over9000Rocks { public: 40c16bd858 2012-04-11 kinaba: int countPossibilities(vector <int> lowerBound, vector <int> upperBound) 40c16bd858 2012-04-11 kinaba: { 40c16bd858 2012-04-11 kinaba: vector< pair<int,bool> > ev; 40c16bd858 2012-04-11 kinaba: const int MIN = 9001; 40c16bd858 2012-04-11 kinaba: const int N = lowerBound.size(); 40c16bd858 2012-04-11 kinaba: for(int mask=1; mask<(1<<N); ++mask) 40c16bd858 2012-04-11 kinaba: { 40c16bd858 2012-04-11 kinaba: int lo=0, hi=0; 40c16bd858 2012-04-11 kinaba: for(int i=0; i<N; ++i) 40c16bd858 2012-04-11 kinaba: if( mask & (1<<i) ) { 40c16bd858 2012-04-11 kinaba: lo += lowerBound[i]; 40c16bd858 2012-04-11 kinaba: hi += upperBound[i]; 40c16bd858 2012-04-11 kinaba: } 40c16bd858 2012-04-11 kinaba: if(hi<MIN) 40c16bd858 2012-04-11 kinaba: continue; 40c16bd858 2012-04-11 kinaba: lo = max(lo, MIN); 40c16bd858 2012-04-11 kinaba: ev.push_back( make_pair(lo,true) ); 40c16bd858 2012-04-11 kinaba: ev.push_back( make_pair(hi+1,false) ); 40c16bd858 2012-04-11 kinaba: } 40c16bd858 2012-04-11 kinaba: sort(ev.begin(), ev.end()); 40c16bd858 2012-04-11 kinaba: int nest = 0; 40c16bd858 2012-04-11 kinaba: int entered = -1; 40c16bd858 2012-04-11 kinaba: int total = 0; 40c16bd858 2012-04-11 kinaba: for(int k=0; k<ev.size(); ++k) 40c16bd858 2012-04-11 kinaba: { 40c16bd858 2012-04-11 kinaba: int x = ev[k].first; 40c16bd858 2012-04-11 kinaba: bool in = ev[k].second; 40c16bd858 2012-04-11 kinaba: if( nest == 0 ) { 40c16bd858 2012-04-11 kinaba: if( in ) 40c16bd858 2012-04-11 kinaba: entered = x; 40c16bd858 2012-04-11 kinaba: } else if( nest == 1 ) { 40c16bd858 2012-04-11 kinaba: if( !in ) 40c16bd858 2012-04-11 kinaba: total += (x - entered); 40c16bd858 2012-04-11 kinaba: } 40c16bd858 2012-04-11 kinaba: if(in) nest++; else nest--; 40c16bd858 2012-04-11 kinaba: } 40c16bd858 2012-04-11 kinaba: return total; 40c16bd858 2012-04-11 kinaba: } 40c16bd858 2012-04-11 kinaba: }; 40c16bd858 2012-04-11 kinaba: 40c16bd858 2012-04-11 kinaba: // BEGIN CUT HERE 40c16bd858 2012-04-11 kinaba: #include <ctime> 40c16bd858 2012-04-11 kinaba: double start_time; string timer() 40c16bd858 2012-04-11 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 40c16bd858 2012-04-11 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 40c16bd858 2012-04-11 kinaba: { os << "{ "; 40c16bd858 2012-04-11 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 40c16bd858 2012-04-11 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 40c16bd858 2012-04-11 kinaba: void verify_case(const int& Expected, const int& Received) { 40c16bd858 2012-04-11 kinaba: bool ok = (Expected == Received); 40c16bd858 2012-04-11 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 40c16bd858 2012-04-11 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 40c16bd858 2012-04-11 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 40c16bd858 2012-04-11 kinaba: #define END verify_case(_, Over9000Rocks().countPossibilities(lowerBound, upperBound));} 40c16bd858 2012-04-11 kinaba: int main(){ 40c16bd858 2012-04-11 kinaba: 40c16bd858 2012-04-11 kinaba: CASE(0) 40c16bd858 2012-04-11 kinaba: int lowerBound_[] = {9000}; 40c16bd858 2012-04-11 kinaba: vector <int> lowerBound(lowerBound_, lowerBound_+sizeof(lowerBound_)/sizeof(*lowerBound_)); 40c16bd858 2012-04-11 kinaba: int upperBound_[] = {9001}; 40c16bd858 2012-04-11 kinaba: vector <int> upperBound(upperBound_, upperBound_+sizeof(upperBound_)/sizeof(*upperBound_)); 40c16bd858 2012-04-11 kinaba: int _ = 1; 40c16bd858 2012-04-11 kinaba: END 40c16bd858 2012-04-11 kinaba: CASE(1) 40c16bd858 2012-04-11 kinaba: int lowerBound_[] = {9000, 1, 10}; 40c16bd858 2012-04-11 kinaba: vector <int> lowerBound(lowerBound_, lowerBound_+sizeof(lowerBound_)/sizeof(*lowerBound_)); 40c16bd858 2012-04-11 kinaba: int upperBound_[] = {9000, 2, 20}; 40c16bd858 2012-04-11 kinaba: vector <int> upperBound(upperBound_, upperBound_+sizeof(upperBound_)/sizeof(*upperBound_)); 40c16bd858 2012-04-11 kinaba: int _ = 15; 40c16bd858 2012-04-11 kinaba: END 40c16bd858 2012-04-11 kinaba: CASE(2) 40c16bd858 2012-04-11 kinaba: int lowerBound_[] = {1001, 2001, 3001, 3001}; 40c16bd858 2012-04-11 kinaba: vector <int> lowerBound(lowerBound_, lowerBound_+sizeof(lowerBound_)/sizeof(*lowerBound_)); 40c16bd858 2012-04-11 kinaba: int upperBound_[] = {1003, 2003, 3003, 3003}; 40c16bd858 2012-04-11 kinaba: vector <int> upperBound(upperBound_, upperBound_+sizeof(upperBound_)/sizeof(*upperBound_)); 40c16bd858 2012-04-11 kinaba: int _ = 9; 40c16bd858 2012-04-11 kinaba: END 40c16bd858 2012-04-11 kinaba: CASE(3) 40c16bd858 2012-04-11 kinaba: int lowerBound_[] = {9000,90000,1,10}; 40c16bd858 2012-04-11 kinaba: vector <int> lowerBound(lowerBound_, lowerBound_+sizeof(lowerBound_)/sizeof(*lowerBound_)); 40c16bd858 2012-04-11 kinaba: int upperBound_[] = {9000,90000,3,15}; 40c16bd858 2012-04-11 kinaba: vector <int> upperBound(upperBound_, upperBound_+sizeof(upperBound_)/sizeof(*upperBound_)); 40c16bd858 2012-04-11 kinaba: int _ = 38; 40c16bd858 2012-04-11 kinaba: END 40c16bd858 2012-04-11 kinaba: CASE(4) 40c16bd858 2012-04-11 kinaba: int lowerBound_[] = {1,1,1,1,1,1}; 40c16bd858 2012-04-11 kinaba: vector <int> lowerBound(lowerBound_, lowerBound_+sizeof(lowerBound_)/sizeof(*lowerBound_)); 40c16bd858 2012-04-11 kinaba: int upperBound_[] = {3,4,5,6,7,8}; 40c16bd858 2012-04-11 kinaba: vector <int> upperBound(upperBound_, upperBound_+sizeof(upperBound_)/sizeof(*upperBound_)); 40c16bd858 2012-04-11 kinaba: int _ = 0; 40c16bd858 2012-04-11 kinaba: END 40c16bd858 2012-04-11 kinaba: CASE(5) 40c16bd858 2012-04-11 kinaba: int lowerBound_[] = {242,7393,3738,2594,3051,7711,3386,4363,6035,4752,5384,7607,5702,2226,7792}; 40c16bd858 2012-04-11 kinaba: vector <int> lowerBound(lowerBound_, lowerBound_+sizeof(lowerBound_)/sizeof(*lowerBound_)); 40c16bd858 2012-04-11 kinaba: int upperBound_[] = {9138,9120,9027,9185,9081,9114,9044,9089,9178,9103,9089,9026,9129,9050,9173}; 40c16bd858 2012-04-11 kinaba: vector <int> upperBound(upperBound_, upperBound_+sizeof(upperBound_)/sizeof(*upperBound_)); 40c16bd858 2012-04-11 kinaba: int _ = -1; 40c16bd858 2012-04-11 kinaba: END 40c16bd858 2012-04-11 kinaba: CASE(6) 40c16bd858 2012-04-11 kinaba: int lowerBound_[] = {9001}; 40c16bd858 2012-04-11 kinaba: vector <int> lowerBound(lowerBound_, lowerBound_+sizeof(lowerBound_)/sizeof(*lowerBound_)); 40c16bd858 2012-04-11 kinaba: int upperBound_[] = {9002}; 40c16bd858 2012-04-11 kinaba: vector <int> upperBound(upperBound_, upperBound_+sizeof(upperBound_)/sizeof(*upperBound_)); 40c16bd858 2012-04-11 kinaba: int _ = 2; 40c16bd858 2012-04-11 kinaba: END 40c16bd858 2012-04-11 kinaba: CASE(7) 40c16bd858 2012-04-11 kinaba: int lowerBound_[] = {9000, 1, 2, 3}; 40c16bd858 2012-04-11 kinaba: vector <int> lowerBound(lowerBound_, lowerBound_+sizeof(lowerBound_)/sizeof(*lowerBound_)); 40c16bd858 2012-04-11 kinaba: int upperBound_[] = {9000, 1, 2, 3}; 40c16bd858 2012-04-11 kinaba: vector <int> upperBound(upperBound_, upperBound_+sizeof(upperBound_)/sizeof(*upperBound_)); 40c16bd858 2012-04-11 kinaba: int _ = 10; 40c16bd858 2012-04-11 kinaba: END 40c16bd858 2012-04-11 kinaba: 40c16bd858 2012-04-11 kinaba: } 40c16bd858 2012-04-11 kinaba: // END CUT HERE