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. win10拷贝文件卡顿的问题-竟然是winrar搞的

    win10拷贝文件卡顿的问题-竟然是winrar搞的 学习了: http://www.w10zj.com/Win10xy/Win10xf_3378.html 没想到你竟然是这样的WinRAR 去除了s ...

  2. 【IE】IE对line-height 失效的的解决方案

    微软的IE9 + Extjs3.1 确实头疼.在使用了line-height:20px 的Tree的样式,可是一直没有生效, 以下给出3中解决方式: 方案1.加padding-top: <div ...

  3. 最全的HTTP头部信息分析

    HTTP 头部解释 1. Accept:告诉WEB服务器自己接受什么介质类型,*/* 表示任何类型,type/* 表示该类型下的所有子类型,type/sub-type. 2. Accept-Chars ...

  4. 小谈Vim打开文件开头的&lt;feff&gt;

    在本地Windows机上开发的PHP程序上传到linuxserver上后,通过浏览器訪问对应接口.发现返回的数据前多了一个莫名的字符'-',甚为不解.之后通过网络抓包的方式,查看到接口返回数据前多了 ...

  5. mysql服务停止

    mysql链接方式分为 tcp链接和 sock链接,   你刚才看到服务停止了还能链接 那种会员是 sock的会话模式   所以需要把所有链接mysql的进程结束掉,才能启动起来的   windows ...

  6. 【Sprint3冲刺之前】TDzhushou软件项目测试计划书

    TDzhushou软件测试计划文档 文档编号:2014-5-8 产品版本:1.1 产品名称:TDzhushou 文 档 作 者: 解凤娇       日期:2014-5-4 软件测试计划 目录 第一章 ...

  7. 生产追溯系统-Wifi+传感器,实现计数器以及监控机器是否停止

    物联网听上去是一个高大上的词儿,还有什么大数据.云.智能制造等等,今天我也往这方面稍微靠一靠,这篇文章主要介绍的是通过 wifi 模块与传感器组合,实现感应计数器,应用场景主要如下: 1.统计 SMT ...

  8. IOS UIWebView 随记

    UIWebView中加载的网页尺寸太大,如何让网页适应屏幕大小 webview.scalesPageToFit = YES;

  9. Nexus 5更新 Android5.0 失败解决方法

    Android 5.0最终推出了正式版,今天也及时刷到了Android5.0 (LRX21O),官方链接:https://developers.google.com/android/nexus/ima ...

  10. (转载)JavaScript递归查询 json 树 父子节点

    在Json中知道某个属性名,想要确定该属性在Json树具体的节点,然后进行操作还是很麻烦的 可以用以下方法找到该属性所在的节点,和父节点 <!DOCTYPE html> <html ...