hdu5517(二维树状数组)

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=AB={a,c,da,bA, c,d,eB and b=e}

For each  a,b,cC, its BETTER set is defined as

BETTERC(a,b,c)={u,v,wCu,v,wa,b,c, ua, vb, wc}

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,cCBETTERC(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 (1t10) 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 (1n105) and  m (1m105) 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  1ai,bi105.
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  1ci,di103 and  1ei105.
 

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);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值