CodeForces 641 E.Little Artem and Time Machine(BIT+map)

256 篇文章 0 订阅
54 篇文章 0 订阅

Description
一个初始为空的可重集,给出n种操作:
1 t x:在t时刻插入一个x
2 t x:在t时刻删除一个x
3 t x:查询t时刻x的数量
Input
第一行一整数n表示操作数,之后n行每行一个操作(1<=n<=1e5,1<=t,x<=1e9)
Output
对于每次查询,输出查询结果
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
Solution
对每个x开一个map用BIT直接怼
Code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define maxn 111111
#define N 1e9+1
#define lowbit(x) (x&(-x))
int n;
map<int,int>m[maxn];
map<int,int>vis;
void update(int pos,int x,int v)
{
    while(x<N)
    {
        m[pos][x]+=v;
        x+=lowbit(x);
    }
}
int sum(int pos,int x)
{
    int ans=0;
    while(x)
    {
        ans+=m[pos][x];
        x-=lowbit(x);
    }
    return ans;
}
int main()
{
    while(~scanf("%d",&n))
    {
        int cnt=0;
        vis.clear();
        while(n--)
        {
            int type,t,x;
            scanf("%d%d%d",&type,&t,&x);
            if(type==1&&!vis[x])
                vis[x]=++cnt,m[cnt].clear();
            if(type==1)update(vis[x],t,1);
            else if(type==2)update(vis[x],t,-1);
            else printf("%d\n",sum(vis[x],t));
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值