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 ...
随机推荐
- win10 安装git
http://jingyan.baidu.com/article/a3a3f811d4cd308da2eb8ad1.html 双击exe安装包,在弹出的安全提示中点击“允许”. 安装向导的头两步都 ...
- [剑指Offer]2.变态跳台阶
题目 一仅仅青蛙一次能够跳上1级台阶,也能够跳上2级--它也能够跳上n级. 求该青蛙跳上一个n级的台阶总共同拥有多少种跳法. 思路 用Fib(n)表示青蛙跳上n阶台阶的跳法数,设定Fib(0) = 1 ...
- 132-pattern(蛮难的)
https://leetcode.com/problems/132-pattern/ 下面是我的做法.后来又看了一个提示: https://discuss.leetcode.com/topic/678 ...
- springMVC前后端分离开发模式下支持跨域请求
1.web.xml中添加cors规则支持(请修改包名) <filter> <filter-name>cors</filter-name> <filter-cl ...
- [Javascript] Await a JavaScript Promise in an async Function with the await Operator
The await operator is used to wait for a promise to settle. It pauses the execution of an async func ...
- 专訪阿里陶辉:大规模分布式系统、高性能server设计经验分享
http://www.csdn.net/article/2014-06-27/2820432 摘要:先后就职于在国内知名的互联网公司,眼下在阿里云弹性计算部门做架构设计与核心模块代码的编写,主要负责云 ...
- pjblog支持QQ、新浪微博一键登录
转载地址: http://www.ruisoftcn.com/blog/article.asp?id=955
- HMM MEMM CRF 差别 联系
声明:本文主要是基于网上的材料做了文字编辑,原创部分甚少.參考资料见最后. 隐马尔可夫模型(Hidden Markov Model.HMM),最大熵马尔可夫模型(Maximum Entropy Mar ...
- Jquery 插件 实例
先说明下应用场景,通过可配项的配置和默认项覆盖,获取指定的需求数据,填充到指定的位置(两个指定其实都是可配的) (function($) { $.fn.extend({ getOneNews: fun ...
- sublime 快捷键 汇总--长期
Ctrl+P 输入当前项目中的文件名,快速搜索文件 Ctrl+G 输入数字跳转到该行代码 Ctrl+R 输入关键字,查找文件中的函数名 Ctrl+: 输入关键字,查找文件中的变量名.属性名等 Ctrl ...