Artifact Content
Not logged in

Artifact 7cd5022ef19f47d8506a15b8c3df714a2050217e


#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>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef complex<double> CMP;

template<typename T>
vector<int> compress(const vector<T>& xs, int origin = 0)
{
	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());

	vector<int> result(xs.size());
	for(int k=0,id=origin; k<xi.size(); ++k) {
		if(0<=k-1 && xi[k-1].first<xi[k].first)
			++id;
		result[xi[k].second] = id;
	}
	return result;
}

std::vector<int> compress(const std::vector<int>& xs)  // convert to 1-origin rank
{
	set<int> s(xs.begin(), xs.end());
	map<int,int> mp;
	int ID = 1;
	for(set<int>::iterator it=s.begin(); it!=s.end(); ++it)
		mp[*it] = ID++;

	std::vector<int> result(xs.size());
	for(int k=0; k<xs.size(); ++k)
		result[k] = mp[xs[k]];
	return result;
}

LL C(LL n, LL k)
{
	if(n<k)
		return 0;

	k = min(k, n-k);

	LL c = 1;
	for(LL i=0; i<k; ++i)
		c *= n-i, c /= i+1;
	return c;
}

class Excavations { public:
	long long count(vector <int> kind, vector <int> depth, vector <int> found, int K)
	{
		depth = compress(depth, 1);
		int MD = *max_element(depth.begin(), depth.end());

		map< int, vector<int> > kind_to_depth;
		set<int> found_set(found.begin(), found.end());

		for(int i=0; i<kind.size(); ++i)
			kind_to_depth[kind[i]].push_back(depth[i]);

		vector< vector<LL> > tbl_f(K+1, vector<LL>(MD+1));
		vector< vector<LL> > tbl_nf(K+1, vector<LL>(MD+1));
		for(int d=0; d<=MD; ++d)
			tbl_f[0][d] = tbl_nf[0][d] = 1;

		for(map<int,vector<int> >::iterator it=kind_to_depth.begin(); it != kind_to_depth.end(); ++it) {
			bool f = !!found_set.count(it->first);
			vector<int> ds = it->second;
			sort(ds.begin(), ds.end());
			vector< vector<LL> > tbl2 = calc_table(K, MD, ds, f);
			vector< vector<LL> >& tbl = (f ? tbl_f : tbl_nf);
			tbl = combine(K, MD, tbl, tbl2);
		}

		LL total = 0;
		for(int k=0; k<=K; ++k)
			total += cross(tbl_f[k], tbl_nf[K-k]);
		return total;
	}

	LL cross(const vector<LL>& f, const vector<LL>& n)
	{
		vector< pair<int,LL> > pf;
		for(int d=0; d<f.size(); ++d) {
			LL cnt = (d==0 ? f[d] : f[d]-f[d-1]);
			if(cnt) {
				pf.push_back(make_pair(d, cnt));
			}
		}

		vector< pair<int,LL> > nf;
		for(int d=0; d<n.size(); ++d) {
			LL cnt = (d==n.size()-1 ? n[d] : n[d]-n[d+1]);
			if(cnt) {
				nf.push_back(make_pair(d, cnt));
			}
		}

		LL total = 0;
		for(int i=0; i<pf.size(); ++i)
		for(int k=0; k<nf.size(); ++k)
		{
			if(pf[i].first <= nf[k].first)
				total += pf[i].second * nf[k].second;
		}
		return total;
	}

	vector< vector<LL> > calc_table(int K, int D, const vector<int>& ds, bool found)
	{
		vector< vector<LL> > tbl(K+1, vector<LL>(D+1));
		for(int d=0; d<=D; ++d)
		{
			int num_discover = upper_bound(ds.begin(), ds.end(), d) - ds.begin();
			for(int k=0; k<=K; ++k)
				tbl[k][d] = (found ? (k==0 ? 0 : C(num_discover, k)) : C(ds.size()-num_discover, k));
		}
		return tbl;
	}

