Submission #1312985

#TimeUsernameProblemLanguageResultExecution timeMemory
1312985Zbyszek99Selling RNA Strands (JOI16_selling_rna)C++20
100 / 100
852 ms225112 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define ll long long #define ld long double #define ull unsigned long long #define ff first #define ss second #define pii pair<int,int> #define pll pair<long long, long long> #define vi vector<int> #define vl vector<long long> #define pb push_back #define rep(i, b) for(int i = 0; i < (b); ++i) #define rep2(i,a,b) for(int i = a; i <= (b); ++i) #define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c) #define count_bits(x) __builtin_popcountll((x)) #define all(x) (x).begin(),(x).end() #define siz(x) (int)(x).size() #define forall(it,x) for(auto& it:(x)) using namespace __gnu_pbds; using namespace std; typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set; //mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());} //ll los(ll a, ll b) {return a + (mt() % (b-a+1));} const int INF = 1e9+50; const ll INF_L = 1e18+40; const ll MOD = 1e9+7; ll p[2] = {2137,692137}; ll P[2][100001]; unordered_map<ll,int> ans_map; vi ans_map2[2000001]; pair<string,string> queries[100001]; vl suf_hashes[100001]; int query_ans[100001]; ll hash_string(string& s) { ll h[2]; h[0] = 0; h[1] = 0; rep(i,siz(s)) rep(d,2) h[d] = (h[d]+P[d][i]*s[i])%MOD; return h[0]+h[1]*MOD; } int main() { ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); //random_start(); rep(d,2) { P[d][0] = 1; rep2(i,1,100000) P[d][i] = (P[d][i-1]*p[d])%MOD; } int n,q; cin >> n >> q; vector<pair<string,int>> vsort; rep(i,n) { string s; cin >> s; reverse(all(s)); vsort.pb({s,1e9+i}); ll h[2]; h[0] = 0; h[1] = 0; rep(j,siz(s)) { rep(d,2) h[d] = (h[d]+P[d][j]*s[j])%MOD; suf_hashes[i].pb(h[0]+h[1]*MOD); } } rep(qq,q) { string s1,s2; cin >> s1 >> s2; reverse(all(s2)); queries[qq] = {s1,s2}; vsort.pb({s2,qq}); } sort(all(vsort)); reverse(all(vsort)); int cur = 1; forall(it,vsort) { if(it.ss >= 1e9) { it.ss -= 1e9; reverse(all(it.ff)); ll h[2]; h[0] = 0; h[1] = 0; rep(i,siz(it.ff)) { rep(d,2) h[d] = (h[d]+P[d][i]*it.ff[i])%MOD; if(ans_map.find(h[0]+h[1]*MOD)==ans_map.end()) ans_map[h[0]+h[1]*MOD] = cur++; ans_map2[ans_map[h[0]+h[1]*MOD]].pb(it.ss); } } else { ll hash = hash_string(queries[it.ss].ff); ll hash2 = hash_string(it.ff); int p = ans_map[hash]; int l = 0; int r = siz(ans_map2[p])-1; int ans = siz(ans_map2[p]); while(l <= r) { int mid = (l+r)/2; if(siz(it.ff) <= siz(suf_hashes[ans_map2[p][mid]]) && hash2 == suf_hashes[ans_map2[p][mid]][siz(it.ff)-1]) { ans = mid; r = mid-1; } else { l = mid+1; } } query_ans[it.ss] = siz(ans_map2[p])-ans; } } rep(qq,q) cout << query_ans[qq] << "\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...