Triple
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1184 Accepted Submission(s): 422
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).
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 Brespectively.
The second line contains 2×n nonnegative integers
which describe the multi-set A, where 1≤ai,bi≤105.
The third line contains 3×m nonnegative integers
corresponding to the m triples of integers in B, where 1≤ci,di≤103 and 1≤ei≤105.
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 Brespectively.
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
题意:n个(a,b),m个(c,d,e),都为多重集,现在使得b=e组合两个多重集,新的多重集为(a,c,d),现在求不存在组合(a1,b1,c1)使a1>=a&&b1>=b&&c1>=c的组合(a,c,d)的个数。
思路:(a,b)去重每个b保留a最大的那个,再和(c,d,e)组合,组合后(a,c,d)按大到小排序,去重(重复数自然要存储),然后利用二维树状数组来处理。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 1e5+10;
const int maxm = 1e3+10;
int n,m;
int ba[maxn];
int cntba[maxn];
bool tree[maxm][maxm];
struct node
{
int a,c,d;
int cnt;
}trpC[maxn],tpc[maxn];
bool cmp(const node x,const node y)
{
if(x.a==y.a)
{
if(x.c==y.c)
{
return x.d>y.d;
}
else return x.c>y.c;
}
else return x.a>y.a;
}
bool cmpare(node x,node y)
{
if(x.a==y.a&&x.c==y.c&&x.d==y.d)return true;
return false;
}
bool query(int x,int y)
{
for(int i=x;i;i-=((i)&(-i)))
{
for(int j=y;j;j-=((j)&(-j)))
{
if(tree[i][j])return true;
}
}
return false;
}
void update(int x,int y)
{
for(int i=x;i<maxm;i+=((i)&(-i)))
{
for(int j=y;j<maxm;j+=((j)&(-j)))
{
if(!tree[i][j])tree[i][j]=1;
}
}
}
int main()
{
int t;
scanf("%d",&t);
for(int tt=1;tt<=t;tt++)
{
int a,b;
int c,d,e;
memset(ba,0,sizeof(ba));
memset(cntba,0,sizeof(cntba));
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&a,&b);
if(ba[b]<a)
{
ba[b]=a;
cntba[b]=1;
}
else if(ba[b]==a)
{
cntba[b]++;
}
}
int cnt=0;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&c,&d,&e);
if(ba[e])
{
trpC[cnt].a=ba[e];
trpC[cnt].c=c;
trpC[cnt].d=d;
trpC[cnt].cnt=cntba[e];
cnt++;
}
}
sort(trpC,trpC+cnt,cmp);
int cnt1=0;
tpc[0].a=trpC[0].a;
tpc[0].c=trpC[0].c;
tpc[0].d=trpC[0].d;
tpc[0].cnt=trpC[0].cnt;
for(int i=1;i<cnt;i++)
{
if(cmpare(tpc[cnt1],trpC[i]))
{
tpc[cnt1].cnt+=trpC[i].cnt;
}
else
{
cnt1++;
tpc[cnt1].a=trpC[i].a;
tpc[cnt1].c=trpC[i].c;
tpc[cnt1].d=trpC[i].d;
tpc[cnt1].cnt=trpC[i].cnt;
}
}
cnt1++;
memset(tree,0,sizeof(tree));
int ans=0;
for(int i=0;i<cnt1;i++)
{
if(!query(1001-tpc[i].c,1001-tpc[i].d))
{
ans+=tpc[i].cnt;
}
update(1001-tpc[i].c,1001-tpc[i].d);
}
printf("Case #%d: %d\n",tt,ans);
}
}