HR34E Magic Cards <状压>
Problem
Magic Cards
by Birjik
Description
Birzhan has cards, numbered from to . Every card has each number from to written on it. Some numbers exist on the front side of the card and some on the back side. No number exists on both the sides of a card at the same time. These cards are placed on the table in a row such that only one side is visible. Birzhan is allowed to flip them any number of times.
Now, Birzhan has to answer queries each of them consisting of two integers and (). We define as the sum of the squares of every integer from to if it exists on the visible side of any card numbered from to . Given that Birzhan can flip any number of cards any number of times, find and print the maximum value of .
For example, given cards and we have the as follows:-
Now, for and . We have on the two cards, the sum of their squares is .
If we flip the first card, we get , the sum of their squares is .
And, if we flip both cards, we get , the sum of their squares is
Hence, the maximum value of in the above case is .
Input Format
The first line contains three space-separated integers, , , and , denoting the number of cards, what numbers written on each card, and the number of queries, respectively.
Each of the next lines describes the cards. On each line, the first number is , denoting the count of numbers written on the visible side of the card. Next space-separated unique integers represent the numbers written on the visible side of that card, each between and .
Next lines contain two space-separated integers, and , describing the query mentioned above.
Constraints
Output Format
Print the maximum value of for each query.
Sample
Sample Input 0
1 | 2 3 2 |
Sample Output 0
1 | 9 |
Explanation 0
In the first query, there is only card, and Birzhan has options:
- Let it be as it is: sum of squares would be
- Or flip it: sum of squares would be
So the maximum Birzhan can achieve is .
On the second query, Birzhan doesn’t need to flip any of them to maximize so the answer would be .
Sample Input 1
1 | 2 5 1 |
Sample Output 1
1 | 50 |
Explanation 1
If Birzhan flipped only the first card, the answer would be . You can notice that it’s impossible to get a better result.
标签:状压
Translation
题目大意:有张牌,每张牌都写上了到的所有正整数各一次,其中有的数字写在牌的正面,有的写在反面。有次询问,每次询问一个区间的所有牌,每张牌可以任意决定正面朝上还是反面朝上,问朝上的牌中所有出现过至少一次的数的平方和的最大能是多少。
Solution
没结论是神仙题,有结论立马变水题。
结论:若牌的数量大于,则一定可以通过某种方式使得所有数都至少出现一次。
证明:对于第张牌,若前面还没出现过的数有个,那么这张牌取正面或反面至少可以出现个前面未出现过的数。于是张牌可以保证出现所有个数。
对于询问,若,则答案为,否则,区间长度一定不大于。预处理出长度不大于的所有区间的最大可能答案,直接回答询问即可。
接下来用状态压缩加速预处理。对于区间的一种方案,用位二进制数表示,其中表示这一位置的牌背面朝上,表示这一位置的牌正面朝上。对于中的每一个数,均可以处理出一个数,其中第位表示第个位置的牌上这个数是在正面()还是背面()。那么当且仅当这个区间的方案为时,才不可能出现这个数。用表示情况下所有不能出现的数的平方和。处理每个数对应的并在减去这个数的平方,然后枚举每种方案取最大值即可。用滑动窗口即可在的时间内从一个区间转移到另一个区间,如此即可在的复杂度下进行预处理。
Code
1 |
|