# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
476408 | Mahmudul_Kabir | 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"
#include "bits/stdc++.h"
#define sp <<" "
#define el <<"\n"
#define S second
#define F first
#define pb push_back
#define all(ar) ar.begin(),ar.end()
#define pii pair<int,int>
using namespace std;
using ll = long long;
using ld = long double;
const ll mod = 100000007;
const ll si = 100005;
const ll inf = 7000;
ll po[si][3], pt[si][3],n;
unordered_map<char,ll> hau = {{'A',0},{'T',1},{'C',2}};
ll str[si * 4];
void make(ll x,ll y,ll node,string &a,string &b){
if(x == y){
str[node] = a[x] == b[x];
return;
}
ll mid = x + (y - x)/2, lef = node * 2;
make(x,mid,lef,a,b); make(mid+1,y,lef + 1,a,b);
str[node] = str[lef] + str[lef+1];
return;
}
ll que(ll a,ll b,ll l,ll r,ll node){
if(b < l || r < a) return 0;
if(a >= l && r >= b) return str[node];
ll mid = a + (b - a)/2, lef = node * 2;
return que(a,mid,l,r,lef) + que(mid+1,b,l,r,lef+1);
}
void init(std::string a, std::string b){
n = a.size();
memset(po,0,sizeof(po)); memset(pt,0,sizeof(pt));
po[0][hau[a[0]]] += 1; pt[0][hau[b[0]]] += 1;
for(ll i = 1; i < n; i++){
for(ll j = 0; j < 3; j++){
po[i][j] = po[i - 1][j];
pt[i][j] = pt[i - 1][j];
}
po[i][hau[a[i]]] += 1;
pt[i][hau[b[i]]] += 1;
}
memset(str,0,sizeof(str));
make(0,n-1,1,a,b);
return;
}
ll get(ll x,ll y,ll i,bool t){
ll ans = t ? pt[y][i] : po[y][i];
if(x) ans -= t ? pt[x - 1][i] : po[x - 1][i];
return ans;
}
bool hobe(ll x,ll y){
//cout<<get(x,y,0,0) sp<< get(x,y,0,1) sp<<get(x,y,1,0) sp<< get(x,y,1,1) sp<<get(x,y,2,0) sp<< get(x,y,2,1) el;
return (get(x,y,0,0) == get(x,y,0,1)) && (get(x,y,1,0) == get(x,y,1,1)) && (get(x,y,2,0) == get(x,y,2,1));
}
int get_distance(int x, int y) {
if(!hobe(x,y)) return -1;
return (y - x - que(0,n - 1,x,y,1) + 2)/2;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//*
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
//*/
ll n,q; cin>>n>>q; string a,b; cin>>a>>b;
init(a,b);
while(q--){
ll x,y; cin>>x>>y;
cout<<get_distance(x,y) el;
}
return 0;
}