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:

  1. Add integer to the multiset. Note that the difference between set and multiset is that multiset may store several instances of one integer.
  2. 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.
  3. 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 aiti 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

Input
6
1 1 5
3 5 5
1 2 5
3 6 5
2 3 5
3 7 5
Output
1
2
1
Input
3
1 1 1
2 2 1
3 3 1
Output
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+树状数组优美地解决)的更多相关文章

  1. 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 事情是按 ...

  2. BZOJ3262陌上花开(三维偏序问题(CDQ分治+树状数组))+CDQ分治基本思想

    emmmm我能怎么说呢 CDQ分治显然我没法写一篇完整的优秀的博客,因为我自己还不是很明白... 因为这玩意的思想实在是太短了: fateice如是说道: 如果说对于一道题目的离线操作,假设有n个操作 ...

  3. [APIO2019] [LOJ 3146] 路灯 (cdq分治或树状数组套线段树)

    [APIO2019] [LOJ 3146] 路灯 (cdq分治或树状数组套线段树) 题面 略 分析 首先把一组询问(x,y)看成二维平面上的一个点,我们想办法用数据结构维护这个二维平面(注意根据题意这 ...

  4. BZOJ1176---[Balkan2007]Mokia (CDQ分治 + 树状数组)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1176 CDQ第一题,warush了好久.. CDQ分治推荐论文: 1 <从<C ...

  5. Hdu4742-Pinball Game 3D(cdq分治+树状数组)

    Problem Description RD is a smart boy and excel in pinball game. However, playing common 2D pinball ...

  6. hdu 5126 stars cdq分治套cdq分治+树状数组

    题目链接 给n个操作, 第一种是在x, y, z这个点+1. 第二种询问(x1, y1, z1). (x2, y2, z2)之间的总值. 用一次cdq分治可以将三维变两维, 两次的话就变成一维了, 然 ...

  7. BZOJ 1176: [Balkan2007]Mokia( CDQ分治 + 树状数组 )

    考虑cdq分治, 对于[l, r)递归[l, m), [m, r); 然后计算[l, m)的操作对[m, r)中询问的影响就可以了. 具体就是差分答案+排序+离散化然后树状数组维护.操作数为M的话时间 ...

  8. hdu_4742_Pinball Game 3D(cdq分治+树状数组)

    题目链接:hdu_4742_Pinball Game 3D 题意: 给你n个点,让你求三维的LIS,并且求出有多少种组合能达到LIS. 题解: 求三维的LIS,典型的三维偏序问题,x排序,解决一维,c ...

  9. hdu_5324_Boring Class(cdq分治+树状数组)

    题目链接:hdu_5324_Boring Class 题意: 给出n个二维点对,求LIS长度和编号字典序最小的LIS(x非增,y非减) 题解: dp[i]=max(dp[j]) (i>j,l[i ...

随机推荐

  1. dubbo开发者指南

    开发者指南 参与 流程 任务 版本管理 源码构建 框架设计 整体设计 模块分包 依赖关系 调用链 暴露服务时序 引用服务时序 领域模型 基本原则 扩展点加载 扩展点配置 扩展点自动包装 扩展点自动装配 ...

  2. 1、CRM2011编程实战——清空指定页签以下的全部选项,并对页签以下的指定控件进行操作

    需求:当页面载入时,"呼叫编号"保持不变,"任务号"自己主动更新."接报时间"和"发生日期"自己主动设置为当天日期和时间 ...

  3. Flash如何为文字描边

    可以使用墨水瓶工具,但是要先把文字打散(可以打散之后再组合起来)粗细和颜色都可以调,粗细就是笔触,颜色就是前景色(边框颜色)  

  4. spring secrity 一些常用小知识

    1.在JSP页面获取当前登录的用户名的方法 首先引入taglib:<%@ taglib prefix="sec" uri="http://www.springfra ...

  5. Heap &amp; Priority Queue

    Heap & Priority Queue Definition & Description: In computer science/data structures, a prior ...

  6. 【git】强制覆盖本地代码

    [git]强制覆盖本地代码(与git远程仓库保持一致) 2018年04月27日 23:53:57 不才b_d 阅读数:21145   版权声明:本文为博主不才b_d原创文章,未经允许不得转载. || ...

  7. 服务管理-Nginx

    nginx优势 select,epoll模型 对于一次IO访问(以read举例),数据会先被拷贝到操作系统内核的缓冲区中,然后才会从操作系统内核的缓冲区拷贝到应用程序的地址空间.所以说.当一个read ...

  8. vue-导入静态文件

    vue导入静态文件不用像网上说的那么麻烦,其实跟普通Django项目导入类似,vue项目中有一个static文件,将你的静态文件放入到里面,然后引入就好 导入的时候和普通Django程序类似:↓ &l ...

  9. 官方Caffe-windows 配置与示例运行

    http://blog.csdn.net/guoyk1990/article/details/52909864 标签: caffewindows配置训练自己的数据 2016-10-24 13:34 1 ...

  10. THE MARTIAN

    影片的最后一段自白 When I was up there, stranded by myself …… “did I think I was going to die?” Yes, absolute ...