Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.7 kB
2
Indexable
Never
#include <bits/stdc++.h>
using namespace std;

#define int long long
void __print(int x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned int x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \
  cerr << "[" << #x << "] = [";                                                \
  _print(x)
#else
#define debug(x...)
#endif

void solve() {
  int n,q;
  cin >> n >> q;
  vector<map <int,int>> v_m1(n),v_m2(n);
  for(int i=0;i<n;i++){
    for(int j=1;j<=26;j++){
      v_m1[i][j]=0;
      v_m2[i][j]=0;
    }
  }
  string a,b;
  cin >> a >> b;
  for(int i=0;i<n;i++){
    if(i==0){
      v_m1[0][a[0]-'a'+1]++;
      v_m2[0][b[0]-'a'+1]++;
      continue;
    }
    for(int j=1;j<=26;j++){
      v_m1[i][j]=v_m1[i-1][j];
      v_m2[i][j]=v_m2[i-1][j];
    }
    v_m1[i][a[i]-'a'+1]=v_m1[i-1][a[i]-'a'+1]+1;
    v_m2[i][b[i]-'a'+1]=v_m2[i-1][b[i]-'a'+1]+1;
  }
  // debug(v_m1);
  // debug(v_m2);

  while(q--){
    // debug(q);
    int l,r;
    cin >> l >> r;
    l--,r--;
    debug(l,r);
    vector<int> let_in_a;
    vector<int> let_in_b;
    if(l==0){
      for(int i=1;i<=26;i++){
        // debug(r,i,v_m1[r][i]);
        let_in_a.push_back(v_m1[r][i]);
        let_in_b.push_back(v_m2[r][i]);
      }
    }
    else{
      for(int i=1;i<=26;i++){
        let_in_a.push_back(v_m1[r][i]-v_m1[l-1][i]);
        let_in_b.push_back(v_m2[r][i]-v_m2[l-1][i]);
      }
    }
    // debug(let_in_a);
    // debug(let_in_b);
    int ans=0;
    for(int i=0;i<26;i++){
      if(let_in_a[i]>let_in_b[i]){
        ans += let_in_a[i]-let_in_b[i];
      }
    }
    cout << ans << endl;

  }

}

signed main() {
  int t = 1;
  cin >> t;
  while (t--) {
    // debug(t);
    solve();
  }

  return 0;
}
Leave a Comment