# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1091872 | agrim_09 | 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 <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define endl '\n';
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define py cout << "YES" << endl;
#define pn cout << "NO" << endl;
#define pb push_back
#define int long long
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
const ll inf = 1e18;
const ll mod = 1e9+7;
const int N = 2e5;
vector<int>primes = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};
// Check for queue, priorioty_queue, stack, ordered_set solutions
// stack => LIFO (whatever goes in last comes out last)
// queue => FIFO (whatever goes in first comes out first)
// priority_queue => Dynamic queries of minimum/maximum
// ordered_set => set in vector form
//[order_of_key(k) gives number of elements less than k, while find_by_order(i) gives i^th element]
// To comment multiple lines : ctrl + /
// To find and replace : ctrl+H
void judge(){
srand(time(NULL));
#ifndef ONLINE_JUDGE
freopen("inputA.txt","r",stdin);
freopen("outputA.txt","w",stdout);
#endif
}
void usaco(string s) {
freopen((s + ".in").c_str(),"r",stdin);
freopen((s + ".out").c_str(),"w",stdout);
}
int val(char a, char b){
if(a==b){
return -1;
}
if(a=='1'){
return (b-'2');
}
if(a=='2'){
if(b=='1'){
return 2;
}
return 3;
}
if(a=='3'){
if(b=='1'){
return 4;
}
return 5;
}
return -1;
}
signed main(){
fastio; //judge();
int n,q; cin >> n >> q;
string s1,s2; cin >> s1 >> s2;
int cnt[n][6];
for(int i = 0;i<n;i++){
for(int j = 0;j<6;j++){
cnt[i][j] = 0;
}
}
int tmp = val(s1[0],s2[0]);
if(tmp!=-1){
cnt[0][val(s1[0],s2[0])] = 1;
}
for(int i = 1;i<n;i++){
for(int j = 0;j<6;j++){
cnt[i][j] = cnt[i-1][j];
}
int tmp = val(s1[i],s2[i]);
if(tmp!=-1){
cnt[i][val(s1[i],s2[i])]++;
}
}
while(q--){
int l,r; cin >> l >> r;
int x1 = cnt[r][0], x2 = cnt[r][1], y1 = cnt[r][2], y2 = cnt[r][3];
int z1 = cnt[r][4], z2 = cnt[r][5];
if(l>0){
x1 -= cnt[l-1][0], x2 -= cnt[l-1][1], y1 -= cnt[l-1][2], y2 -= cnt[l-1][3];
z1 -= cnt[l-1][4], z2 -= cnt[l-1][5];
}
int ans = min(x1,y1) + min(x2,z1) + min(y2,z2);
set<int>front; set<int>end;
int r1 = abs(x1-y1), r2 =abs(x2-z1), r3 = abs(y2-z2);
if(x1>y1){
front.insert(1); end.insert(2);
}
if(y1>x1){
front.insert(2); end.insert(1);
}
if(x2>z1){
front.insert(1); end.insert(3);
}
if(z1>x2){
front.insert(3); end.insert(1);
}
if(y2>z2){
front.insert(2); end.insert(3);
}
if(z2>y2){
front.insert(3); end.insert(2);
}
bool yes = (front==end and r1==r2 and r2==r3);
if(yes){
cout << ans + 2*r1 << endl;
}
else{
cout << -1 << endl;
}
}
}