题目链接

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=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(C), as

TOP(C)={⟨a,b,c⟩∈C∣BETTERC(⟨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 (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(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
 
题意:每组数据第一行输入n m  ,第二行输入a1 b1  a2  b2......an  bn,第三行输入c1  d1  e1......cm  dm  em 
        现在定义C=A*B  即{<a,c,d>|<a,b>属于A & <c,d,e>属于B & b==e}
        然后基于C有这样一个运算TOP(C)={<a,c,d>|<a,c,d>属于C & C中不存在<u,v,w>使得 u>=a,v>=c,w>=d,<u,v,w>!=<a,c,d> }
        现在求 TOP(C)中有几个元素?
 
思路:
          

上面是从论坛上截图下来的,我觉得优化的时候,只需要用第一条即可,即:对于二元组(a,b) ,b相同的话只有最大的a值有效,所以对相同的b记录一下最大值的个数

第二条不一定能优化,在极端的数据上,一点都不会优化。经过第一条的优化后,C的大小为1e5,然后用二维树状数组处理O(n)=1e5*log2(1000)*log2(1000)=1e7

实际的数据肯定会比这个复杂度要小。

代码如下:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int N=1e5+;
int a1[N],cnt[N];
int c[][]; struct Node
{
int a,c,d;
int v;
}tr[N];
int cmp(const Node s1,const Node s2)
{
if(s1.a!=s2.a) return s1.a<s2.a;
if(s1.c!=s2.c) return s1.c<s2.c;
return s1.d<s2.d;
}
int lowbit(int x)
{
return x&(-x);
}
int query(int x)
{
int ans=;
int i=tr[x].c;
while(i<)
{
int j=tr[x].d;
while(j<)
{
ans+=c[i][j];
j+=lowbit(j);
}
i+=lowbit(i);
}
return ans;
}
void update(int x)
{
int i=tr[x].c;
while(i>)
{
int j=tr[x].d;
while(j>)
{
c[i][j]++;
j-=lowbit(j);
}
i-=lowbit(i);
}
}
int main()
{
///cout << "Hello world!" << endl;
int t,Case=1;
cin>>t;
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
memset(a1,-,sizeof(a1));
memset(c,,sizeof(c));
for(int i=;i<=n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
if(a1[b]<a){
a1[b]=a;
cnt[b]=;
}
else if(a1[b]==a) cnt[b]++;
}
int num=;
for(int i=;i<=m;i++)
{
int c,d,e;
scanf("%d%d%d",&c,&d,&e);
if(a1[e]==-) continue;
tr[num].a=a1[e];
tr[num].c=c;
tr[num].d=d;
tr[num++].v=cnt[e];
}
sort(tr,tr+num,cmp);
int flag=;
int k=;
for(int i=;i<num;i++)
{
if(tr[i].a==tr[k].a&&tr[i].c==tr[k].c&&tr[i].d==tr[k].d)
{
tr[k].v+=tr[i].v;
}
else{
k++;
flag=;
tr[k].a=tr[i].a;
tr[k].c=tr[i].c;
tr[k].d=tr[i].d;
tr[k].v=tr[i].v;
}
}
long long ans=;
if(flag) ///防止 1 1 (1,1) (1,1,2) 这样的数据(但是HDU上没这样的数据);
for(int i=k;i>=;i--)
{
if(!query(i)) ans+=(long long)tr[i].v;
update(i);
}
printf("Case #%d: %lld\n",Case++,ans);
}
return ;
}

HDU 5517---Triple(二维树状数组)的更多相关文章

  1. hdu 5517 Triple(二维树状数组)

    Triple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  2. HDU 5517 【二维树状数组///三维偏序问题】

    题目链接:[http://acm.split.hdu.edu.cn/showproblem.php?pid=5517] 题意:定义multi_set A<a , d>,B<c , d ...

  3. HDU 4456(二维树状数组+坐标转换)

    题目链接:Problem - 4456 看别人叙述看的心烦,于是我自己画了一张图. 上图. 上代码 #include <iostream> #include <cstdio> ...

  4. 【 HDU - 4456 】Crowd (二维树状数组、cdq分治)

    BUPT2017 wintertraining(15) #5A HDU 4456 题意 给你一个n行n列的格子,一开始每个格子值都是0.有M个操作,p=1为第一种操作,给格子(x,y)增加z.p=2为 ...

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

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5465 Clarke and puzzle  Accepts: 42  Submissions: 26 ...

  6. hdu 2642 二维树状数组 单点更新区间查询 模板水题

    Stars Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/65536 K (Java/Others) Total Subm ...

  7. hdu 2642二维树状数组 单点更新区间查询 模板题

    二维树状数组 单点更新区间查询 模板 从零开始借鉴http://www.2cto.com/kf/201307/227488.html #include<stdio.h> #include& ...

  8. HDU1559 最大子矩阵 (二维树状数组)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1559 最大子矩阵 Time Limit: 30000/10000 MS (Java/Others)  ...

  9. hdu6078 Wavel Sequence dp+二维树状数组

    //#pragma comment(linker, "/STACK:102400000,102400000") /** 题目:hdu6078 Wavel Sequence 链接:h ...

随机推荐

  1. E. Segment Sum(数位dp)

    题意:求一个区间内满足所有数位不同数字个数小于K的数字总和.比如:k=2   1,2,3所有数位的不同数字的个数为1满足,但是123数位上有三个不同的数字,即123不满足. 我们可以使用一个二进制的数 ...

  2. 视觉和imu融合的算法研究

    融合方式概述 同SLAM发展过程类似,视觉融合IMU问题也可以分成基于滤波和基于优化两大类. 同时按照是否把图像特征信息加入状态向量来进行分类,可以分为松耦合和紧耦合两大类. 一.基于滤波的融合算法 ...

  3. 通过类名或者jar名查询所在jar包

    一.问题 例如我想查看一下FilterSecurityInterceptor的源码,但是我不知道它在maven依赖中的哪个jar包中 二.解决方案 http://www.findmaven.net/ ...

  4. xib中的label加边框

    选中xib中的label,在右边栏的第三个标签页中第三项是User Defined Runtime Attributes 添加一个keyPath,keyPath值为layer.borderWidth, ...

  5. 用nodejs搭建类似于C++的服务器后台.类似网易pomelo

    实际的情况,用nodejs跑业务,非常的快,只要用好其无阻塞和回调这两点,处理速度真的是杠杠的. 从年初开始,我用nodejs搭建了类似C++的服务器后台,也想和做同样的事情的朋友分享,本服务平台因为 ...

  6. C#实现局部峰值查找,功能对应Matlab中的findpeaks.m(转)

    相关算法的原理参考Ronny,地址:图像分析:投影曲线的波峰查找,这里感谢下原作者. 参照C++的代码实现,我用C#翻译了下,其实原理也很简单的,下面放相关实现代码: private double[] ...

  7. 【Git】 GitLab配置优化及汉化

    GitLab配置 1.修改GitLab绑定的域名 a.修改/etc/gitlab/gitlab.rb配置文件,修改成自己的域名 external_url 'http://gitlab.example. ...

  8. drf6 权限和频率控制组件

    对某件事情决策的范围和程度,我们叫做权限,权限是我们在项目开发中非常常用到的. DRF框架给我们提供的权限组件 权限组件 之前DRF的版本和认证,知道了权限和频率跟版本认证都是在initial方法里初 ...

  9. UVaLive 5760 Alice and Bob (博弈 + 记忆化搜索)

    题意:有 n 堆石子,有两种操作,一种是从一堆中拿走一个,另一种是把两堆合并起来,Alice 先拿,谁不能拿了谁输,问谁胜. 析:某些堆石子数量为 1 是特殊,石子数量大于 1 个的都合并起来,再拿, ...

  10. PCL-Kinfu编译手册

    1:配置要求 硬件 Win7-62bit 显卡需要compute Capability >=2.0 可以从https://developer.nvidia.com/cuda-gpus 中查找 实 ...