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;
template <typename T>
struct fenwick_tree
{
vector<T> t;
fenwick_tree() {}
fenwick_tree(size_t n) { t.resize(n); }
void update(int64_t i, T x)
{
++i;
while (i <= t.size())
t[i - 1] += x, i += i & -i;
}
T range_sum(int64_t i, int64_t j)
{
T x = 0;
++j;
while (j)
x += t[j - 1], j -= j & -j;
while (i)
x -= t[i - 1], i -= i & -i;
return x;
}
};
constexpr size_t N = 100000;
vector<uint8_t> a, b;
fenwick_tree<int> c[3][3];
uint8_t conv(char c) { return c == 'A' ? 0 : (c == 'C' ? 1 : 2); }
void init(string a_, string b_)
{
size_t n = a_.size();
a.resize(n);
b.resize(n);
for (size_t i = 0; i < n; ++i)
a[i] = conv(a_[i]), b[i] = conv(b_[i]);
for (size_t i = 0; i < 3; ++i)
for (size_t j = 0; j < 3; ++j)
c[i][j] = fenwick_tree<int>(n);
for (size_t i = 0; i < n; ++i)
c[a[i]][b[i]].update(i, 1);
}
bool equal_set_in_range(size_t i, size_t j)
{
return c[0][0].range_sum(i, j) + c[0][1].range_sum(i, j) + c[0][2].range_sum(i, j) ==
c[0][0].range_sum(i, j) + c[1][0].range_sum(i, j) + c[2][0].range_sum(i, j) &&
c[1][0].range_sum(i, j) + c[1][1].range_sum(i, j) + c[1][2].range_sum(i, j) ==
c[0][1].range_sum(i, j) + c[1][1].range_sum(i, j) + c[2][1].range_sum(i, j) &&
c[2][0].range_sum(i, j) + c[2][1].range_sum(i, j) + c[2][2].range_sum(i, j) ==
c[0][2].range_sum(i, j) + c[1][2].range_sum(i, j) + c[2][2].range_sum(i, j);
}
int get_distance(int x, int y)
{
if (!equal_set_in_range(x, y))
return -1;
int ans = 0, num_matched = 0;
num_matched += c[0][0].range_sum(x, y) + c[1][1].range_sum(x, y) + c[2][2].range_sum(x, y);
for (size_t i = 0; i < 3; ++i)
for (size_t j = i + 1; j < 3; ++j)
{
int count = min(c[i][j].range_sum(x, y), c[j][i].range_sum(x, y));
ans += count;
num_matched += count << 1;
}
return ans + ((y - x + 1 - num_matched) / 3) * 2;
}
Compilation message (stderr)
dna.cpp: In instantiation of 'void fenwick_tree<T>::update(int64_t, T) [with T = int; int64_t = long int]':
dna.cpp:53:34: required from here
dna.cpp:17:18: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | while (i <= t.size())
| ~~^~~~~~~~~~~
# | 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... |