CF1872E.Data Structures Fan
普及/提高-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given an array of integers a1,a2,…,an, as well as a binary string† s consisting of n characters.
Augustin is a big fan of data structures. Therefore, he asked you to implement a data structure that can answer q queries. There are two types of queries:
- "1 l r" (1≤l≤r≤n) — replace each character si for l≤i≤r with its opposite. That is, replace all 0 with 1 and all 1 with 0.
- "2 g" (g∈0,1) — calculate the value of the bitwise XOR of the numbers ai for all indices i such that si=g. Note that the XOR of an empty set of numbers is considered to be equal to 0.
Please help Augustin to answer all the queries!
For example, if n=4, a=[1,2,3,6], s=1001, consider the following series of queries:
- "2 0" — we are interested in the indices i for which si=0, since s=1001, these are the indices 2 and 3, so the answer to the query will be a2⊕a3=2⊕3=1.
- "1 1 3" — we need to replace the characters s1,s2,s3 with their opposites, so before the query s=1001, and after the query: s=0111.
- "2 1" — we are interested in the indices i for which si=1, since s=0111, these are the indices 2, 3, and 4, so the answer to the query will be a2⊕a3⊕a4=2⊕3⊕6=7.
- "1 2 4" — s=0111 → s=0000.
- "2 1" — s=0000, there are no indices with si=1, so since the XOR of an empty set of numbers is considered to be equal to 0, the answer to this query is 0.
† A binary string is a string containing only characters 0 or 1.
给你一个整数数组 a1,a2,…,an,以及一个由 n 个字符组成的二进制字符串† s。
奥古斯丁是一位数据结构爱好者。因此,他请你实现一种数据结构,以回答 q 个查询。查询共分两类:
- “1 l r”(其中 1≤l≤r≤n)——将所有满足 l≤i≤r 的字符 si 替换为其相反字符。即:将所有 0 替换为 1,所有 1 替换为 0。
- “2 g”(其中 g∈{0,1})——计算所有满足 si=g 的下标 i 对应的 ai 的按位异或值。注意:空集合的 XOR 值定义为 0。
请帮助奥古斯丁回答所有查询!
例如,若 n=4,a=[1,2,3,6],s=1001,考虑如下一系列查询:
- “2 0” —— 我们关注满足 si=0 的下标 i;由于 s=1001,这些下标为 2 和 3,因此该查询的答案为 a2⊕a3=2⊕3=1。
- “1 1 3” —— 我们需将字符 s1,s2,s3 替换为其相反字符;查询前 s=1001,查询后变为 s=0111。
- “2 1” —— 我们关注满足 si=1 的下标 i;由于 s=0111,这些下标为 2、3 和 4,因此该查询的答案为 a2⊕a3⊕a4=2⊕3⊕6=7。
- “1 2 4” —— s=0111 → s=0000。
- “2 1” —— s=0000,不存在满足 si=1 的下标,因此根据空集合的 XOR 值为 0 的约定,该查询的答案为 0。
† 二进制字符串是指仅包含字符 0 或 1 的字符串。
输入格式
The first line of the input contains one integer t (1≤t≤104) — the number of test cases in the test.
The descriptions of the test cases follow.
The first line of each test case description contains an integer n (1≤n≤105) — the length of the array.
The second line of the test case contains n integers a1,a2,…,an (1≤ai≤109).
The third line of the test case contains the binary string s of length n.
The fourth line of the test case contains one integer q (1≤q≤105) — the number of queries.
The subsequent q lines of the test case describe the queries. The first number of each query, tp∈1,2, characterizes the type of the query: if tp=1, then 2 integers 1≤l≤r≤n follow, meaning that the operation of type 1 should be performed with parameters l,r, and if tp=2, then one integer g∈0,1 follows, meaning that the operation of type 2 should be performed with parameter g.
It is guaranteed that the sum of n over all test cases does not exceed 105, and also that the sum of q over all test cases does not exceed 105.
输入的第一行包含一个整数 t(1≤t≤104)—— 表示测试用例的数量。
随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤105)—— 表示数组的长度。
测试用例的第二行包含 n 个整数 a1,a2,…,an(1≤ai≤109)。
测试用例的第三行包含一个长度为 n 的二进制字符串 s。
测试用例的第四行包含一个整数 q(1≤q≤105)—— 表示查询的数量。
接下来的 q 行描述了该测试用例中的各个查询。每个查询的第一项为数字 tp∈{1,2},用于表示查询类型:若 tp=1,则其后跟随两个整数 1≤l≤r≤n,表示需以参数 l,r 执行类型 1 的操作;若 tp=2,则其后跟随一个整数 g∈{0,1},表示需以参数 g 执行类型 2 的操作。
保证所有测试用例的 n 之和不超过 105,且所有测试用例的 q 之和也不超过 105。
输出格式
For each test case, and for each query of type 2 in it, output the answer to the corresponding query.
对于每个测试用例,以及其中每个类型为 2 的查询,输出对应查询的答案。
输入输出样例
输入#1
5 5 1 2 3 4 5 01000 7 2 0 2 1 1 2 4 2 0 2 1 1 1 3 2 1 6 12 12 14 14 5 5 001001 3 2 1 1 2 4 2 1 4 7 7 7 777 1111 3 2 0 1 2 3 2 0 2 1000000000 996179179 11 1 2 1 5 1 42 20 47 7 00011 5 1 3 4 1 1 1 1 3 4 1 2 4 2 0
输出#1
3 2 6 7 7 11 7 0 0 16430827 47
说明/提示
Let's analyze the first test case:
- "2 0" — we are interested in the indices i for which si=0, since s=01000, these are the indices 1,3,4, and 5, so the answer to the query will be a1⊕a3⊕a4⊕a5=1⊕3⊕4⊕5=3.
- "2 1" — we are interested in the indices i for which si=1, since s=01000, the only suitable index is 2, so the answer to the query will be a2=2.
- "1 2 4" — we need to replace the characters s2,s3,s4 with their opposites, so before the query s=01000, and after the query: s=00110.
- "2 0" — we are interested in the indices i for which si=0, since s=00110, these are the indices 1,2, and 5, so the answer to the query will be a1⊕a2⊕a5=1⊕2⊕5=6.
- "2 1" — we are interested in the indices i for which si=1, since s=00110, these are the indices 3 and 4, so the answer to the query will be a3⊕a4=3⊕4=7.
- "1 1 3" — s=00110 → s=11010.
- "2 1" — we are interested in the indices i for which si=1, since s=11010, these are the indices 1,2, and 4, so the answer to the query will be a1⊕a2⊕a4=1⊕2⊕4=7.
我们来分析第一个测试用例:
- “2 0” — 我们关注满足 si=0 的下标 i;由于 s=01000,这些下标为 1,3,4 和 5,因此该查询的答案为 a1⊕a3⊕a4⊕a5=1⊕3⊕4⊕5=3。
- “2 1” — 我们关注满足 si=1 的下标 i;由于 s=01000,唯一满足条件的下标是 2,因此该查询的答案为 a2=2。
- “1 2 4” — 我们需要将字符 s2,s3,s4 替换为其相反字符;查询前 s=01000,查询后变为 s=00110。
- “2 0” — 我们关注满足 si=0 的下标 i;由于 s=00110,这些下标为 1,2 和 5,因此该查询的答案为 a1⊕a2⊕a5=1⊕2⊕5=6。
- “2 1” — 我们关注满足 si=1 的下标 i;由于 s=00110,这些下标为 3 和 4,因此该查询的答案为 a3⊕a4=3⊕4=7。
- “1 1 3” — s=00110 → s=11010。
- “2 1” — 我们关注满足 si=1 的下标 i;由于 s=11010,这些下标为 1,2 和 4,因此该查询的答案为 a1⊕a2⊕a4=1⊕2⊕4=7。
输入解题思路,AI测评打分。不知道怎么写?