	vector< vector<LL> > combine(int K, int D, const vector<vector<LL> >& s, const vector<vector<LL> >& t)
	{
		vector< vector<LL> > r(K+1, vector<LL>(D+1));
		for(int ks=0; ks<=K; ++ks)
		for(int kt=0; kt<=K; ++kt) if(ks+kt<=K)
			for(int d=0; d<=D; ++d)
				r[ks+kt][d] += s[ks][d] * t[kt][d];
		return r;
	}
};

// 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(_, Excavations().count(kind, depth, found, K));}
int main(){

CASE(0)
	int kind_[] = {1, 1, 2, 2};
	  vector <int> kind(kind_, kind_+sizeof(kind_)/sizeof(*kind_)); 
	int depth_[] = {10, 15, 10, 20};
	  vector <int> depth(depth_, depth_+sizeof(depth_)/sizeof(*depth_)); 
	int found_[] = {1};
	  vector <int> found(found_, found_+sizeof(found_)/sizeof(*found_)); 
	int K = 2; 
	long long _ = 3LL; 
END
CASE(1)
	int kind_[] = {1, 1, 2, 2};
	  vector <int> kind(kind_, kind_+sizeof(kind_)/sizeof(*kind_)); 
	int depth_[] = {10, 15, 10, 20};
	  vector <int> depth(depth_, depth_+sizeof(depth_)/sizeof(*depth_)); 
	int found_[] = {1, 2};
	  vector <int> found(found_, found_+sizeof(found_)/sizeof(*found_)); 
	int K = 2; 
	long long _ = 4LL; 
END
CASE(2)
	int kind_[] = {1, 2, 3, 4};
	  vector <int> kind(kind_, kind_+sizeof(kind_)/sizeof(*kind_)); 
	int depth_[] = {10, 10, 10, 10};
	  vector <int> depth(depth_, depth_+sizeof(depth_)/sizeof(*depth_)); 
	int found_[] = {1, 2};
	  vector <int> found(found_, found_+sizeof(found_)/sizeof(*found_)); 
	int K = 3; 
	long long _ = 0LL; 
END
CASE(3)
	int kind_[] = {1, 2, 2, 3, 1, 3, 2, 1, 2};
	  vector <int> kind(kind_, kind_+sizeof(kind_)/sizeof(*kind_)); 
	int depth_[] = {12512, 12859, 125, 1000, 99, 114, 125, 125, 114};
	  vector <int> depth(depth_, depth_+sizeof(depth_)/sizeof(*depth_)); 
	int found_[] = {1, 2, 3};
	  vector <int> found(found_, found_+sizeof(found_)/sizeof(*found_)); 
	int K = 7; 
	long long _ = 35LL; 
END
CASE(4)
	int kind_[] = {50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50};
	  vector <int> kind(kind_, kind_+sizeof(kind_)/sizeof(*kind_)); 
	int depth_[] = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3};
	  vector <int> depth(depth_, depth_+sizeof(depth_)/sizeof(*depth_)); 
	int found_[] = {50};
	  vector <int> found(found_, found_+sizeof(found_)/sizeof(*found_)); 
	int K = 18; 
	long long _ = 9075135300LL; 
END
/*
CASE(5)
	int kind_[] = ;
	  vector <int> kind(kind_, kind_+sizeof(kind_)/sizeof(*kind_)); 
	int depth_[] = ;
	  vector <int> depth(depth_, depth_+sizeof(depth_)/sizeof(*depth_)); 
	int found_[] = ;
	  vector <int> found(found_, found_+sizeof(found_)/sizeof(*found_)); 
	int K = ; 
	long long _ = LL; 
END
CASE(6)
	int kind_[] = ;
	  vector <int> kind(kind_, kind_+sizeof(kind_)/sizeof(*kind_)); 
	int depth_[] = ;
	  vector <int> depth(depth_, depth_+sizeof(depth_)/sizeof(*depth_)); 
	int found_[] = ;
	  vector <int> found(found_, found_+sizeof(found_)/sizeof(*found_)); 
	int K = ; 
	long long _ = LL; 
END
	*/
}
// END CUT HERE