1. 程式人生 > >Triple HDU - 5517 —— 二維樹狀陣列

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。

題解:

我們可以先求有多少個相同的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;
}