1 条题解

  • 1
    @ 2024-11-12 21:25:54
    #include <bits/stdc++.h>
    using namespace std;
    #define ls u << 1
    #define rs u << 1 | 1
    #define LL long long
    #define int long long
    #define PII pair <int, int>
    #define fi first
    #define se second
    #define pub push_back
    #define pob pop_back
    #define puf push_front
    #define pof pop_front
    #define lb lower_bound
    #define ub upper_bound
    #define i128 __int128
    #define pcnt(x) __builtin_popcount(x)
    #define mem(a,goal) memset(a, (goal), sizeof(a))
    #define rep(x,start,end) for(int x = (start) - ((start) > (end)); x != (end) - ((start) > (end)); ((start) < (end) ? x ++ : x --))
    #define aLL(x) (x).begin(), (x).end()
    #define sz(x) (int)(x).size()
    const int INF = 998244353;
    const int P = 1e9 + 7;
    const int N = 2e5 + 5;
    int s1[N + 1][30], s2[N + 1][30];
    void solve()
    {
        int n, q;
        cin >> n >> q;
        vector<char> a(n + 1), b(n + 1);
        for(int i = 1; i <= n; ++ i) cin >> a[i];
        for(int i = 1; i <= n; ++ i) cin >> b[i];
        for(int i = 1; i <= n; ++ i)
        {
            s1[i][a[i] - 'a'] ++;
            for(int j = 0; j < 26; ++ j)
            {
                s1[i][j] += s1[i - 1][j];
            }
        }
        
        for(int i = 1; i <= n; ++ i)
        {
            s2[i][b[i] - 'a'] ++;
            for(int j = 0; j < 26; ++ j)
            {
                s2[i][j] += s2[i - 1][j];
            }
        }
     
        while(q --)
        {
            int l, r;
            cin >> l >> r;
            int sum = 0;
     
            for(int i = 0; i < 26; ++ i)
            {
                int p = s1[r][i] - s1[l - 1][i];
                int q = s2[r][i] - s2[l - 1][i];
                sum += abs (p - q);
            }
            cout << sum / 2 << '\n';
        }
    }
    signed main()
    {
        int t;
        cin >> t;
        while(t --) solve();
        return 0;
    }
    
    • 1

    信息

    ID
    5475
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    (无)
    递交数
    4
    已通过
    1
    上传者