Triple HDU - 5517 —— 二维树状数组

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©, as

TOP©={⟨a,b,c⟩∈C∣BETTERC(⟨a,b,c⟩)=∅}

You need to compute the size of TOP©.
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©.
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

题意:

给你n个二元组<a,b>,m个三元组<c,d,e>,当e==b的时候两个组可以合成一个<a,c,d>当所有合成后的三元组之中没有任意一个三元组的所有元素大于等于一个三元组,那么这个三元组就称为top,求有多少top三元组,举个例子:<1,2,3>和<1,3,2>是两个top三元组,<1,2,3>和<1,3,4>只有后面这一个是top。

扫描二维码关注公众号,回复: 3957005 查看本文章

题解:

我们可以先求有多少个相同的b,在b相同时求出最大的a,这是肯定的,比如<1,2>,<2,2>和<1,2,2>匹配的时候,最终结果是<1,2,2>和<2,2,2>,那么<1,2,2>就没有意义了。之后就算每个e有多少个b匹配,加到结构体里。排序先按a排序,再按c,再按d,都是从大到小,这样插到树状数组里面首先可以排除a的影响,然后从大到小插,从小到大找。

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
const int maxn=1e3+5;
int b[N],cntb[N];
struct node
{
    int a,c,d,cnt;
    node(){}
    node(int a,int c,int d,int cnt):a(a),c(c),d(d),cnt(cnt){}
    bool operator< (const node& x)const
    {
        if(a==x.a&&c==x.c)
            return d>x.d;
        else if(a==x.a)
            return c>x.c;
        return a>x.a;
    }
    bool operator == (const node& x)const
    {
        if(a==x.a&&c==x.c&&d==x.d)
            return 1;
        return 0;
    }
}p[N];
int n,m,all,vis[maxn][maxn];
int lowbit(int x)
{
    return x&(-x);
}
void add(int x,int y)
{
    for(int i=x;i;i-=lowbit(i))
        for(int j=y;j;j-=lowbit(j))
            vis[i][j]=1;
}
int query(int x,int y)
{
    for(int i=x;i<maxn;i+=lowbit(i))
    {
        for(int j=y;j<maxn;j+=lowbit(j))
        {
            if(vis[i][j])
                return 1;
        }
    }
    return 0;
}
int main()
{
    int t;
    scanf("%d",&t);
    int cas=0;
    while(t--)
    {
        scanf("%d%d",&n,&m);
        memset(b,0,sizeof(b));
        memset(cntb,0,sizeof(cntb));
        memset(vis,0,sizeof(vis));
        int x,y,z;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&x,&y);
            if(b[y]==x)
                cntb[y]++;
            else if(x>b[y])
                b[y]=x,cntb[y]=1;
        }
        all=0;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&x,&y,&z);
            if(cntb[z])
                p[++all]=node(b[z],x,y,cntb[z]);
        }
        sort(p+1,p+1+all);
        int len=0;
        p[++len]=p[1];
        for(int i=2;i<=all;i++)
        {
            if(p[i]==p[len])
                p[len].cnt+=p[i].cnt;
            else
                p[++len]=p[i];
        }
        int ans=0;
        for(int i=1;i<=len;i++)
        {
            if(!query(p[i].c,p[i].d))
                ans+=p[i].cnt;
            add(p[i].c,p[i].d);
        }
        printf("Case #%d: %d\n",++cas,ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/tianyizhicheng/article/details/83302143