首页 技术 正文
技术 2022年11月19日
0 收藏 848 点赞 3,387 浏览 2912 个字

E. Little Artem and Time Machine

题目连接:

http://www.codeforces.com/contest/669/problem/E

Description

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.

Sample Input

6

1 1 5

3 5 5

1 2 5

3 6 5

2 3 5

3 7 5

Sample Output

1

2

1

Hint

题意

有三个操作

1 x y,在第x秒插入一个y

2 x y,在第x秒移走一个y

3 x y, 问第x秒有多少个y

题解:

裸的可持久化treap,可以直接莽一波……

但是这道题可以用树状数组做,时间复杂度是nlog(1e9)log(1e9)的

所以感觉还是蛮快的

空间是nlogn的

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
map<int,int>H[maxn];
map<int,int>Vis;
int tot = 0;
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int t,int y)
{
for(int i=t;i<1e9+5;i+=lowbit(i))
H[x][i]+=y;
}
int get(int x,int t)
{
int ans = 0;
for(int i=t;i;i-=lowbit(i))
ans+=H[x][i];
return ans;
}
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
int op,x,y;
scanf("%d%d%d",&op,&x,&y);
if(op==1&&!Vis[y])Vis[y]=++tot;
if(op==1)update(Vis[y],x,1);
if(op==2)update(Vis[y],x,-1);
if(op==3)printf("%d\n",get(Vis[y],x));
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,401
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,813
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,896