CodeForces669E:Little Artem and Time Machine(CDQ分治)(或者用map+树状数组优美地解决)
Little Artem has invented a time machine! He could go anywhere in time, but all his thoughts of course are with computer science. He wants to apply this time machine to a well-known data structure: multiset.
Artem wants to create a basic multiset of integers. He wants these structure to support operations of three types:
- Add integer to the multiset. Note that the difference between set and multiset is that multiset may store several instances of one integer.
- Remove integer from the multiset. Only one instance of this integer is removed. Artem doesn't want to handle any exceptions, so he assumes that every time remove operation is called, that integer is presented in the multiset.
- Count the number of instances of the given integer that are stored in the multiset.
But what about time machine? Artem doesn't simply apply operations to the multiset one by one, he now travels to different moments of time and apply his operation there. Consider the following example.
- First Artem adds integer 5 to the multiset at the 1-st moment of time.
- Then Artem adds integer 3 to the multiset at the moment 5.
- Then Artem asks how many 5 are there in the multiset at moment 6. The answer is 1.
- Then Artem returns back in time and asks how many integers 3 are there in the set at moment 4. Since 3 was added only at moment 5, the number of integers 3 at moment 4 equals to 0.
- Then Artem goes back in time again and removes 5 from the multiset at moment 3.
- Finally Artyom asks at moment 7 how many integers 5 are there in the set. The result is 0, since we have removed 5 at the moment 3.
Note that Artem dislikes exceptions so much that he assures that after each change he makes all delete operations are applied only to element that is present in the multiset. The answer to the query of the third type is computed at the moment Artem makes the corresponding query and are not affected in any way by future changes he makes.
Help Artem implement time travellers multiset.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of Artem's queries.
Then follow n lines with queries descriptions. Each of them contains three integers ai, ti and xi (1 ≤ ai ≤ 3, 1 ≤ ti, xi ≤ 109) — type of the query, moment of time Artem travels to in order to execute this query and the value of the query itself, respectively. It's guaranteed that all moments of time are distinct and that after each operation is applied all operations of the first and second types are consistent.
Output
For each ask operation output the number of instances of integer being queried at the given moment of time.
Examples
6
1 1 5
3 5 5
1 2 5
3 6 5
2 3 5
3 7 5
1
2
1
3
1 1 1
2 2 1
3 3 1
0
题意:按顺序给定一些操作或者询问。操作:在集合里加元素,删元素,然后有执行时间。 或询问。
思路:3维偏序,第1维:给出的顺序; 第二维:时间; 第三维:大小。
第一维已经排好序了,第二维也可以手动排序,然后搞定第三维。
复杂度O(NlgNlgN),不过跑起来还挺快的。
(看了一下排行榜,也有人用map+树状数组做的,又短又快,强的啊。
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int b[maxn],ans[maxn],num[maxn],cnt,tN;
struct in{ int id,opt,t,x; }s[maxn];
bool cmp1(in w,in v){ return w.t<v.t ;}
bool cmp2(in w,in v){ return w.id<v.id; }
void solve(int L,int R)
{
if(L==R) return ;
int Mid=(L+R)/;
solve(L,Mid);
solve(Mid+,R);
sort(s+L,s+R+,cmp1); //按第二偏序(时间)排序
for(int i=L;i<=R;i++){
if(s[i].id<=Mid){ //如果在左边,则累加对右边的影响
int pos=lower_bound(b+,b+tN+,s[i].x)-b;
if(s[i].opt==) num[pos]++;
if(s[i].opt==) num[pos]--;
}
else { //如果在右边,则累加答案
int pos=lower_bound(b+,b+tN+,s[i].x)-b;
if(s[i].opt==) ans[s[i].id]+=num[pos];
}
}
for(int i=L;i<=R;i++){ //删去左边的标记。
if(s[i].id<=Mid){
int pos=lower_bound(b+,b+tN+,s[i].x)-b;
if(s[i].opt==) num[pos]--;
if(s[i].opt==) num[pos]++;
}
}
sort(s+L,s+R+,cmp2);
}
int main()
{
int N,i,j;
scanf("%d",&N);
for(i=;i<=N;i++) scanf("%d%d%d",&s[i].opt,&s[i].t,&s[i].x);
for(i=;i<=N;i++) s[i].id=i, b[i]=s[i].x;
sort(b+,b+N+);
tN=unique(b+,b+N+)-(b+);
solve(,N);
for(i=;i<=N;i++) if(s[i].opt==) printf("%d\n",ans[i]);
return ;
}
CodeForces669E:Little Artem and Time Machine(CDQ分治)(或者用map+树状数组优美地解决)的更多相关文章
- CodeForces 669 E Little Artem and Time Machine CDQ分治
题目传送门 题意:现在有3种操作, 1 t x 在t秒往multiset里面插入一个x 2 t x 在t秒从multiset里面删除一个x 3 t x 在t秒查询multiset里面有多少x 事情是按 ...
- BZOJ3262陌上花开(三维偏序问题(CDQ分治+树状数组))+CDQ分治基本思想
emmmm我能怎么说呢 CDQ分治显然我没法写一篇完整的优秀的博客,因为我自己还不是很明白... 因为这玩意的思想实在是太短了: fateice如是说道: 如果说对于一道题目的离线操作,假设有n个操作 ...
- [APIO2019] [LOJ 3146] 路灯 (cdq分治或树状数组套线段树)
[APIO2019] [LOJ 3146] 路灯 (cdq分治或树状数组套线段树) 题面 略 分析 首先把一组询问(x,y)看成二维平面上的一个点,我们想办法用数据结构维护这个二维平面(注意根据题意这 ...
- BZOJ1176---[Balkan2007]Mokia (CDQ分治 + 树状数组)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1176 CDQ第一题,warush了好久.. CDQ分治推荐论文: 1 <从<C ...
- Hdu4742-Pinball Game 3D(cdq分治+树状数组)
Problem Description RD is a smart boy and excel in pinball game. However, playing common 2D pinball ...
- hdu 5126 stars cdq分治套cdq分治+树状数组
题目链接 给n个操作, 第一种是在x, y, z这个点+1. 第二种询问(x1, y1, z1). (x2, y2, z2)之间的总值. 用一次cdq分治可以将三维变两维, 两次的话就变成一维了, 然 ...
- BZOJ 1176: [Balkan2007]Mokia( CDQ分治 + 树状数组 )
考虑cdq分治, 对于[l, r)递归[l, m), [m, r); 然后计算[l, m)的操作对[m, r)中询问的影响就可以了. 具体就是差分答案+排序+离散化然后树状数组维护.操作数为M的话时间 ...
- hdu_4742_Pinball Game 3D(cdq分治+树状数组)
题目链接:hdu_4742_Pinball Game 3D 题意: 给你n个点,让你求三维的LIS,并且求出有多少种组合能达到LIS. 题解: 求三维的LIS,典型的三维偏序问题,x排序,解决一维,c ...
- hdu_5324_Boring Class(cdq分治+树状数组)
题目链接:hdu_5324_Boring Class 题意: 给出n个二维点对,求LIS长度和编号字典序最小的LIS(x非增,y非减) 题解: dp[i]=max(dp[j]) (i>j,l[i ...
随机推荐
- Leet Code OJ 338. Counting Bits [Difficulty: Medium]
题目: Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate ...
- 分布式服务框架选型:面对Dubbo,阿里巴巴为什么选择了HSF?
转载:http://www.sohu.com/a/141490021_268033 阿里巴巴集团内部使用的分布式服务框架 HSF(High Speed Framework,也有人戏称“好舒服”)已经被 ...
- Sql语言复习
一.创建数据库 创建和打开数据库 注意一点:在新建数据库的时候,一般放置数据文件与日志文件的位置,需要提前建立文件夹,不然会报错. 一般主数据文件,我们以.mdf结尾,次数据文件用.ndf结尾.对于日 ...
- Solaris文件系统管理
不同的操作系统使用不同类型的文件系统 1.文件(管理)系统:是用来对文件和目录进行管理.控制的数据结构的总称. Windows当中的文件系统: ntfs ,fat32 ,fat64 Solaris 当 ...
- vim调试
首先,想调试一个程序的话,输入以下命令: guest-djjtew@ubuntu:~$ python3 -m pdb 1.py 这时候就停止了,等待着你的输入,然后输入"l"的话, ...
- 服务管理-Nginx
nginx优势 select,epoll模型 对于一次IO访问(以read举例),数据会先被拷贝到操作系统内核的缓冲区中,然后才会从操作系统内核的缓冲区拷贝到应用程序的地址空间.所以说.当一个read ...
- C# 之 集合ArrayList
.NET Framework提供了用于数据存储和检索的专用类,这些类统称集合. 这些类提供对堆栈.队列.列表和哈希表的支持.大多数集合类实现系统的接口.以下我们主要来讲一下ArrayList. ...
- 转_Greenplum 数据库安装部署(生产环境)
Greenplum 数据库安装部署(生产环境) 硬件配置: 16 台 IBM X3650, 节点配置:CPU 2 * 8core,内存 128GB,硬盘 16 * 900GB,万兆网卡. 万兆交换机. ...
- XML解析PULL
解析xml是很经常使用的操作,除了SAX和DOM两种最经常使用的解析xml外,Pull解析器解析XML文件. 在Android的源代码中大量的使用Pull解析.pull不仅更加的面相对象,并且使用速度 ...
- 2016/07/07 mymps(蚂蚁分类信息/地方门户系统)
mymps(蚂蚁分类信息/地方门户系统)是一款基于php mysql的建站系统.为在各种服务器上架设分类信息以及地方门户网站提供完美的解决方案. mymps,整站生成静态,拥有世界一流的用户体验,卓越 ...