Karen and Cards
题目描述
Karen just got home from the supermarket, and is getting ready to go to sleep.
After taking a shower and changing into her pajamas, she looked at her shelf and saw an album. Curious, she opened it and saw a trading card collection.
She recalled that she used to play with those cards as a child, and, although she is now grown-up, she still wonders a few things about it.
Each card has three characteristics: strength, defense and speed. The values of all characteristics of all cards are positive integers. The maximum possible strength any card can have is $p$, the maximum possible defense is $q$ and the maximum possible speed is $r$.
There are $n$ cards in her collection. The $i$-th card has a strength $a_i$, defense $b_i$ and speed $c_i$, respectively.
A card beats another card if at least two of its characteristics are strictly greater than the corresponding characteristics of the other card.
She now wonders how many different cards can beat all the cards in her collection. Two cards are considered different if at least one of their characteristics have different values.
题意概述
给定$n$张牌,每张牌有三个属性$a_i, b_i, c_i$。一张牌能压制另一张牌$\Leftrightarrow$前者的至少两个属性严格大于后者的对应属性。已知三个属性的上限分别为$p, q, r$,问有多少种不同的牌能压制给定的$n$张牌。
数据范围:$1 \le n, p, q, r \le 5 \times 10^5, \ 1 \le a_i \le p, \ 1 \le b_i \le q, \ 1 \le c_i \le r$。
算法分析
可以先枚举从$1$到$p$枚举$a$属性,分别算出可行的牌数,累加得到答案。
现在考虑$b, c$属性。设正在枚举$a=k$的情况。我们以$b$为$x$轴、$c$为$y$轴建立平面直角坐标系。对于$a_i \ge k$的牌,必须满足$b_i \lt x \land c_i \lt y$;对于$a_i \lt k$的牌,必须满足$b_i \lt x \lor c_i \lt y$。
如图,红色区域表示$a_i \ge k$的牌可取的点,蓝色、绿色区域表示$a_i \lt k$的牌不可取的点。显然,用红色区域的点数减去红色和蓝色、绿色区域交集的点数,就是$a=k$时可行的牌数。
红色区域的大小取决于$a_i \ge k$的牌中$b_i, c_i$的最大值。蓝色、绿色区域的大小取决于$a_i \lt k$的牌中$b_i, c_i$的值;这可以用线段树来维护。理论时间复杂度为$O(n\log n)$。
代码
1 |
|