题目链接

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. JVM参数类型

    java -version看版本号(混合模式) java -Xint -version  解释执行 java -Xcomp -version 编译执行 XX参数是不稳定的用来JVM调优和DeBug B ...

  2. KO ----- 静态资源404问题

    --------------------siwuxie095                                 KO ----- 静态资源 404 问题         在 Spring ...

  3. AudiosessionSetActive

    IOS audiosession 会话控制声音播放 今天遇到一个问题: 当我外部想要关闭声音播放的时候 audiosessionsetActive(false) 居然报错了,但是作用起了  AVAud ...

  4. Fefora 14 源

    默认的源不能用,需要用下边的源路径. [fedora] name=Fedora $releasever - $basearch failovermethod=priority #baseurl=htt ...

  5. Apache Beam是什么?

    Apache Beam 的前世今生 1月10日,Apache软件基金会宣布,Apache Beam成功孵化,成为该基金会的一个新的顶级项目,基于Apache V2许可证开源. 2003年,谷歌发布了著 ...

  6. 【轻松前端之旅】​CSS选择器中的空格与尖括号有何区别?

    CSS选择器中的空格与尖括号有何区别? 例子1: .a .b { margin: 0; } 空格隔开a和b,选择所有后代元素. 例子2: .a>.b { margin: 0; } 尖括号隔开a和 ...

  7. hibernate的Could not execute JDBC batch update错误原因及处理

    http://blog.csdn.net/derpvailzhangfan/article/details/2332795\ 上述问题: 一设置关联 二包含关键字 三 映射文件设置 catalog=“ ...

  8. JSP请求重定向与请求转发的区别

    请求重定向 客户端行为,response.sendRedirect(),从本质上讲等同于两次请求,前一次请求对象不会保存,地址栏URL会改变: 请求转发 服务器行为,request.getReques ...

  9. Element.querySelector和Element.querySelectorAll和jQuery(element).find(selector)选择器的区别

    <divid="test1"> <a href="http://www.hujuntao.com/">设计蜂巢</a> &l ...

  10. html样式板

    一.bootstrap 二.element 三.iconfont图标 四.font awesome图标