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

相关文章

剑指offer(C++)--数组中只出现一次的数字

题目 一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。 class Solution {public:void FindNumsAppearOnce(vector<int> data,int* num1,int *num2) {int len = data.size();if(len<2)return;int one = 0;for(int i

IOS 数组去重的几种方式

本来只知道NSSet和KeyValues的。今天又新学了几种方式 还有就是和同事学的一种方式 外层循环从0开始遍历,内层从最后一个元素开始遍历 for(int i=0;i<index;i++){  for(int j=index-1;j>i;j-- ){ } }

Java基础(二)——数组,方法,方法重载

个人简介 👀个人主页: 前端杂货铺 ⚡开源项目: rich-vue3 (基于 Vue3 + TS + Pinia + Element Plus + Spring全家桶 + MySQL) 🙋‍♂️学习方向: 主攻前端方向,正逐渐往全干发展 📃个人状态: 研发工程师,现效力于中国工业软件事业 🚀人生格言: 积跬步至千里,积小流成江海 🥇推荐学习:🍖开源 rich-vue3 🍍前端面试

上海邀请赛 A题目 HDU 5236(dp)

先求出没有ctrl+s的时候构造长度为i的期望f[i] 。然后枚举保存的次数,求出最小即可。 #include<cstdio>#include<cstdio>#include<cmath>#include<queue>#include<stack>#include<string>#include<cstring>#include<iostream>#include<map>

poj 3882(Stammering Aliens) 后缀数组 或者 hash

后缀数组:  构建后缀数组,注意要在字符串莫末尾加上一个没出现过的字符。然后可以2分或者直接扫描,直接扫描需要用单调队列来维护 VIEW CODE #include<cstdio>#include<algorithm>#include<iostream>#include<cmath>#include<queue>#include<stack>#include<string

poj 3294(Life Forms) 2分+ 后缀数组

我曾用字符串hash写,但是超时了。只能用后最数组了。大致思路:用不同的符号吧字符串连接起来,构建后缀数组,然后2分答案,依次扫描后缀数组,看是否瞒住条件。 VIEW CODE #include<cstdio>#include<vector>#include<cmath>#include<algorithm>#include<cstring>#include<cassert>#

hdu 2586 树上点对最近距离 (lca)

,只要知道dis[i][j]=dis[i][root]+dis[j][root]-2*dis[Lca(i,j)][root].   其中root为树的根节点,LCA(i,j)为i,j的最近公共祖先。 所以我们先把所有的询问储存下来,然后离线直接查询。复杂度是o(n+q)的。 VIE #include<cstdio>#include<algorithm>#include<i

C语言函数参数--数组长度

int read_column_numbers(int columns[], int max){} 在函数声明的数组参数中,并未指定数组的长度。这种格式是OK的,因为无论调用函数的程序传递给它的数组参数的长度是多少,这个函数都将照收不误。 这是一个伟大的特性,它允许单个函数操纵任意长度的一维数组。 这个特性不利的一面是函数没法知道该数组的长度。如果确实需要数组的长度,它的值必须作为一个单独的

从JavaScript 数组去重看兼容性问题,及性能优化(摘自玉伯博客)

缘由 JavaScript 数组去重经常出现在前端招聘的笔试题里,比如: 有数组 var arr = ['a', 'b', 'c', '1', 0, 'c', 1, '', 1, 0],请用 JavaScript 实现去重函数 unqiue,使得 unique(arr) 返回 ['a', 'b', 'c', '1', 0, 1, ''] 作为笔试题,考点有二: 正确。别小看这个考点

【Java】ArrayListString转化为String数组问题

Java的容器类Collections中toArray()方法,可以把诸如ArrayList<String>的动态数组、不定长转化静态数组、定长数组String[] 但是,如下的转化方式是错误的。 [java]  view plain copy String[] strArray = (String[]) arrayList.toArray();   如果这样执行会导致