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

2024-04-07 00:58

本文主要是介绍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;elsep[++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;
}

这篇关于Triple HDU - 5517 —— 二维树状数组的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/881270

相关文章

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

Java 字符数组转字符串的常用方法

《Java字符数组转字符串的常用方法》文章总结了在Java中将字符数组转换为字符串的几种常用方法,包括使用String构造函数、String.valueOf()方法、StringBuilder以及A... 目录1. 使用String构造函数1.1 基本转换方法1.2 注意事项2. 使用String.valu

JAVA中整型数组、字符串数组、整型数和字符串 的创建与转换的方法

《JAVA中整型数组、字符串数组、整型数和字符串的创建与转换的方法》本文介绍了Java中字符串、字符数组和整型数组的创建方法,以及它们之间的转换方法,还详细讲解了字符串中的一些常用方法,如index... 目录一、字符串、字符数组和整型数组的创建1、字符串的创建方法1.1 通过引用字符数组来创建字符串1.2

vue如何监听对象或者数组某个属性的变化详解

《vue如何监听对象或者数组某个属性的变化详解》这篇文章主要给大家介绍了关于vue如何监听对象或者数组某个属性的变化,在Vue.js中可以通过watch监听属性变化并动态修改其他属性的值,watch通... 目录前言用watch监听深度监听使用计算属性watch和计算属性的区别在vue 3中使用watchE

poj2576(二维背包)

题意:n个人分成两组,两组人数只差小于1 , 并且体重只差最小 对于人数要求恰好装满,对于体重要求尽量多,一开始没做出来,看了下解题,按照自己的感觉写,然后a了 状态转移方程:dp[i][j] = max(dp[i][j],dp[i-1][j-c[k]]+c[k]);其中i表示人数,j表示背包容量,k表示输入的体重的 代码如下: #include<iostream>#include<

hdu2159(二维背包)

这是我的第一道二维背包题,没想到自己一下子就A了,但是代码写的比较乱,下面的代码是我有重新修改的 状态转移:dp[i][j] = max(dp[i][j], dp[i-1][j-c[z]]+v[z]); 其中dp[i][j]表示,打了i个怪物,消耗j的耐力值,所得到的最大经验值 代码如下: #include<iostream>#include<algorithm>#include<

hdu2241(二分+合并数组)

题意:判断是否存在a+b+c = x,a,b,c分别属于集合A,B,C 如果用暴力会超时,所以这里用到了数组合并,将b,c数组合并成d,d数组存的是b,c数组元素的和,然后对d数组进行二分就可以了 代码如下(附注释): #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<que

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO