6cfba8c225 2015-12-10 kinaba: #include <iostream> 6cfba8c225 2015-12-10 kinaba: #include <sstream> 6cfba8c225 2015-12-10 kinaba: #include <iomanip> 6cfba8c225 2015-12-10 kinaba: #include <vector> 6cfba8c225 2015-12-10 kinaba: #include <string> 6cfba8c225 2015-12-10 kinaba: #include <map> 6cfba8c225 2015-12-10 kinaba: #include <set> 6cfba8c225 2015-12-10 kinaba: #include <algorithm> 6cfba8c225 2015-12-10 kinaba: #include <numeric> 6cfba8c225 2015-12-10 kinaba: #include <iterator> 6cfba8c225 2015-12-10 kinaba: #include <functional> 6cfba8c225 2015-12-10 kinaba: #include <complex> 6cfba8c225 2015-12-10 kinaba: #include <queue> 6cfba8c225 2015-12-10 kinaba: #include <stack> 6cfba8c225 2015-12-10 kinaba: #include <cmath> 6cfba8c225 2015-12-10 kinaba: #include <cassert> 6cfba8c225 2015-12-10 kinaba: #include <tuple> 6cfba8c225 2015-12-10 kinaba: using namespace std; 6cfba8c225 2015-12-10 kinaba: typedef long long LL; 6cfba8c225 2015-12-10 kinaba: typedef complex<double> CMP; 6cfba8c225 2015-12-10 kinaba: 6cfba8c225 2015-12-10 kinaba: class FindingKids { public: 6cfba8c225 2015-12-10 kinaba: long long getSum(int n, int q, int A_, const int B, const int C) 6cfba8c225 2015-12-10 kinaba: { 6cfba8c225 2015-12-10 kinaba: set<int> pos_set; 6cfba8c225 2015-12-10 kinaba: vector<int> pos(n), dir(n); 6cfba8c225 2015-12-10 kinaba: 6cfba8c225 2015-12-10 kinaba: const int M = 1000000007; 6cfba8c225 2015-12-10 kinaba: LL A = A_; 6cfba8c225 2015-12-10 kinaba: for(int i=0; i<n; ++i) { 6cfba8c225 2015-12-10 kinaba: A = (A * B + C) % M; 6cfba8c225 2015-12-10 kinaba: int p = int(A % (M - n + i + 1)); 6cfba8c225 2015-12-10 kinaba: if(pos_set.count(p)) 6cfba8c225 2015-12-10 kinaba: p = M - n + i; 6cfba8c225 2015-12-10 kinaba: pos[i] = p; 6cfba8c225 2015-12-10 kinaba: pos_set.insert(p); 6cfba8c225 2015-12-10 kinaba: if(p%2 == 0) 6cfba8c225 2015-12-10 kinaba: dir[i] = +1; 6cfba8c225 2015-12-10 kinaba: else 6cfba8c225 2015-12-10 kinaba: dir[i] = -1; 6cfba8c225 2015-12-10 kinaba: } 6cfba8c225 2015-12-10 kinaba: 6cfba8c225 2015-12-10 kinaba: vector<int> kid(q), time(q); 6cfba8c225 2015-12-10 kinaba: for(int i=0; i<q; ++i) { 6cfba8c225 2015-12-10 kinaba: A = (A * B + C) % M; 6cfba8c225 2015-12-10 kinaba: kid[i] = int(A%n); 6cfba8c225 2015-12-10 kinaba: A = (A * B + C) % M; 6cfba8c225 2015-12-10 kinaba: time[i] = int(A); 6cfba8c225 2015-12-10 kinaba: } 6cfba8c225 2015-12-10 kinaba: 6cfba8c225 2015-12-10 kinaba: return solve(n, pos, dir, q, kid, time); 6cfba8c225 2015-12-10 kinaba: } 6cfba8c225 2015-12-10 kinaba: 6cfba8c225 2015-12-10 kinaba: LL solve( 6cfba8c225 2015-12-10 kinaba: const int N, const vector<int>& P, const vector<int>& D, 6cfba8c225 2015-12-10 kinaba: const int Q, const vector<int>& K, const vector<int>& T) { 6cfba8c225 2015-12-10 kinaba: 6cfba8c225 2015-12-10 kinaba: vector<int> kid_id_to_pos_order(N); { 6cfba8c225 2015-12-10 kinaba: vector<int> kids_sorted_by_pos(N); 6cfba8c225 2015-12-10 kinaba: for(int i=0; i<N; ++i) kids_sorted_by_pos[i] = i; 6cfba8c225 2015-12-10 kinaba: sort(kids_sorted_by_pos.begin(), kids_sorted_by_pos.end(), [&](int a, int b){ 6cfba8c225 2015-12-10 kinaba: return P[a] < P[b]; 6cfba8c225 2015-12-10 kinaba: }); 6cfba8c225 2015-12-10 kinaba: for(int i=0; i<N; ++i) 6cfba8c225 2015-12-10 kinaba: kid_id_to_pos_order[kids_sorted_by_pos[i]] = i; 6cfba8c225 2015-12-10 kinaba: } 6cfba8c225 2015-12-10 kinaba: 6cfba8c225 2015-12-10 kinaba: vector<LL> move_kids, stay_kids; 6cfba8c225 2015-12-10 kinaba: for(int i=0; i<N; ++i) 6cfba8c225 2015-12-10 kinaba: (D[i] == +1 ? move_kids : stay_kids).emplace_back(P[i]); 6cfba8c225 2015-12-10 kinaba: 6cfba8c225 2015-12-10 kinaba: sort(move_kids.begin(), move_kids.end()); 6cfba8c225 2015-12-10 kinaba: sort(stay_kids.begin(), stay_kids.end()); 6cfba8c225 2015-12-10 kinaba: 6cfba8c225 2015-12-10 kinaba: LL total = 0; 6cfba8c225 2015-12-10 kinaba: for(int i=0; i<Q; ++i) { 6cfba8c225 2015-12-10 kinaba: LL p = find_kth_left(kid_id_to_pos_order[K[i]], stay_kids, move_kids, 2*T[i]); 6cfba8c225 2015-12-10 kinaba: total += abs(p - T[i]); 6cfba8c225 2015-12-10 kinaba: } 6cfba8c225 2015-12-10 kinaba: return total; 6cfba8c225 2015-12-10 kinaba: } 6cfba8c225 2015-12-10 kinaba: 6cfba8c225 2015-12-10 kinaba: // Find the $k$-th left kid in {stay_kids} U {move_kids += t}. 6cfba8c225 2015-12-10 kinaba: LL find_kth_left(const int k, const vector<LL>& stay_kids, const vector<LL>& move_kids, const LL t) { 6cfba8c225 2015-12-10 kinaba: if(stay_kids.empty()) return move_kids[k]+t; 6cfba8c225 2015-12-10 kinaba: if(move_kids.empty()) return stay_kids[k]; 6cfba8c225 2015-12-10 kinaba: if(stay_kids.back() < move_kids.front()+t) { 6cfba8c225 2015-12-10 kinaba: if(k < stay_kids.size()) 6cfba8c225 2015-12-10 kinaba: return stay_kids[k]; 6cfba8c225 2015-12-10 kinaba: else 6cfba8c225 2015-12-10 kinaba: return move_kids[k-stay_kids.size()]+t; 6cfba8c225 2015-12-10 kinaba: } 6cfba8c225 2015-12-10 kinaba: if(move_kids.back()+t < stay_kids.front()) { 6cfba8c225 2015-12-10 kinaba: if(k < move_kids.size()) 6cfba8c225 2015-12-10 kinaba: return move_kids[k]+t; 6cfba8c225 2015-12-10 kinaba: else 6cfba8c225 2015-12-10 kinaba: return stay_kids[k-move_kids.size()]+t; 6cfba8c225 2015-12-10 kinaba: } 6cfba8c225 2015-12-10 kinaba: 6cfba8c225 2015-12-10 kinaba: int L=0, R=stay_kids.size(); 6cfba8c225 2015-12-10 kinaba: while(R-L>1) { 6cfba8c225 2015-12-10 kinaba: int C = (L+R)/2; 6cfba8c225 2015-12-10 kinaba: int left_in_stay = C; 6cfba8c225 2015-12-10 kinaba: int left_in_move = lower_bound(move_kids.begin(), move_kids.end(), stay_kids[C]-t) - move_kids.begin(); 6cfba8c225 2015-12-10 kinaba: int left = left_in_stay + left_in_move; 6cfba8c225 2015-12-10 kinaba: if(left == k) 6cfba8c225 2015-12-10 kinaba: return stay_kids[C]; 6cfba8c225 2015-12-10 kinaba: (left >= k ? R : L) = C; 6cfba8c225 2015-12-10 kinaba: } 6cfba8c225 2015-12-10 kinaba: 6cfba8c225 2015-12-10 kinaba: L=0, R=move_kids.size(); 6cfba8c225 2015-12-10 kinaba: while(R-L>1) { 6cfba8c225 2015-12-10 kinaba: int C = (L+R)/2; 6cfba8c225 2015-12-10 kinaba: int left_in_move = C; 6cfba8c225 2015-12-10 kinaba: int left_in_stay = lower_bound(stay_kids.begin(), stay_kids.end(), move_kids[C]+t) - stay_kids.begin(); 6cfba8c225 2015-12-10 kinaba: int left = left_in_stay + left_in_move; 6cfba8c225 2015-12-10 kinaba: if(left == k) 6cfba8c225 2015-12-10 kinaba: return move_kids[C]+t; 6cfba8c225 2015-12-10 kinaba: (left >= k ? R : L) = C; 6cfba8c225 2015-12-10 kinaba: } 6cfba8c225 2015-12-10 kinaba: 6cfba8c225 2015-12-10 kinaba: vector<LL> now = stay_kids; 6cfba8c225 2015-12-10 kinaba: for(LL p: move_kids) now.push_back(p + t); 6cfba8c225 2015-12-10 kinaba: sort(now.begin(), now.end()); 6cfba8c225 2015-12-10 kinaba: return now[k]; 6cfba8c225 2015-12-10 kinaba: } 6cfba8c225 2015-12-10 kinaba: }; 6cfba8c225 2015-12-10 kinaba: 6cfba8c225 2015-12-10 kinaba: // BEGIN CUT HERE 6cfba8c225 2015-12-10 kinaba: #include <ctime> 6cfba8c225 2015-12-10 kinaba: double start_time; string timer() 6cfba8c225 2015-12-10 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 6cfba8c225 2015-12-10 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 6cfba8c225 2015-12-10 kinaba: { os << "{ "; 6cfba8c225 2015-12-10 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 6cfba8c225 2015-12-10 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 6cfba8c225 2015-12-10 kinaba: void verify_case(const long long& Expected, const long long& Received) { 6cfba8c225 2015-12-10 kinaba: bool ok = (Expected == Received); 6cfba8c225 2015-12-10 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 6cfba8c225 2015-12-10 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 6cfba8c225 2015-12-10 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 6cfba8c225 2015-12-10 kinaba: #define END verify_case(_, FindingKids().getSum(n, q, A, B, C));} 6cfba8c225 2015-12-10 kinaba: int main(){ 6cfba8c225 2015-12-10 kinaba: 6cfba8c225 2015-12-10 kinaba: CASE(0) 6cfba8c225 2015-12-10 kinaba: int n = 5; 6cfba8c225 2015-12-10 kinaba: int q = 2; 6cfba8c225 2015-12-10 kinaba: int A = 0; 6cfba8c225 2015-12-10 kinaba: int B = 1; 6cfba8c225 2015-12-10 kinaba: int C = 1; 6cfba8c225 2015-12-10 kinaba: long long _ = 15LL; 6cfba8c225 2015-12-10 kinaba: END 6cfba8c225 2015-12-10 kinaba: CASE(1) 6cfba8c225 2015-12-10 kinaba: int n = 5; 6cfba8c225 2015-12-10 kinaba: int q = 4; 6cfba8c225 2015-12-10 kinaba: int A = 3; 6cfba8c225 2015-12-10 kinaba: int B = 2; 6cfba8c225 2015-12-10 kinaba: int C = 1; 6cfba8c225 2015-12-10 kinaba: long long _ = 43376LL; 6cfba8c225 2015-12-10 kinaba: END 6cfba8c225 2015-12-10 kinaba: CASE(2) 6cfba8c225 2015-12-10 kinaba: int n = 200000; 6cfba8c225 2015-12-10 kinaba: int q = 200000; 6cfba8c225 2015-12-10 kinaba: int A = 12345; 6cfba8c225 2015-12-10 kinaba: int B = 67890; 6cfba8c225 2015-12-10 kinaba: int C = 111213141; 6cfba8c225 2015-12-10 kinaba: long long _ = 133378408428237LL; 6cfba8c225 2015-12-10 kinaba: END 6cfba8c225 2015-12-10 kinaba: /* 6cfba8c225 2015-12-10 kinaba: CASE(3) 6cfba8c225 2015-12-10 kinaba: int n = ; 6cfba8c225 2015-12-10 kinaba: int q = ; 6cfba8c225 2015-12-10 kinaba: int A = ; 6cfba8c225 2015-12-10 kinaba: int B = ; 6cfba8c225 2015-12-10 kinaba: int C = ; 6cfba8c225 2015-12-10 kinaba: long long _ = LL; 6cfba8c225 2015-12-10 kinaba: END 6cfba8c225 2015-12-10 kinaba: CASE(4) 6cfba8c225 2015-12-10 kinaba: int n = ; 6cfba8c225 2015-12-10 kinaba: int q = ; 6cfba8c225 2015-12-10 kinaba: int A = ; 6cfba8c225 2015-12-10 kinaba: int B = ; 6cfba8c225 2015-12-10 kinaba: int C = ; 6cfba8c225 2015-12-10 kinaba: long long _ = LL; 6cfba8c225 2015-12-10 kinaba: END 6cfba8c225 2015-12-10 kinaba: */ 6cfba8c225 2015-12-10 kinaba: } 6cfba8c225 2015-12-10 kinaba: // END CUT HERE