Triple
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 295 Accepted Submission(s): 120
Problem 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={⟨a,c,d⟩∣⟨a,b⟩∈A, ⟨c,d,e⟩∈B and b=e}
For each ⟨a,b,c⟩∈C, its BETTER set is defined as
BETTERC(⟨a,b,c⟩)={⟨u,v,w⟩∈C∣⟨u,v,w⟩≠⟨a,b,c⟩, u≥a, v≥b, w≥c}
As a \textbf{multi-set} of triples, we define the TOP subset (as a multi-set as well) of C, denoted by TOP(C), as
TOP(C)={⟨a,b,c⟩∈C∣BETTERC(⟨a,b,c⟩)=∅}
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≤t≤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≤n≤105) and m (1≤m≤105) corresponding to the size of A and B respectively.
The second line contains 2×n nonnegative integers
a1,b1,a2,b2,⋯,an,bn
which describe the multi-set A, where 1≤ai,bi≤105.
The third line contains 3×m nonnegative integers
c1,d1,e1,c2,d2,e3,⋯,cm,dm,em
corresponding to the m triples of integers in B, where 1≤ci,di≤103 and 1≤ei≤105.
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
Source
2015ACM/ICPC亚洲区沈阳站-重现赛
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5517
题意:
有二元组(a,b),三元组(c,d,e)。当b == e时它们能构成(a,c,d)。
然后,当不存在(u,v,w)!=(a,b,c)且u>=a,v>=b,w>=c时,则是一个新集合里的元素。
问这个r集合有几个元素。
思路:
首先可想到一个剪支:在第一个二元组里b相同时只用维护最小的a即可;
构建P(新集合后的三元组),按a排序,剩二维,加入集合的条件即c,d的二维面积,不存在新集合点,1000*1000可拿二维树状数组维护;
代码:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int n,m;
int cnt[100005],id[100005];
int t[1005][1005];
struct node{
int a,c,d,v;
}p[100005];
int cmp(node x,node y)
{
if(x.a!=y.a) return x.a<y.a;
if(x.c!=y.c) return x.c<y.c;
return x.d<y.d;
}
void add(int x,int y,int v)
{
for(int i=x;i<=1000;i+=i&(-i))
for(int j=y;j<=1000;j+=j&(-j))
{
t[i][j]+=v;
}
return;
}
long long cal(int x,int y)
{
long long ret=0;
for(int i=x;i>0;i-=i&(-i))
for(int j=y;j>0;j-=j&(-j))
ret+=t[i][j];
return ret;
}
long long query(int x1,int y1,int x2,int y2)
{
return cal(x2,y2)-cal(x2,y1-1)-cal(x1-1,y2)+cal(x1-1,y1-1);
}
int main()
{
int T;
scanf("%d",&T);
int aa,bb,cc;
for(int ii=1;ii<=T;ii++)
{
memset(t,0,sizeof(t));
memset(id,0,sizeof(id));
memset(cnt,0,sizeof(cnt));
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&aa,&bb);
if(id[bb]<aa)
{
id[bb]=aa;
cnt[bb]=1;
}
else if(id[bb]==aa) cnt[bb]++;
}
int tot=0;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&aa,&bb,&cc);
if(cnt[cc])
{
node now;
now.a=id[cc],now.c=aa,now.d=bb,now.v=cnt[cc];
p[++tot]=now;
}
}
sort(p+1,p+1+tot,cmp);
int n=1;
for(int i=2;i<=tot;i++)
if(p[i].a==p[n].a&&p[i].c==p[n].c&&p[i].d==p[n].d)
{
p[n].v+=p[i].v;
}else p[++n]=p[i];
long long ans=0;
for(int i=n;i>=1;i--)
{
if(!query(p[i].c,p[i].d,1000,1000)) ans+=p[i].v;
add(p[i].c,p[i].d,1);
}
printf("Case #%d: %lld\n", ii, ans);
}
}