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 <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define TranLeThanhTam "TranLeThanhTam"
#define all(v) v.begin(),v.end()
#define reset(a,i) memset(a,i,sizeof(a))
#define FOR(i,a,b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i,a,b) for (int i = (a), _b = (b); i >= _b; --i)
#define compact(v) sort(all(v)), v.erase(unique(all(v)), end(v))
#define sz(v) (int) v.size()
#define MASK(i) (1LL << (i)
#define BIT(i,x) ((x & MASK(i)) >> (i))
typedef long long ll;
typedef long double lb;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
const int Thanh_Tam = 1e5 + 17;
const int inf = 1e9 + 17;
const long long infll = 1e18 + 17;
const int MOD = 1e9 + 7;
const int Log = 18;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
const int max_block_size = 270;
const ll P = 311;
const int maxA = 1e2 + 17;
const int addi = 1e9;
const char dir[4] = {'W', 'S', 'N', 'E'};
const int Mod[3] = {998244353, MOD, 759186269};
ll mul(ll a, ll b, ll mod);
ll binpow(ll a, ll b, ll mod)
{
ll res = 1;
while (b > 0)
{
if (b & 1) res = mul(res,a,mod);
b >>= 1;
a = mul(a,a,mod);
}
return res;
}
ll add(ll a, ll b, ll mod) { return (a + b) % mod; }
ll sub(ll a, ll b, ll mod) { return (a - b + mod) % mod; }
ll mul(ll a, ll b, ll mod) { return ((a % mod) * (b % mod)) % mod; }
ll div(ll a, ll b, ll mod) { return mul(a, binpow(b, mod - 2, mod), mod); }
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rand_range(ll l, ll r) { return uniform_int_distribution<ll>(l,r)(rng); }
struct iii
{
int x,y,z;
};
int n,q;
string a,b;
int pref_valid[Thanh_Tam][3][2], pref[Thanh_Tam][6];
iii per[2] = {{0, 4, 3}, {2, 5, 1}};
void init(string a, string b)
{
a = '.' + a;
b = '.' + b;
//Preparation
FOR(i,1,n)
{
if (a[i] == 'T') a[i] = 'B';
if (b[i] == 'T') b[i] = 'B';
}
FOR(i,1,n)
FOR(j,0,2)
{
pref_valid[i][j][0] = pref_valid[i - 1][j][0] + (a[i] == char ('A' + j));
pref_valid[i][j][1] = pref_valid[i - 1][j][1] + (b[i] == char ('A' + j));
}
FOR(i,1,n)
{
pref[i][0] = pref[i - 1][0] + (a[i] == 'A' && b[i] == 'B');
pref[i][1] = pref[i - 1][1] + (a[i] == 'B' && b[i] == 'A');
pref[i][2] = pref[i - 1][2] + (a[i] == 'A' && b[i] == 'C');
pref[i][3] = pref[i - 1][3] + (a[i] == 'C' && b[i] == 'A');
pref[i][4] = pref[i - 1][4] + (a[i] == 'B' && b[i] == 'C');
pref[i][5] = pref[i - 1][5] + (a[i] == 'C' && b[i] == 'B');
}
}
bool Valid(int x, int y)
{
int A[3], B[3];
FOR(j,0,2)
{
A[j] = pref_valid[y][j][0] - pref_valid[x - 1][j][0];
B[j] = pref_valid[y][j][1] - pref_valid[x - 1][j][1];
}
return (A[0] == B[0] && A[1] == B[1] && A[2] == B[2]);
}
int get_distance(int x, int y)
{
++x, ++y;
if (!Valid(x,y)) return -1;
int val[6];
FOR(i,0,5) val[i] = pref[y][i] - pref[x - 1][i];
int ans = 0;
FOR(i,0,2)
{
int tmp = min(val[i * 2], val[i * 2 + 1]);
val[i * 2] -= tmp;
val[i * 2 + 1] -= tmp;
ans += tmp;
}
FOR(i,0,1)
{
int tmp = min({val[per[i].x], val[per[i].y], val[per[i].z]});
val[per[i].x] -= tmp;
val[per[i].y] -= tmp;
val[per[i].z] -= tmp;
ans += (tmp << 1);
}
FOR(i,0,5)
if (val[i])
return -1;
return ans;
}
/*int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
if (fopen(TranLeThanhTam".inp","r"))
{
freopen(TranLeThanhTam".inp","r",stdin);
freopen(TranLeThanhTam".out","w",stdout);
}
cin >> n >> q >> a >> b;
init(a,b);
//Processing
while (q--)
{
int l,r;
cin >> l >> r;
cout << get_distance(l,r) << '\n';
}
return 0;
}*/
# | 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... |