题目链接

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. 数据库类型空间效率探索(五)- decimal/float/double/varchar

    以下测试为userinfo增加一列,列类型分别为decimal.float.double.varchar.由于innodb不支持optimize,所以每次测试,都会删除表test.userinfo,重 ...

  2. win7 win10下80端口被System进程占用的解决方法

    用如下方法可以解决System进程占用80端口的问题:打开RegEdit:开始-运行-输入regedit-调出注册表找到HKEY_LOCAL_MACHINE\SYSTEM\CurrentControl ...

  3. 使用Pyquery+selenium抓取淘宝商品信息

    配置文件,配置好数据库名称,表名称,要搜索的产品类目,要爬取的页数 MONGO_URL = 'localhost' MONGO_DB = 'taobao' MONGO_TABLE = 'phone' ...

  4. io.netty.resolver.dns.DnsNameResolverContext

    java.net.UnknownHostException: failed to resolve 'xxx.com' after 3 queries at io.netty.resolver.dns. ...

  5. OS history

    UNIX的诞生   1965年时,贝尔实验室(Bell Labs)加入一项由奇异电子(General Electric)和麻省理工学院(MIT)合作的计划:该计划要建立一套多使用者.多任务.多层次(m ...

  6. vba文件对比并高亮显示

    每月月底要和人事要离职人员名单,并账号列表里删除已经离职人员的账号,如下代码通过将账号列表与人事发来的离职清单进行对比,高亮找出离职人员的账号,并进行删除. Sub DeleteMain() Dim ...

  7. CentOS 7 Redis 内网 安装 卸载

    # 不能连接外网, 安装Redis服务器的过程 https://redis.io/download (官网下载安装包, 最新版) redis-*.tar.gz 放在安装目录/usr/local/red ...

  8. Linux 6上使用UDEV绑定共享存储

    1.硬盘的查看方式 [root@cl6-11gr2-rac1 ~]# ls -ltr /dev/sd* brw-rw----. 1 root disk 8, 48 8月 16 13:34 /dev/s ...

  9. Maven学习 二 Maven环境搭建

    第一步:下载Maven并解压 注意选择镜像地址,选择国内的会快点 解压目录 Maven目录分析 bin:含有mvn运行的脚本 boot:含有plexus-classworlds类加载器框架 conf: ...

  10. Alpha项目冲刺

    一.团队成员 学号 姓名 211606361 何承华(队长) 211606356 陈宇 211606360 丁培辉 211606333 温志铭 211606343 杨宇潇 211606391 张主强 ...