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.
Input
The first line of input contains four integers, \(n,p,q\) and \(r\) (\(1 \le n,p,q,r \le 500000\)), the number of cards in the collection, the maximum possible strength, the maximum possible defense, and the maximum possible speed, respectively.
The next \(n\) lines each contain three integers. In particular, the \(i\)-th line contains \(a_i,b_i\) and \(c_i\) (\(1\le a_i\le p,1\le b_i\le q,1\le c_i\le r\)), the strength, defense and speed of the \(i\)-th collection card, respectively.
Output
Output a single integer on a line by itself, the number of different cards that can beat all the cards in her collection.
Examples
Input
1 | 3 4 4 5 |
Output
1 | 10 |
Input
1 | 5 10 10 10 |
Output
1 | 972 |
Note
In the first test case, the maximum possible strength is 4, the maximum possible defense is 4 and the maximum possible speed is 5. Karen has three cards:
- The first card has strength 2, defense 2 and speed 5.
- The second card has strength 1, defense 3 and speed 4.
- The third card has strength 4, defense 1 and speed 1.
There are 10 cards that beat all the cards here:
- The card with strength 3, defense 3 and speed 5.
- The card with strength 3, defense 4 and speed 2.
- The card with strength 3, defense 4 and speed 3.
- The card with strength 3, defense 4 and speed 4.
- The card with strength 3, defense 4 and speed 5.
- The card with strength 4, defense 3 and speed 5.
- The card with strength 4, defense 4 and speed 2.
- The card with strength 4, defense 4 and speed 3.
- The card with strength 4, defense 4 and speed 4.
- The card with strength 4, defense 4 and speed 5.
In the second test case, the maximum possible strength is 10, the maximum possible defense is 10 and the maximum possible speed is 10. Karen has five cards, all with strength 1, defense 1 and speed 1.
Any of the 972 cards which have at least two characteristics greater than 1 can beat all of the cards in her collection.
Problem
给定\(n\)个三元组\((a_i,b_i,c_i)\),以及三元组中每个数的上限\(p,q,r\),定义一个三元组能击败另一个三元组当且仅当这个三元组中有任意两个数严格大于另一个三元组对应的两个数。求有多少三元组能击败所有\(n\)个三元组。
Solution
记满足条件的三元组为\((x,y,z)\)。显然如果枚举\(x\),则三元组可以分为两类:\(a_i<x\)与\(a_i\ge x\)。对于\(a_i<x\)的三元组,只需要满足\(y>b_i\)或者\(z>c_i\)。而对于\(a_i\ge x\)的三元组,需要满足\(y>b_i\)并且\(z>c_i\)。
记\(smx_j(j\in [1,q])\)表示\(b_i\ge j\)的三元组中最大的\(c_i\),\(mx3\)表示\(a_i\ge x\)的三元组中最大的\(c_i\)。那么\(z\)的方案数就是\(r-max(smx_y,mx3)\),即\(z\)需要满足\(z>smx_y,z>mx3\),因为对于\(a_i\ge x\)的三元组,\(z\)一定大于\(c_i\),且对于\(b_i\ge y\)的三元组,\(z\)也一定大于\(c_i\)。
记\(mx2\)表示\(a_i\ge x\)的三元组中最大的\(b_i\),那么当\(x\)固定时,总方案可以用for (int i=mx2+1;i<=q;i++) ans+=r-max(smx[i],mx3);
进行统计。
这样的话,因为我们要统计\(a_i\ge x\)的三元组中最大的\(b_i\)(\(mx2\)),最大的\(c_i\)(\(mx3\)),所以我们应该倒着枚举\(x\)。
时间复杂度?\(\mathcal O(n^2)\)。显然不能过。
考虑优化for (int i=mx2+1;i<=q;i++) ans+=r-max(smx[i],mx3);
这段代码。
\(max\)很不好处理,我们考虑把它拆开分别计算。由\(smx_i\)的定义可得,\(\{smx_i\}\)单调不升,也就是\(i\)越小,\(smx_i\)越大。那么我们找到一个\(max\)分界线,记为\(k\),使得\(i\ge k\)时,\(smx_i\le mx3\),\(i<k\)时,\(smx_i>mx3\)。于是就可以\(\mathcal O(1)\)计算答案了,用前缀和维护\(\{smx_i\}\)的区间和即可。
显然,\(\{smx_i\},mx2,mx3\)很容易维护。那么\(k\)怎么办呢?因为\(x\)从大到小枚举,\(mx3\)会逐渐变大,而\(\{smx_i\}\)单调不升,所以\(k\)慢慢变小(向前),一直维护即可。
一些细节具体见代码。
Source
1 |
|