Submission #390433

#TimeUsernameProblemLanguageResultExecution timeMemory
390433VROOM_VARUNSelling RNA Strands (JOI16_selling_rna)C++14
35 / 100
1596 ms229572 KiB
/* ID: varunra2 LANG: C++ TASK: rna */ #include <bits/stdc++.h> using namespace std; #ifdef DEBUG #include "lib/debug.h" #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #define debug_arr(...) \ cerr << "[" << #__VA_ARGS__ << "]:", debug_arr(__VA_ARGS__) #pragma GCC diagnostic ignored "-Wsign-compare" //#pragma GCC diagnostic ignored "-Wunused-parameter" //#pragma GCC diagnostic ignored "-Wunused-variable" #else #define debug(...) 42 #endif #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using ordered_set_less = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>; using ordered_set_equal = tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>; using ordered_map = tree<int, int, less<int>, rb_tree_tag, tree_order_statistics_node_update>; const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count(); struct chash { int operator()(int x) const { return x ^ RANDOM; } }; gp_hash_table<int, int, chash> table; mt19937 rng( (unsigned int)chrono::steady_clock::now().time_since_epoch().count()); #define EPS 1e-9 #define IN(A, B, C) assert(B <= A && A <= C) #define INF (int)1e9 #define MEM(a, b) memset(a, (b), sizeof(a)) #define MOD 1000000007 #define MP make_pair #define PB push_back #define all(cont) cont.begin(), cont.end() #define rall(cont) cont.end(), cont.begin() #define x first #define y second const double PI = acos(-1.0); typedef long long ll; typedef long double ld; typedef pair<int, int> PII; typedef map<int, int> MPII; typedef multiset<int> MSETI; typedef set<int> SETI; typedef set<string> SETS; typedef vector<int> VI; typedef vector<PII> VII; typedef vector<VI> VVI; typedef vector<string> VS; #define rep(i, a, b) for (int i = a; i < (b); ++i) #define trav(a, x) for (auto& a : x) #define sz(x) (int)(x).size() typedef pair<int, int> pii; typedef vector<int> vi; #pragma GCC diagnostic ignored "-Wsign-compare" // util functions const int md1 = 4441; const int md2 = 7817; VI hsh(string& s, int md) { VI ret(sz(s) + 1, 1); map<char, int> mp; mp['A'] = 1; mp['G'] = 2; mp['C'] = 3; mp['U'] = 4; trav(x, mp) x.y--; for (int i = 0; i < sz(s); i++) { ret[i + 1] = (1ll * ret[i] * 4 + mp[s[i]]) % MOD; } return ret; } VS vals; VS valsrev; vector<pair<VI, VI>> valshsh; vector<pair<VI, VI>> valshshrev; vector<pair<string, string>> qrys; vector<pair<pair<VI, VI>, pair<VI, VI>>> qryshsh; VVI finval; VVI finrevval; vector<pair<VI, VI>> finqry; VI ret; int n, m; void init() { vals.resize(n); valsrev.resize(n); valshshrev.resize(n); finrevval.resize(n); qrys.resize(m); valshsh.resize(n); qryshsh.resize(m); finval.resize(n); finqry.resize(m); ret.assign(m, 0); } map<PII, int> cmp; int siz = 0; void comp(VII& hshs) { sort(all(hshs)); hshs.resize(unique(all(hshs)) - hshs.begin()); for (int i = 0; i < sz(hshs); i++) { cmp[hshs[i]] = i; } if (siz == 0) siz = sz(hshs); } VVI adj; vector<gp_hash_table<int, int, chash>> merg; VVI qryind; void init2() { adj.resize(siz); merg.resize(siz); qryind.resize(siz); } void swp(int i, int j) { if (sz(merg[i]) < sz(merg[j])) { swap(merg[i], merg[j]); } for (auto& x : merg[j]) { merg[i][x.x] += x.y; } } void dfs(int u) { for (auto& x : adj[u]) { dfs(x); swp(u, x); } for (auto& x : qryind[u]) { ret[x] = merg[u][finqry[x].y.back()]; } } int main() { // #ifndef ONLINE_JUDGE // freopen("rna.in", "r", stdin); // freopen("rna.out", "w", stdout); // #endif cin.sync_with_stdio(0); cin.tie(0); cin >> n >> m; init(); for (int i = 0; i < n; i++) { cin >> vals[i]; valsrev[i] = vals[i]; reverse(all(valsrev[i])); valshsh[i].x = hsh(vals[i], md1); valshsh[i].y = hsh(vals[i], md2); valshshrev[i].x = hsh(valsrev[i], md1); valshshrev[i].y = hsh(valsrev[i], md2); } for (int i = 0; i < m; i++) { cin >> qrys[i].x >> qrys[i].y; reverse(all(qrys[i].y)); qryshsh[i].x.x = hsh(qrys[i].x, md1); qryshsh[i].x.y = hsh(qrys[i].x, md2); qryshsh[i].y.x = hsh(qrys[i].y, md1); qryshsh[i].y.y = hsh(qrys[i].y, md2); } VII hshs; // VII hshs1; for (int i = 0; i < n; i++) { for (int j = 0; j < sz(valshsh[i].x); j++) { hshs.PB(MP(valshsh[i].x[j], valshsh[i].y[j])); hshs.PB(MP(valshshrev[i].x[j], valshshrev[i].y[j])); } } for (int i = 0; i < m; i++) { for (int j = 0; j < sz(qryshsh[i].x.x); j++) { hshs.PB(MP(qryshsh[i].x.x[j], qryshsh[i].x.y[j])); } for (int j = 0; j < sz(qryshsh[i].y.x); j++) { hshs.PB(MP(qryshsh[i].y.x[j], qryshsh[i].y.y[j])); } } comp(hshs); for (int i = 0; i < n; i++) { for (int j = 0; j < sz(valshsh[i].x); j++) { finval[i].PB(cmp[MP(valshsh[i].x[j], valshsh[i].y[j])]); } } for (int i = 0; i < m; i++) { for (int j = 0; j < sz(qryshsh[i].x.x); j++) { finqry[i].x.PB(cmp[MP(qryshsh[i].x.x[j], qryshsh[i].x.y[j])]); } for (int j = 0; j < sz(qryshsh[i].y.x); j++) { finqry[i].y.PB(cmp[MP(qryshsh[i].y.x[j], qryshsh[i].y.y[j])]); } } // comp(hshs1); for (int i = 0; i < n; i++) { for (int j = 0; j < sz(valshshrev[i].x); j++) { finrevval[i].PB(cmp[MP(valshshrev[i].x[j], valshshrev[i].y[j])]); } } // hashing and compression done, make the tree and then do small to large init2(); // debug(finval); // debug(sz(adj)); for (int i = 0; i < n; i++) { for (int j = 0; j < sz(finval[i]) - 1; j++) { adj[finval[i][j]].PB(finval[i][j + 1]); } } // debug(adj); // debug("asdfasdfsadfasdfas"); for (int i = 0; i < siz; i++) { sort(all(adj[i])); adj[i].resize(unique(all(adj[i])) - adj[i].begin()); } // made the tree, let's do the leaves for (int i = 0; i < n; i++) { for (int j = 0; j < sz(finrevval[i]); j++) { merg[finval[i].back()][finrevval[i][j]]++; } } // now let's preprocess queries for (int i = 0; i < m; i++) { qryind[finqry[i].x.back()].PB(i); } // debug("adj"); // trav(x, adj) debug(x); // debug("finval"); // trav(x, finval) debug(x); // debug("finrevval"); // trav(x, finrevval) { debug(x); } // // debug("finqry"); // trav(x, finqry) debug(x.x.back(), x.y.back()); dfs(0); for (int i = 0; i < m; i++) { cout << ret[i] << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...