Description

Given the finite multi-set \(A\) of \(n\) pairs of integers, an another finite multi-set \(B\) of \(m\) triples of integers, we define the product of \(A\) and \(B\) as a multi-set

\[C=A∗B=\lbrace \langle a,c,d \rangle \mid \langle a,b \rangle \in A, \langle c,d,e \rangle \in B \; and \; b=e \rbrace
\]

For each ⟨a,b,c⟩∈C, its BETTER set is defined as

\[BETTER_{C}(\langle a,b,c \rangle )=\lbrace \langle u,v,w \rangle \in C \mid \langle u,v,w \rangle \neq \langle a,b,c \rangle, u \ge a, v \ge b, w \ge c \rbrace
\]

As a multi-set of triples, we define the TOP subset (as a multi-set as well) of \(C\), denoted by \(TOP(C)\), as

\[TOP(C)=\lbrace \langle a,b,c \rangle \in C \mid BETTER_{C}(\langle a,b,c \rangle \rbrace ) = \phi
\]

You need to compute the size of \(TOP(C)\).

Input

The input contains several test cases. The first line of the input is a single integer \(t (1 \le t \le 10)\) which is the number of test case. Then \(t\) test cases follow.

Each test case contains three lines. The first line contains two integers \(n (1 \le n \le 10^{5})\) and \(m (1 \le m \le 10^{5})\) corresponding to the size of \(A\) and \(B\) respectively.

The second line contains \(2 \times n\) nonnegative integers \(a_{1},b_{1},a_{2},b_{2},\cdots,a_{n},b_{n}\) which describe the multi-set \(A\), where \(1 \le a_{i},b_{i} \le 10^{5}\).

The third line contains \(3 \times m\) nonnegative integers \(c_{1},d_{1},e_{1},c_{2},d_{2},e_{3},\cdots,c_{m},d_{m},e_{m}\) corresponding to the \(m\) triples of integers in \(B\), where \(1 \le c_{i},d_{i} \le 10^{3}\) and \(1 \le e_{i} \le 10^{5}\).

Output

For each test case, you should output the size of set \(TOP(C)\).

Sample Input

2

5 9

1 1 2 2 3 3 3 3 4 2

1 4 1 2 2 1 4 1 1 1 3 2 3 2 2 4 1 2 2 4 3 3 2 3 4 1 3

3 4

2 7 2 7 2 7

1 4 7 2 3 7 3 2 7 4 1 7

Sample Output

Case #1: 5

Case #2: 12

首先对于二元组\(\langle a_{1},b \rangle,\langle a_{2},b \rangle \dots\)我肯定直选\(a\)最大的与\(B\)集合中的元素配对。不妨设最大的为\(a_{1}\),若我选择了\(a_{i}\)配对,那么$$\langle a_{1},c,d \rangle \in BETTER_{C}\langle a_{i},b,c \rangle$$

所以\(C\)中有用的元素减少到了\(O(N)\)个。之后就是要找的极大的三元组的个数,这个CDQ分治或者二维树状数组都行(就是最大非升子序列长度为\(1\)的元素个数)。

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cstdlib>
using namespace std; typedef long long ll;
#define lowbit(a) (a&(-a))
#define maxm (1010)
#define maxn (100010)
int T,tot,cnt,N,M,A[maxn],nA[maxn],tree[maxm][maxm]; ll ans;
struct node { int a,b; }; vector <node> B[maxn];
struct mode
{
int a,b,c; ll num;
friend inline bool operator <(const mode &x,const mode &y)
{
if (x.a != y.a) return x.a < y.a;
else
{
if (x.b != y.b) return x.b < y.b;
else return x.c < y.c;
}
}
friend inline bool operator ==(const mode &x,const mode &y) { return x.a == y.a&&x.b == y.b&&x.c == y.c; }
}C[maxn],bac[maxn]; inline bool cmp(int x,int y) { return x > y; } inline void ins(int i,int y)
{
for (;i <= 1000;i += lowbit(i))
for (int j = y;j <= 1000;j += lowbit(j)) ++tree[i][j];
}
inline int calc(int i,int y)
{
int ret = 0;
for (;i;i -= lowbit(i)) for (int j = y;j;j -= lowbit(j)) ret += tree[i][j];
return ret;
} inline void init()
{
tot = cnt = ans = 0; memset(tree,0,sizeof(tree));
for (int i = 1;i <= 100000;++i) A[i] = nA[i] = 0,B[i].clear();
} int main()
{
freopen("5517.in","r",stdin);
freopen("5517.out","w",stdout);
scanf("%d",&T);
for (int Cas = 1;Cas <= T;++Cas)
{
printf("Case #%d: ",Cas);
scanf("%d %d",&N,&M); init();
for (int i = 1;i <= N;++i)
{
int a,b; scanf("%d %d",&a,&b);
if (a > A[b]) A[b] = a,nA[b] = 1;
else if (a == A[b]) nA[b]++;
}
for (int i = 1;i <= M;++i)
{
int a,b,c; scanf("%d %d %d",&a,&b,&c);
B[c].push_back((node){a,b});
}
for (int i = 1;i <= 100000;++i)
if (A[i]&&!B[i].empty())
for (int j = 0,nn = B[i].size();j < nn;++j)
bac[++tot] = (mode){A[i],B[i][j].a,B[i][j].b,nA[i]};
sort(bac+1,bac+tot+1);
for (int i = 1,j;i <= tot;i = j)
{
for (j = i+1;j <= tot&&bac[j] == bac[i];++j) bac[i].num += bac[j].num;
C[++cnt] = bac[i];
}
for (int i = cnt;i;--i)
{
int p1 = 1000-C[i].b+1,p2 = 1000-C[i].c+1;
if (!calc(p1,p2)) ans += C[i].num; ins(p1,p2);
}
cout << ans << endl;
}
fclose(stdin); fclose(stdout);
return 0;
}

