Artifact c0de4a8f67dfbf764f5cde71cf9d264e41a3c74e
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <memory>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef complex<LD> CMP;
template<typename T=LL>
struct FenwickTree
{
vector<T> x;
FenwickTree(size_t n) : x(n) {}
void incr( int k, const T& a ) { // z[k] += a;
for(; k < x.size(); k|=k+1)
x[k] += a;
}
T sum(int i, int j) { // Sigma z[i,j) excl.
return sum_impl(j-1) - sum_impl(i-1);
}
T sum_impl(int j) { // Sigma z[0,j] incl.
T v = T();
for(; j>=0; j=(j&(j+1))-1)
v += x[j];
return v;
}
};
template<typename T>
std::vector<int> compress(std::vector<T>& xs)
{
std::vector< pair<T,int> > xi;
for(int i=0; i<xs.size(); ++i)
xi.push_back( make_pair(xs[i], i) );
sort(xi.begin(), xi.end());
std::vector<int> result(xs.size());
for(int k=0; k<xi.size(); ++k)
result[xi[k].second] = k;
return result;
}
class ThreePoints { public:
long long countColoring(int N,
int xzero, int xmul, int xadd, int xmod,
int yzero, int ymul, int yadd, int ymod)
{
vector<int> x(N), y(N);
x[0] = xzero; for(int i=1; i<N; ++i) x[i] = (x[i-1] * LL(xmul) + xadd) % xmod;
y[0] = yzero; for(int i=1; i<N; ++i) y[i] = (y[i-1] * LL(ymul) + yadd) % ymod;
x = compress(x);
y = compress(y);
vector< pair<int,int> > y_desc;
for(int i=0; i<N; ++i)
y_desc.push_back(make_pair(y[i], x[i]));
sort(y_desc.rbegin(), y_desc.rend());
LL result = 0;
FenwickTree<> p(N), sp(N);
for(int i=0; i<N; ++i) {
const int x = y_desc[i].second;
LL sp_ = p.sum(x+1, N);
LL ssp_ = sp.sum(x+1, N);
p.incr(x, 1);
sp.incr(x, sp_);
result += sp_*(sp_-1)/2 - ssp_;
}
return result;
}
};
// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
{ ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
{ os << "{ ";
for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const long long& Expected, const long long& Received) {
bool ok = (Expected == Received);
if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl;
cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END verify_case(_, ThreePoints().countColoring(N, xzero, xmul, xadd, xmod, yzero, ymul, yadd, ymod));}
int main(){
CASE(0)
int N = 9;
int xzero = 3;
int xmul = 8;
int xadd = 6;
int xmod = 11;
int yzero = 5;
int ymul = 7;
int yadd = 8;
int ymod = 11;
long long _ = 8LL;
END
CASE(1)
int N = 4;
int xzero = 9;
int xmul = 6;
int xadd = 8;
int xmod = 10;
int yzero = 4;
int ymul = 8;
int yadd = 5;
int ymod = 10;
long long _ = 2LL;
END
CASE(2)
int N = 20;
int xzero = 30;
int xmul = 3;
int xadd = 71;
int xmod = 100;
int yzero = 78;
int ymul = 12;
int yadd = 50;
int ymod = 100;
long long _ = 263LL;
END
CASE(3)
int N = 300000;
int xzero = 99097861;
int xmul = 102766912;
int xadd = 95284952;
int xmod = 123456789;
int yzero = 443104491;
int ymul = 971853214;
int yadd = 569775557;
int ymod = 987654321;
long long _ = 749410681185726LL;
END
/*
CASE(4)
int N = ;
int xzero = ;
int xmul = ;
int xadd = ;
int xmod = ;
int yzero = ;
int ymul = ;
int yadd = ;
int ymod = ;
long long _ = LL;
END
CASE(5)
int N = ;
int xzero = ;
int xmul = ;
int xadd = ;
int xmod = ;
int yzero = ;
int ymul = ;
int yadd = ;
int ymod = ;
long long _ = LL;
END
*/
}
// END CUT HERE