hdu5517 二维树状数组

题意是给了 n个二元组 m个三元组, 二元组可以和三元组 合并生成3元组,合并条件是<a,b> 与<c,d,e>合并成 <a,c,d> 前提是 b==e,

如果存在组合 uwv 使得u>=a w>=c v>=d 并且uwv和acd不等  就说abc 不是最优的,求问最后又多少个组合是最优的 , 这个组合中是允许重复的

我们对于每个b只取最大的a,然后让这个最大的a去和相应的b,c进行组合,然后对于这样的三元组 为了省去判断和他相等的个数,我们直接将相同的元组合并到一起去,

然后枚举a求在 在矩阵C[b][c]右下边是否存在值如果存在显然这个就不是最优的,用二维树状数组解决这个问题

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <string.h>
using namespace std;
const int maxn=100005;
struct point{
  int a,c,d,nu;
  bool operator <(const point &rhs)const{
      if(a!=rhs.a)return a<rhs.a;
      if(c!=rhs.c)return c<rhs.c;
      return d<rhs.d;
  }
  bool operator ==(const point &rhs)const{
     return a==rhs.a&&c==rhs.c&&d==rhs.d;
  }
}P[maxn];
int B[maxn];
int nu[maxn];
int C[1005][1005];
int Nc,Nd,numofC;
void init()
{
    numofC=Nc=Nd=0;
    memset(B,0,sizeof(B));
    memset(nu,0,sizeof(nu));
    memset(C,0,sizeof(C));
}
int lowbit(int x)
{
    return x&(-x);
}
void add(int c,int d,int val)
{
    for(int i=c; i<=Nc; i+=lowbit(i))
        for(int j=d; j<=Nd; j+=lowbit(j))
           C[i][j]+=val;
}
int sum(int c, int d)
{
    int ans=0;
    for(int i=c; i>0; i-=lowbit(i))
        for(int j=d; j>0; j-=lowbit(j))
          ans+=C[i][j];
    return ans;
}
int main()
{
    int cas;
    scanf("%d",&cas);
    for(int cc=1; cc<=cas; cc++)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        init();
        for(int i=0; i<n; i++)
        {
             int a,b;
             scanf("%d%d",&a,&b);
             if(B[b]<a){ B[b]=a; nu[b]=1;}
             else if(B[b]==a) nu[b]++;
        }
        for(int i=0; i<m; i++)
        {
            int c,d,e;
            scanf("%d%d%d",&c,&d,&e);
            if(nu[e]>0)
            {
                point t;
                t.a=B[e]; t.c=c; t.d=d; t.nu=nu[e];
                P[numofC++]=t;
            }
            Nc=max(c,Nc); Nd=max(Nd,d);
        }
        sort(P,P+numofC);
        int ge=1;
        for(int i=1; i<numofC; i++)
            if(P[i]==P[ge-1])P[ge-1].nu+=P[i].nu;
            else P[ge++]=P[i];
        numofC=ge;
        int ans=0;
        ge=0;
        for(int i=numofC-1; i>=0; i--)
        {
            point t=P[i];
            int s2=sum(t.c-1,Nd);
            int s3=sum(Nc,t.d-1);
            int s4=sum(t.c-1,t.d-1);
            if(ge-s2-s3+s4 == 0){
                ans+=t.nu;
            }
            ge++;
            add(t.c,t.d,1);
        }
        printf("Case #%d: %d\n",cc,ans);

    }
    return 0;
}

时间: 2024-08-08 22:07:21

hdu5517 二维树状数组的相关文章

【二维树状数组】See you~

https://www.bnuoj.com/v3/contest_show.php?cid=9148#problem/F [题意] 给定一个矩阵,每个格子的初始值为1.现在可以对矩阵有四种操作: A x y n1 :给格点(x,y)的值加n1 D x y n1: 给格点(x,y)的值减n1,如果现在格点的值不够n1,把格点置0 M x1 y1 x2 y2:(x1,y1)移动给(x2,y2)n1个 S x1 y1 x2 y2 查询子矩阵的和 [思路] 当然是二维树状数组 但是一定要注意:lowbi

POJ 1195 Mobile phones(二维树状数组)

题目链接:POJ 1195 题意: 给出一个S*S的矩阵(行.列号从1开始),每个元素初始值为0,有两种操作:一种是第X行第Y列元素值加A:另一种是查询给定范围矩阵的所有元素之和(L<=X<=R,B<=Y<=T). 分析: 查询给定范围矩阵的所有元素之和是二维区间和,可以转换为二维前缀和求值.类比一维前缀和求法,二维区间和S(L, B, R, T) = S(1, 1, R, T) - S(1 ,1, L-1, T) - S(1, 1, R, B-1) + S(1, 1, L-1,

POJ 2155 Matrix(二维树状数组,绝对具体)

Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20599   Accepted: 7673 Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row and j-th column. Initially we have A[i, j] = 0 (1

HDU 5465 Clarke and puzzle Nim游戏+二维树状数组

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5465 Clarke and puzzle Accepts: 42 Submissions: 269 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 克拉克是一名人格分裂患者.某一天,有两个克拉克(aa和bb)在玩一个方格游戏. 这个方格是一个n*mn∗m的矩阵,每个格子里有一

HDOJ 4456 Crowd 离散化+二维树状数组

将坐标旋转45度就可以得到正方形,可以用二维树状数组求解... 为了节省内存,提前将树状数组中会被更新的点全都存下来,并离散化 Crowd Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1199    Accepted Submission(s): 282 Problem Description City F in the south

POJ2029:Get Many Persimmon Trees(二维树状数组)

Description Seiji Hayashi had been a professor of the Nisshinkan Samurai School in the domain of Aizu for a long time in the 18th century. In order to reward him for his meritorious career in education, Katanobu Matsudaira, the lord of the domain of

POJ1195 Mobile phones 【二维树状数组】

Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14288   Accepted: 6642 Description Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows. The area is divided into squares. The

MooFest_二维树状数组

Description Every year, Farmer John's N (1 <= N <= 20,000) cows attend "MooFest",a social gathering of cows from around the world. MooFest involves a variety of events including haybale stacking, fence jumping, pin the tail on the farmer,

HDU_4456_二维树状数组

http://acm.hdu.edu.cn/showproblem.php?pid=4456 第一道二维树状数组就这么麻烦,题目要计算的是一个菱形范围内的和,于是可以把原来的坐标系旋转45度,就是求一个正方形范围内的和,这里还涉及到坐标系平移和放大,由于题目数据较大,用了离散化来保存需要处理的点,放在h数组内,对应的二维树状数组存在tree数组内. #include<iostream> #include<cstring> #include<cstdio> #includ