b3c7c622f4 2015-12-18 kinaba: #include <iostream> b3c7c622f4 2015-12-18 kinaba: #include <sstream> b3c7c622f4 2015-12-18 kinaba: #include <iomanip> b3c7c622f4 2015-12-18 kinaba: #include <vector> b3c7c622f4 2015-12-18 kinaba: #include <string> b3c7c622f4 2015-12-18 kinaba: #include <map> b3c7c622f4 2015-12-18 kinaba: #include <set> b3c7c622f4 2015-12-18 kinaba: #include <algorithm> b3c7c622f4 2015-12-18 kinaba: #include <numeric> b3c7c622f4 2015-12-18 kinaba: #include <iterator> b3c7c622f4 2015-12-18 kinaba: #include <functional> b3c7c622f4 2015-12-18 kinaba: #include <complex> b3c7c622f4 2015-12-18 kinaba: #include <queue> b3c7c622f4 2015-12-18 kinaba: #include <stack> b3c7c622f4 2015-12-18 kinaba: #include <cmath> b3c7c622f4 2015-12-18 kinaba: #include <cassert> b3c7c622f4 2015-12-18 kinaba: #include <tuple> b3c7c622f4 2015-12-18 kinaba: using namespace std; b3c7c622f4 2015-12-18 kinaba: typedef long long LL; b3c7c622f4 2015-12-18 kinaba: typedef complex<double> CMP; b3c7c622f4 2015-12-18 kinaba: b3c7c622f4 2015-12-18 kinaba: class LimitedMemorySeries1 { public: b3c7c622f4 2015-12-18 kinaba: long long getSum(int n, int x0, int a, int b, const vector <int>& query) b3c7c622f4 2015-12-18 kinaba: { b3c7c622f4 2015-12-18 kinaba: LL sum = 0; b3c7c622f4 2015-12-18 kinaba: multiset<int> qq(query.begin(), query.end()); b3c7c622f4 2015-12-18 kinaba: b3c7c622f4 2015-12-18 kinaba: const int BACKET = min(n, 250000); b3c7c622f4 2015-12-18 kinaba: int prev_turn_largest = -1; b3c7c622f4 2015-12-18 kinaba: int ptl_count = 0; b3c7c622f4 2015-12-18 kinaba: b3c7c622f4 2015-12-18 kinaba: int* pq = new int[BACKET+1]; b3c7c622f4 2015-12-18 kinaba: int pql = 0; b3c7c622f4 2015-12-18 kinaba: b3c7c622f4 2015-12-18 kinaba: for(int s=0; s<n; s+=BACKET) { b3c7c622f4 2015-12-18 kinaba: pql = 0; // PQ keeping min BACKET elements b3c7c622f4 2015-12-18 kinaba: b3c7c622f4 2015-12-18 kinaba: int v = x0; b3c7c622f4 2015-12-18 kinaba: for(int i=0; i<n; ++i) { b3c7c622f4 2015-12-18 kinaba: if(v < prev_turn_largest) {} b3c7c622f4 2015-12-18 kinaba: else if(v == prev_turn_largest && ptl_count>0) {ptl_count--;} b3c7c622f4 2015-12-18 kinaba: else { b3c7c622f4 2015-12-18 kinaba: pq[pql++]=v; push_heap(pq+0, pq+pql); b3c7c622f4 2015-12-18 kinaba: if(pql>BACKET) b3c7c622f4 2015-12-18 kinaba: pop_heap(pq+0, pq+pql), pql--; // remove largest b3c7c622f4 2015-12-18 kinaba: } b3c7c622f4 2015-12-18 kinaba: v = int((LL(v) * a + b) % 1000000007); b3c7c622f4 2015-12-18 kinaba: } b3c7c622f4 2015-12-18 kinaba: int cur_largest = -1; b3c7c622f4 2015-12-18 kinaba: int cl_count = 0; b3c7c622f4 2015-12-18 kinaba: b3c7c622f4 2015-12-18 kinaba: for(int rank=s+pql-1; rank>=s; --rank) { b3c7c622f4 2015-12-18 kinaba: int v = pq[0]; b3c7c622f4 2015-12-18 kinaba: pop_heap(pq+0, pq+pql), pql--; b3c7c622f4 2015-12-18 kinaba: b3c7c622f4 2015-12-18 kinaba: if(cur_largest < v) cur_largest = v, cl_count = 1; b3c7c622f4 2015-12-18 kinaba: else if(cur_largest == v) cl_count++; b3c7c622f4 2015-12-18 kinaba: b3c7c622f4 2015-12-18 kinaba: if(qq.count(rank)) { b3c7c622f4 2015-12-18 kinaba: sum += LL(v) * qq.count(rank); b3c7c622f4 2015-12-18 kinaba: qq.erase(rank); b3c7c622f4 2015-12-18 kinaba: } b3c7c622f4 2015-12-18 kinaba: } b3c7c622f4 2015-12-18 kinaba: b3c7c622f4 2015-12-18 kinaba: prev_turn_largest = cur_largest; b3c7c622f4 2015-12-18 kinaba: ptl_count = cl_count; b3c7c622f4 2015-12-18 kinaba: } b3c7c622f4 2015-12-18 kinaba: delete[] pq; b3c7c622f4 2015-12-18 kinaba: return sum; b3c7c622f4 2015-12-18 kinaba: } b3c7c622f4 2015-12-18 kinaba: }; b3c7c622f4 2015-12-18 kinaba: b3c7c622f4 2015-12-18 kinaba: // BEGIN CUT HERE b3c7c622f4 2015-12-18 kinaba: #include <ctime> b3c7c622f4 2015-12-18 kinaba: double start_time; string timer() b3c7c622f4 2015-12-18 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } b3c7c622f4 2015-12-18 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) b3c7c622f4 2015-12-18 kinaba: { os << "{ "; b3c7c622f4 2015-12-18 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) b3c7c622f4 2015-12-18 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } b3c7c622f4 2015-12-18 kinaba: void verify_case(const long long& Expected, const long long& Received) { b3c7c622f4 2015-12-18 kinaba: bool ok = (Expected == Received); b3c7c622f4 2015-12-18 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; b3c7c622f4 2015-12-18 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } b3c7c622f4 2015-12-18 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); b3c7c622f4 2015-12-18 kinaba: #define END verify_case(_, LimitedMemorySeries1().getSum(n, x0, a, b, query));} b3c7c622f4 2015-12-18 kinaba: int main(){ b3c7c622f4 2015-12-18 kinaba: b3c7c622f4 2015-12-18 kinaba: CASE(0) b3c7c622f4 2015-12-18 kinaba: int n = 5; b3c7c622f4 2015-12-18 kinaba: int x0 = 100; b3c7c622f4 2015-12-18 kinaba: int a = 1; b3c7c622f4 2015-12-18 kinaba: int b = 5; b3c7c622f4 2015-12-18 kinaba: int query_[] = {0,3}; b3c7c622f4 2015-12-18 kinaba: vector <int> query(query_, query_+sizeof(query_)/sizeof(*query_)); b3c7c622f4 2015-12-18 kinaba: long long _ = 215LL; b3c7c622f4 2015-12-18 kinaba: END b3c7c622f4 2015-12-18 kinaba: CASE(1) b3c7c622f4 2015-12-18 kinaba: int n = 5; b3c7c622f4 2015-12-18 kinaba: int x0 = 123456789; b3c7c622f4 2015-12-18 kinaba: int a = 987654321; b3c7c622f4 2015-12-18 kinaba: int b = 1000000006; b3c7c622f4 2015-12-18 kinaba: int query_[] = {0,1,2,3}; b3c7c622f4 2015-12-18 kinaba: vector <int> query(query_, query_+sizeof(query_)/sizeof(*query_)); b3c7c622f4 2015-12-18 kinaba: long long _ = 733028692LL; b3c7c622f4 2015-12-18 kinaba: END b3c7c622f4 2015-12-18 kinaba: CASE(2) b3c7c622f4 2015-12-18 kinaba: int n = 5000000; b3c7c622f4 2015-12-18 kinaba: int x0 = 482995837; b3c7c622f4 2015-12-18 kinaba: int a = 512849030; b3c7c622f4 2015-12-18 kinaba: int b = 120583724; b3c7c622f4 2015-12-18 kinaba: int query_[] = {4854010,3139503,1855546,219904,846357,2624639,891260,217467,4940091,4802760,2424821,424076, b3c7c622f4 2015-12-18 kinaba: 3848272,2062765,2922407,4850301,1279972,4929307,2035456,3562859,1749594,4089499,3797495,1013980, b3c7c622f4 2015-12-18 kinaba: 1650805,1245213,3020379,4661668,427170,1176815,292944,2435328,420809,4170355,2635197,3940607, b3c7c622f4 2015-12-18 kinaba: 4311718,2098572,4868054,30319,4588969,1460677,1335028,3921495,3621970,4459335,966000,3686977, b3c7c622f4 2015-12-18 kinaba: 2316560,1634961,2280624,1940395,3321546,3168811,1856547,3859093,4340475,1382782,3482928,2517843, b3c7c622f4 2015-12-18 kinaba: 185008,1135373,2821050,3260484,4821220,1700954,3243343,2183615,394339,2630121,1811267,1060542, b3c7c622f4 2015-12-18 kinaba: 3898314,892312,2015580,11161,4965095,2128470,2320933,1095881,3938450,1887098,975426,2098073,3300937, b3c7c622f4 2015-12-18 kinaba: 1145560,2894728,1026181,3259403,4509345,3610224,3584456,1877483,2665752,2243671,616205,504849,3068426, b3c7c622f4 2015-12-18 kinaba: 1471925,4144568}; b3c7c622f4 2015-12-18 kinaba: vector <int> query(query_, query_+sizeof(query_)/sizeof(*query_)); b3c7c622f4 2015-12-18 kinaba: long long _ = 49684994148LL; b3c7c622f4 2015-12-18 kinaba: END b3c7c622f4 2015-12-18 kinaba: CASE(2) b3c7c622f4 2015-12-18 kinaba: int n = 5000000; b3c7c622f4 2015-12-18 kinaba: int x0 = 482995837; b3c7c622f4 2015-12-18 kinaba: int a = 512849030; b3c7c622f4 2015-12-18 kinaba: int b = 120583724; b3c7c622f4 2015-12-18 kinaba: int query_[] = {5000000,4999999,4999998,4999997,4999996,4999995,4999994,4999993,4999992,4999991,4999990,4999989,4999988,4999987,4999986,4999985,4999984,4999983,4999982,4999981,4999980,4999979,4999978,4999977,4999976,4999975,4999974,4999973,4999972,4999971,4999970,4999969,4999968,4999967,4999966,4999965,4999964,4999963,4999962,4999961,4999960,4999959,4999958,4999957,4999956,4999955,4999954,4999953,4999952,4999951,4999950,4999949,4999948,4999947,4999946,4999945,4999944,4999943,4999942,4999941,4999940,4999939,4999938,4999937,4999936,4999935,4999934,4999933,4999932,4999931,4999930,4999929,4999928,4999927,4999926,4999925,4999924,4999923,4999922,4999921,4999920,4999919,4999918,4999917,4999916,4999915,4999914,4999913,4999912,4999911,4999910,4999909,4999908,4999907,4999906,4999905,4999904,4999903,4999902,4999901}; b3c7c622f4 2015-12-18 kinaba: vector <int> query(query_, query_+sizeof(query_)/sizeof(*query_)); b3c7c622f4 2015-12-18 kinaba: long long _ = -1LL; b3c7c622f4 2015-12-18 kinaba: END b3c7c622f4 2015-12-18 kinaba: /* b3c7c622f4 2015-12-18 kinaba: CASE(3) b3c7c622f4 2015-12-18 kinaba: int n = ; b3c7c622f4 2015-12-18 kinaba: int x0 = ; b3c7c622f4 2015-12-18 kinaba: int a = ; b3c7c622f4 2015-12-18 kinaba: int b = ; b3c7c622f4 2015-12-18 kinaba: int query_[] = ; b3c7c622f4 2015-12-18 kinaba: vector <int> query(query_, query_+sizeof(query_)/sizeof(*query_)); b3c7c622f4 2015-12-18 kinaba: long long _ = LL; b3c7c622f4 2015-12-18 kinaba: END b3c7c622f4 2015-12-18 kinaba: CASE(4) b3c7c622f4 2015-12-18 kinaba: int n = ; b3c7c622f4 2015-12-18 kinaba: int x0 = ; b3c7c622f4 2015-12-18 kinaba: int a = ; b3c7c622f4 2015-12-18 kinaba: int b = ; b3c7c622f4 2015-12-18 kinaba: int query_[] = ; b3c7c622f4 2015-12-18 kinaba: vector <int> query(query_, query_+sizeof(query_)/sizeof(*query_)); b3c7c622f4 2015-12-18 kinaba: long long _ = LL; b3c7c622f4 2015-12-18 kinaba: END b3c7c622f4 2015-12-18 kinaba: */ b3c7c622f4 2015-12-18 kinaba: } b3c7c622f4 2015-12-18 kinaba: // END CUT HERE