# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
442130 | SorahISA | Mutating DNA (IOI21_dna) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "dna.h"
#pragma GCC optimize("Ofast", "unroll-loops")
#include <bits/stdc++.h>
using namespace std;
// #define int long long
// #define double long double
using pii = pair<int, int>;
template<typename T>
using Prior = std::priority_queue<T>;
template<typename T>
using prior = std::priority_queue<T, vector<T>, greater<T>>;
#define X first
#define Y second
#define eb emplace_back
#define pb pop_back
#define pf pop_front
#define ALL(x) begin(x), end(x)
#define RALL(x) rbegin(x), rend(x)
namespace {
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 1 << 17;
int N;
string A, B;
vector<tuple<int, int, int>> cntA, cntB;
vector<array<array<int, 3>, 3>> _trans;
int to_int[256];
inline int is_same_dna(int L, int R) {
if (L == 0) return cntA[R] == cntB[R];
auto &[LAA, LAT, LAC] = cntA[L-1];
auto &[LBA, LBT, LBC] = cntB[L-1];
auto &[RAA, RAT, RAC] = cntA[R];
auto &[RBA, RBT, RBC] = cntB[R];
return (RAA - LAA == RBA - LBA) and
(RAT - LAT == RBT - LBT) and
(RAC - LAC == RBC - LBC);
}
array<array<int, 3>, 3> get_range_trans(int L, int R) {
if (L == 0) return _trans[R];
array<array<int, 3>, 3> tmp;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
tmp[i][j] = _trans[R][i][j] - _trans[L-1][i][j];
}
}
return tmp;
}
} /// end of namespace
void init(string _A, string _B) {
A = _A, B = _B, N = A.size();
cntA.assign(N, {0, 0, 0}), cntB.assign(N, {0, 0, 0});
_trans.resize(N);
to_int['A'] = 0, to_int['T'] = 1, to_int['C'] = 2;
for (int i = 0; i < N; ++i) {
if (i) cntA[i] = cntA[i-1], cntB[i] = cntB[i-1], trans[i] = trans[i-1];
auto &[AA, AT, AC] = cntA[i];
auto &[BA, BT, BC] = cntB[i];
if (A[i] == 'A') ++AA; if (A[i] == 'T') ++AT; if (A[i] == 'C') ++AC;
if (B[i] == 'A') ++BA; if (B[i] == 'T') ++BT; if (B[i] == 'C') ++BC;
++_trans[i][to_int[A[i]]][to_int[B[i]]];
}
}
int get_distance(int L, int R) {
if (!is_same_dna(L, R)) return -1;
auto trans = get_range_trans(L, R);
return trans[0][1]; /// 'A' or 'T'
}