Submission #647566

# Submission time Handle Problem Language Result Execution time Memory
647566 2022-10-03T07:31:36 Z ghostwriter Selling RNA Strands (JOI16_selling_rna) C++14
0 / 100
360 ms 135976 KB
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#else
#define debug(...)
#endif
#define ft front
#define bk back
#define st first
#define nd second
#define ins insert
#define ers erase
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define bg begin
#define ed end
#define all(x) (x).bg(), (x).ed()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
#define FRN(i, n) for (int (i) = 0; (i) < (n); ++(i))
#define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i))
#define EACH(i, x) for (auto &(i) : (x))
#define WHILE while
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
----------------------------------------------------------------
    END OF TEMPLATE
----------------------------------------------------------------
    Tran The Bao - ghostwriter
    Training for VOI23 gold medal
----------------------------------------------------------------
    DIT ME CHUYEN BAO LOC
----------------------------------------------------------------
*/
const pi M = {1e9 + 7, 1e9 + 9};
const pi base = {37, 127};
struct Rna {
	str s;
	vpi h;
	Rna() {}
	void calh() {
		h.resize(sz(s));
		pi cur = {0, 0};
		int n = sz(s);
		FRN(i, n) {
			cur.st = (1LL * cur.st * base.st % M.st + s[i] - 'A' + 1) % M.st;
			cur.nd = (1LL * cur.nd * base.nd % M.nd + s[i] - 'A' + 1) % M.nd;
			h[i] = cur;
		}
	}
};
struct Query {
	str p, q;
	pi hq;
	vpi hp;
	Query() {}
	void calh() {
		hp.resize(sz(p));
		pi cur = {0, 0};
		int n = sz(p);
		FRN(i, n) {
			cur.st = (1LL * cur.st * base.st % M.st + p[i] - 'A' + 1) % M.st;
			cur.nd = (1LL * cur.nd * base.nd % M.nd + p[i] - 'A' + 1) % M.nd;
			hp[i] = cur;
		}
		hq = {0, 0};
		n = sz(q);
		FRN(i, n) {
			hq.st = (1LL * hq.st * base.st % M.st + q[i] - 'A' + 1) % M.st;
			hq.nd = (1LL * hq.nd * base.nd % M.nd + q[i] - 'A' + 1) % M.nd;
		}
	}
};
const int N = 1e5 + 1;
const int TLEN = 2e6 + 1;
int n, m;
vpi h[N];
Rna a[N];
Query q[N];
pi P[N];
vpi v;
vi spos[TLEN];
int getpos(pi &x) { if (x > v.bk()) return -1; int ans = lb(all(v), x) - v.bg(); if (v[ans] != x) return -1; return ans; }
int get(vi &a, int &l, int &r) {
	if (l > a.bk()) return 0;
	l = lb(all(a), l) - a.bg();
	if (r >= a.bk()) r = sz(a) - 1;
	else r = ub(all(a), r) - a.bg() - 1;
	return r - l + 1;
}
pi geth(vpi &h, int l, int r) {
	pi ans;
	ans.st = (h[r].st - 1LL * (l? h[l - 1].st : 0) * P[r - l + 1].st % M.st + M.st) % M.st;
	ans.nd = (h[r].nd - 1LL * (l? h[l - 1].nd : 0) * P[r - l + 1].nd % M.nd + M.nd) % M.nd;
	return ans;
}
int mmatch(vpi &h, vpi &h1) {
	int l = 0, r = min(sz(h), sz(h1)) - 1, ans = -1;
	WHILE(l <= r) {
		int mid = l + (r - l) / 2;
		pi f = geth(h, 0, mid), s = geth(h1, 0, mid);
		if (f == s) {
			ans = mid;
			l = mid + 1;
		}
		else r = mid - 1;
	}
	return ans + 1;
}
bool cmp(Rna &a, Rna &b) {
	int len = mmatch(a.h, b.h);
	if (len == sz(a.s) && len == sz(b.s)) return 0;
	if (len == sz(a.s)) return 1;
	if (len == sz(b.s)) return 0;
	return a.s[len] < b.s[len];
}
signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    // freopen(file".inp", "r", stdin);
    // freopen(file".out", "w", stdout);
    P[0] = {1, 1};
    FOR(i, 1, N - 1) {
    	P[i].st = 1LL * P[i - 1].st * base.st % M.st;
    	P[i].nd = 1LL * P[i - 1].nd * base.nd % M.nd;
    }
    cin >> n >> m;
    FOR(i, 1, n) {
    	cin >> a[i].s;
    	a[i].calh();
    	pi cur = {0, 0};
    	FSN(j, sz(a[i].s)) {
    		cur.st = (1LL * cur.st * base.st % M.st + a[i].s[j] - 'A' + 1) % M.st;
    		cur.nd = (1LL * cur.nd * base.nd % M.nd + a[i].s[j] - 'A' + 1) % M.nd;
    		v.pb(cur);
    		h[i].pb(cur);
    	}
    }
    FOR(i, 1, m) {
    	cin >> q[i].p >> q[i].q;
    	reverse(all(q[i].q));
    	q[i].calh();
    }
    sort(a + 1, a + 1 + n, cmp);
    sort(all(v));
 //   	FOR(i, 1, n)
	// EACH(j, h[i])
	// 	spos[getpos(j)].pb(i);
    // FOR(i, 1, m) {
    // 	Query &curq = q[i];
    // 	int pos = getpos(curq.hq);
    // 	if (pos == -1) {
    // 		cout << 0 << '\n';
    // 		continue;
    // 	}
    // 	int clen = sz(curq.p), l = 1, r = n, lans = -1, uans = -1;
    // 	WHILE(l <= r) {
    // 		int mid = l + (r - l) / 2;
    // 		int len = mmatch(a[mid].h, curq.hp);
    // 		if (len == clen) {
    // 			lans = mid;
    // 			r = mid - 1;
    // 		}
    // 		else if (a[mid].s[len] < curq.p[len]) l = mid + 1;
    // 		else r = mid - 1;
    // 	}
    // 	if (lans == -1) {
    // 		cout << 0 << '\n';
    // 		continue;
    // 	}
    // 	l = 1; r = n;
    // 	WHILE(l <= r) {
    // 		int mid = l + (r - l) / 2;
    // 		int len = mmatch(a[mid].h, curq.hp);
    // 		if (len == clen) {
    // 			uans = mid;
    // 			l = mid + 1;
    // 		}
    // 		else if (a[mid].s[len] < curq.p[len]) l = mid + 1;
    // 		else r = mid - 1;
    // 	}
    // 	cout << get(spos[pos], lans, uans) << '\n';
    // }
    // cerr << "\nTime: " << setprecision(5) << fixed << (ldb)clock() / CLOCKS_PER_SEC << "ms\n";
    return 0;
}
/*
2 3
AUGC
AGC
G C
AU C
A C
----------------------------------------------------------------
From Benq:
    stuff you should look for
        * int overflow, array bounds
        * special cases (n=1?)
        * do smth instead of nothing and stay organized
        * WRITE STUFF DOWN
        * DON'T GET STUCK ON ONE APPROACH
----------------------------------------------------------------
*/