Hdu5517 Triple的更多相关文章

  1. 【hdu5517】Triple

    题目大意:给定一个二元组集合A{<a, b>}, 一个三元组集合B{<c, d, e>}, 定义 C 为 A 和 B 在 {b = e} 上的连接,求 C 集合中凸点的个数,即 ...

  2. 山东省第七届ACM省赛------Triple Nim

    Triple Nim Time Limit: 2000MS Memory limit: 65536K 题目描述 Alice and Bob are always playing all kinds o ...

  3. hdu 3908 Triple(组合计数、容斥原理)

    Triple Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Su ...

  4. hdu 5517 Triple(二维树状数组)

    Triple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  5. Codeforces 1071 C - Triple Flips

    C - Triple Flips 思路: 小范围暴力 大范围递归构造 构造方法: solve(l, r) 表示使l 到 r 区间全变为0的方法 为了使反转次数小于等于n/3 + 12 我们只需要保证每 ...

  6. 【BZOJ3771】Triple(生成函数,多项式运算)

    [BZOJ3771]Triple(生成函数,多项式运算) 题面 有\(n\)个价值\(w\)不同的物品 可以任意选择\(1,2,3\)个组合在一起 输出能够组成的所有价值以及方案数. \(n,w< ...

  7. PAT 1009. Triple Inversions (35) 数状数组

    Given a list of N integers A1, A2, A3,...AN, there's a famous problem to count the number of inversi ...

  8. 【BZOJ 3771】 3771: Triple (FFT+容斥)

    3771: Triple Time Limit: 20 Sec  Memory Limit: 64 MBSubmit: 547  Solved: 307 Description 我们讲一个悲伤的故事. ...

  9. hihoCoder #1872 : Pythagorean triple

    此题是 2018 年 ICPC Asia Beijing Regional Contest 的 C 题. 题目大意 求斜边长度不超过 $n$($ n \le 10^9$) 的勾股数的数量.不计两直角边 ...

随机推荐

  1. 再次轻度破解EXE文件

    在经历股市多年的大起大落.大赚大赔之后.痛定思痛.深切感到在金融市场拼搏.必须建立健全交易纪律守则,严格运行. 这套完整的纪律守则,就是"交易系统". 在很多方面,它与一般的专家系 ...

  2. linux 启动流程图

    http://blog.163.com/x_ares/blog/static/101548562011710112613165/ http://baogf92.blog.51cto.com/10869 ...

  3. at91sam9x5 linux 4.1.0下dts驱动编程模型

    测试环境:  CPU: AT91SAM9X35      Linux: Atmel提供的linux-at91-linux4sam_5.3 (Linux-4.1.0) 转载请注明: 凌云物网智科嵌入式实 ...

  4. Vmare12(虚拟机)安装Mac OS X Yosemite 10.10

    需要预备的软件如下: OSX10.10的系统镜像,下载好之后将后缀.cdr改成.iso,下载来源如下:    链接:http://pan.baidu.com/s/1sj4ri5R 密码:y86w un ...

  5. jQuery各种选择器总结

    首先介绍几个简单的: id选择器 $('#p1').html('<font color='red'>nihao</font>); 类选择器:表示页面上所有应用了a样式的标签 $ ...

  6. Android EditText的输入监听,输入字符的动态获取

    http://itindex.net/detail/38974-android-edittext-%E7%9B%91%E5%90%AC 有时候我们可能会用到时时的监听EditText输入字符的时时监听 ...

  7. javascript类继承系列三(对象伪装)

    原理:在子类的构造器上调用超类构造器(父类构造器中的this指向子类实例),js提供了apply()和call()函数,可以实现这种调用 function baseClass() { this.col ...

  8. 记录平时code点滴,这次是通过一张充满异样字符的表,对数据表中的每一列进行清理,比double quotation的issue难多了!

    需要提供对象: 一张需要被替换字符的表. 通过游标结合动态SQL对某一张特定表的所有列进行更新,主要是对其列值的异常字符处理. dbo.Characters_need_to_be_replaced c ...

  9. linux常用命令(自我积累)

    创建目录:mkdir + 目录名 使文件可执行:chmod +x filename 执行文件:./filename 来执行您的脚本 {程序必须以下面的行开始(必须方在文件的第一行): #!/bin/s ...

  10. xcode7启动页的尺寸设置

    iPhone Portrait iOS 8,9-Retina HD 5.5 (1242×2208) @3x iPhone Portrait iOS 8,9-Retina HD 4.7 (750×133 ...