hdu 5517 (三元组)二维树状数组

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5514

思路: 第一眼看还觉得没法处理,但是我们可以发现他要求top三元组,所以对于二元组  a b 对于每个b 我只需要保留他的最大的a就可以了。生成的新的三元组最多就100000 个。 这样的话,就直接  二维树状数组求就可以了。

代码:

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

int B[100005],cntb[100005];

ll tr[1005][1005];
int n,m;
int tot;
struct node
{
    int a,c,d;
    int cnt;
}san[100005],tmp[100005];

void add(int x, int y, ll z){ //将点(x, y)加上z
    int memo_y = y;
    while(x <= 1002){
        y = memo_y;
        while(y <= 1002)
            tr[x][y] += z, y += y & -y;
        x += x & -x;
    }
}

ll ask(int x, int y){//求左上角为(1,1)右下角为(x,y) 的矩阵和
    ll res = 0, memo_y = y;
    while(x){
        y = memo_y;
        while(y)
            res += tr[x][y], y -= y & -y;
        x -= x & -x;
    }
    return res;
}

void range_add(int xa, int ya, int xb, int yb, int z){
    add(xa, ya, z);
    add(xa, yb + 1, -z);
    add(xb + 1, ya, -z);
    add(xb + 1, yb + 1, z);
}

int ask(int x,int y,int x2,int y2)  // 查询 x+1 y+1 到 x2 y2
{
    ll c1=ask(x,y);
    ll c2=ask(x2,y);
    ll c3=ask(x,y2);
    ll c4=ask(x2,y2);
    return c4-c2-c3+c1;
}

bool cmp(node x,node y)
{
    if(x.a==y.a&&x.c==y.c) return x.d>y.d;
    if(x.a==y.a) return x.c>y.c;
    return x.a>y.a;
}

int buxiangdeng(int i,int j)
{
    if(san[i].a!=san[j].a||san[i].c!=san[j].c||san[i].d!=san[j].d) return 1;
    return 0;
}

int main()
{
    int T;
    int x,y,z;
    scanf("%d",&T);
    int kk=0;
    while(T--)
    {
        for(int i=0;i<=100002;i++)
        {
            B[i]=cntb[i]=0;
        }
        for(int i=0;i<=1002;i++)
        {
            for(int j=0;j<=1002;j++)
            {
                tr[i][j]=0;
            }
        }

        scanf("%d %d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            scanf("%d %d",&x,&y);
            if(B[y]==0||x>B[y])
            {
                B[y]=x; cntb[y]=1;
            }
            else if(x==B[y])
            {
                cntb[y]++;
            }
        }
        tot=0;
        for(int i=1;i<=m;i++)
        {
            scanf("%d %d %d",&x,&y,&z);
            if(B[z]!=0)
            {
                san[++tot]=(node){B[z],x,y,cntb[z]};
            }
        }

        sort(san+1,san+tot+1,cmp);
        int tot1=0;
        for(int i=1;i<=tot;i++)
        {
            if(buxiangdeng(i,i-1))
            {
                tmp[++tot1]=san[i];
            }
            else
            {
                tmp[tot1].cnt+=san[i].cnt;
            }
        }

        for(int i=1;i<=tot1;i++)
        {
            san[i]=tmp[i];
        }
        tot=tot1;
        /*
        for(int i=1;i<=tot;i++)
        {
            printf("(%d %d %d) cnt %d \n",san[i].a,san[i].c,san[i].d,san[i].cnt);
        }
        */

        ll ans=0;
        for(int i=1;i<=tot;i++)
        {
            int c,d;
            c=san[i].c; d=san[i].d;
            ll cnt=ask(c-1,d-1,1000,1000);
            //printf("** cnt %lld\n",cnt);
            if(cnt==0)
            {
                ans+=san[i].cnt;
            }
            add(c,d,1ll*san[i].cnt);
        }

        printf("Case #%d: %d\n",++kk,ans);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值