Compilation message

selling_rna.cpp: In member function 'void Rna::calh()':
selling_rna.cpp:34:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   34 | #define FRN(i, n) for (int (i) = 0; (i) < (n); ++(i))
      |                            ^
selling_rna.cpp:61:3: note: in expansion of macro 'FRN'
   61 |   FRN(i, n) {
      |   ^~~
selling_rna.cpp: In member function 'void Query::calh()':
selling_rna.cpp:34:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   34 | #define FRN(i, n) for (int (i) = 0; (i) < (n); ++(i))
      |                            ^
selling_rna.cpp:77:3: note: in expansion of macro 'FRN'
   77 |   FRN(i, n) {
      |   ^~~
selling_rna.cpp:34:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   34 | #define FRN(i, n) for (int (i) = 0; (i) < (n); ++(i))
      |                            ^
selling_rna.cpp:84:3: note: in expansion of macro 'FRN'
   84 |   FRN(i, n) {
      |   ^~~
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
selling_rna.cpp:138:5: note: in expansion of macro 'FOR'
  138 |     FOR(i, 1, N - 1) {
      |     ^~~
selling_rna.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
selling_rna.cpp:143:5: note: in expansion of macro 'FOR'
  143 |     FOR(i, 1, n) {
      |     ^~~
selling_rna.cpp:35:28: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   35 | #define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i))
      |                            ^
selling_rna.cpp:147:6: note: in expansion of macro 'FSN'
  147 |      FSN(j, sz(a[i].s)) {
      |      ^~~
selling_rna.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
selling_rna.cpp:154:5: note: in expansion of macro 'FOR'
  154 |     FOR(i, 1, m) {
      |     ^~~
# Verdict Execution time Memory Grader output
1 Incorrect 32 ms 65276 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 360 ms 135976 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 86 ms 70720 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 32 ms 65276 KB Output isn't correct
2 Halted 0 ms 0 KB -