jiejiejiang2004

题解:Codeforces Round 962 (Div. 3) C

C. Sort

time limit per test: 5 seconds

memory limit per test: 256 megabytes

input: standard input

output: standard output

You are given two strings \(a\) and \(b\) of length \(n\). Then, you are (forced against your will) to answer \(q\) queries.

For each query, you are given a range bounded by \(l\) and \(r\). In one operation, you can choose an integer \(i\) (\(l \leq i \leq r\)) and set \(a_i = x\) where \(x\) is any character you desire. Output the minimum number of operations you must perform such that \(\texttt{sorted(a[l..r])} = \texttt{sorted(b[l..r])}\). The operations you perform on one query does not affect other queries.

For an arbitrary string \(c\), \(\texttt{sorted(c[l..r])}\) denotes the substring consisting of characters \(c_l, c_{l+1}, ... , c_r\) sorted in lexicographical order.

给你两个长度为 \(n\) 的字符串 \(a\)\(b\) 。然后,您(被迫)回答 \(q\) 个问题。

每个查询都会给你一个由 \(l\)\(r\) 限定的范围。在一次操作中,您可以选择一个整数 \(i\) ( \(l \leq i \leq r\) ) 并设置 \(a_i = x\) 其中 \(x\) 是您想要的任何字符。输出必须执行的最少操作数,以便 \(\texttt{sorted(a[l..r])} = \texttt{sorted(b[l..r])}\) 。**对一个查询执行的操作不会影响其他查询。

对于任意字符串 \(c\)\(\texttt{sorted(c[l..r])}\) 表示由按词典顺序排列的字符 \(c_l, c_{l+1}, ... , c_r\) 组成的子串。

Input

The first line contains \(t\) (\(1 \leq t \leq 1000\)) – the number of test cases.

The first line of each test case contains two integers \(n\) and \(q\) (\(1 \leq n, q \leq 2 \cdot 10^5\)) – the length of both strings and the number of queries.

The following line contains \(a\) of length \(n\). It is guaranteed \(a\) only contains lowercase latin letters.

The following line contains \(b\) of length \(n\). It is guaranteed \(b\) only contains lowercase latin letters.

The following \(q\) lines contain two integers \(l\) and \(r\) (\(1 \leq l \leq r \leq n\)) – the range of the query.

It is guaranteed the sum of \(n\) and \(q\) over all test cases does not exceed \(2 \cdot 10^5\).

输入

第一行包含 \(t\) ( \(1 \leq t \leq 1000\) ) - 测试用例数。

每个测试用例的第一行包含两个整数 \(n\)\(q\) ( \(1 \leq n, q \leq 2 \cdot 10^5\) ) - 两个字符串的长度和查询次数。

下面一行包含长度为 \(n\)\(a\) 。可以保证 \(a\) 只包含小写拉丁字母。

下面一行包含长度为 \(n\)\(b\) 。保证 \(b\) 只包含小写拉丁字母。

下面的 \(q\) 行包含两个整数 \(l\)\(r\) ( \(1 \leq l \leq r \leq n\) ) - 查询范围。

保证所有测试用例中的 \(n\)\(q\) 之和不超过 \(2 \cdot 10^5\)

Output

For each query, output an integer, the minimum number of operations you need to perform in a new line.

输出

对于每个查询,在新行中输出一个整数,即需要执行的最少操作数。

Example

Input

3
5 3
abcde
edcba
1 5
1 4
3 3
4 2
zzde
azbe
1 3
1 4
6 3
uwuwuw
wuwuwu
2 4
1 3
1 6

Output

0
1
0
2
2
1
1
0

Note

For the first query, \(\texttt{sorted(a[1..5])} =\) abcde and \(\texttt{sorted(b[1..5])} =\) abcde, so no operations are necessary.

For the second query, you need to set $a_1 = $ e. Then, $\texttt{sorted(a[1..4])} = \texttt{sorted(b[1..4])} = $ bcde.

对于第一个查询, \(\texttt{sorted(a[1..5])} =\) abcde 和 \(\texttt{sorted(b[1..5])} =\) abcde,因此无需进行任何操作。

对于第二个查询,需要设置 $a_1 = $ e,然后是 $\texttt{sorted(a[1..4])} = \texttt{sorted(b[1..4])} = $ bcde。

题意

每次给你两个数字 \(n,q\) ,两个长度为 \(n\) 的字符串 \(a,b\)
然后进行 \(q\) 次询问,每次询问给出两个数字 \(l,r\)
表示对字符串 \(a,b\)\([l,r]\) 区间进行排序(排序后恢复原样,不影响下一次询问)
每次排序前可以对字符串进行操作:把第 \(i\) 个字符替换成任意字符。
问:每次排序前需要进行至少多少次操作,才能使字符串 \(a,b\) 排序完后的区间内容相同

题解

很显然,如果暴力做的话每次一定会超时
题目说的是排序,实际上是查重
在这里我们可以做一个预处理:前缀和
在每个字符的地方创建一个数组,记录每种字母前面出现次数

  • 假如在字符串 \(a\) 出现过,那么该字符对应的数组内的元素就 加1
  • 假如在字符串 \(b\) 出现过,那么该字符对应的数组内的元素就 减1

以此完成查重
然后每次询问,我们对 \(r\)\(l-1\) 内的数组进行处理
对每个下标(一共 \(26\) 个嘛),假如数组 \(r\) 内的元素 大于 数组 \(l-1\) 内的元素,就加上他们的差值(也就是要改变的数量)
然后输出就好了

代码

#include <bits/stdc++.h>

#define int long long
#define INF 0x3f3f3f3f
#define all(x) x.begin(),x.end()

using i64 = long long;

int t = 1;

void solve(){
    int n,q;
    std::cin >> n >> q;
    std::string a,b;
    std::cin >>a >> b;
    std::vector<std::array<int,26> > qzh(n+1);
    for(int i = 0 ; i < n ; i ++) {
        qzh[i+1] = qzh[i];
        qzh[i+1][a[i]-'a']++;
        qzh[i+1][b[i]-'a']--;
    }

    for(int i = 0 ; i < q ; i ++) {
        int l,f;
        std::cin >> l >> f;
        l--;
        int ans = 0;
        for(int j = 0 ; j < 26 ; j ++) {
            ans += std::max(qzh[f][j] - qzh[l][j],(int)0);
        }
        std::cout << ans << "\n";
    }

}

signed main(){
    std::cin >> t;
    while(t--){
        solve();
    }
    return 0;
}

posted on 2024-07-30 10:17  Jiejiejiang  阅读(14)  评论(0编辑  收藏  举报

导航