# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
719177 | thimote75 | Mutating DNA (IOI21_dna) | C++17 | 75 ms | 16620 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"
#include <bits/stdc++.h>
using namespace std;
int to_int(char ch) {
if (ch == 'A') return 0;
if (ch == 'C') return 1;
if (ch == 'T') return 2;
assert(false);
}
struct SGD {
int m[3][3];
void init () {
for (int i = 0; i < 3; i ++)
for (int j = 0; j < 3; j ++)
m[i][j] = 0;
}
SGD () { init(); }
SGD (char a, char b) {
init();
int ia = to_int(a);
int ib = to_int(b);
m[ia][ib] ++;
}
SGD merge (SGD &other) {
SGD res;
for (int i = 0; i < 3; i ++)
for (int j = 0; j < 3; j ++)
res.m[i][j] = m[i][j] + other.m[i][j];
return res;
}
int flux (int i) {
int f = 0;
for (int j = 0; j < 3; j ++) {
if (j == i) continue ;
f -= m[i][j];
f += m[j][i];
}
return f;
}
int distance () {
if (flux(0) != 0
|| flux(1) != 0
|| flux(2) != 0)
return -1;
int r[3][3];
for (int i = 0; i < 3; i ++) for (int j = 0; j < 3; j ++)
r[i][j] = m[i][j];
int dist = 0;
for (int i = 0; i < 3; i ++) {
for (int j = i + 1; j < 3; j ++) {
dist += min(r[j][i], r[i][j]);
r[i][j] -= r[j][i];
r[j][i] = 0;
}
}
int residual = abs(r[0][2]);
if (residual != abs(r[0][1])) return -1;
if (residual != abs(r[1][2])) return -1;
dist += 2 * residual;
return dist;
}
};
struct SegTree {
int size, start, height;
vector<SGD> tree;
SegTree () = default;
SegTree (vector<SGD> &values) {
size = values.size();
height = ceil(log2(size));
start = 1 << height;
tree.resize(start << 1);
for (int i = 0; i < size; i ++)
tree[i + start] = values[i];
for (int i = start - 1; i >= 1; i --) {
int n0 = i << 1;
int n1 = n0 + 1;
tree[i] = tree[n0].merge(tree[n1]);
}
}
SGD query (int x, int y) {
x += start;
y += start;
SGD res;
while (x < y) {
if ((x & 1) == 1) res = res.merge(tree[x]);
if ((y & 1) == 0) res = res.merge(tree[y]);
x = (x + 1) >> 1;
y = (y - 1) >> 1;
}
if (x == y) res = res.merge(tree[x]);
return res;
}
};
vector<SGD> object_array;
SegTree tree;
void init(string a, string b) {
object_array.resize(a.size());
for (int i = 0; i < a.size(); i ++)
object_array[i] = SGD(a[i], b[i]);
tree = SegTree(object_array);
}
int get_distance(int x, int y) {
SGD res = tree.query(x, y);
return res.distance();
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |