CF641E Little Artem and Time Machine(时间离散化+平衡树套树状数组)

题意:

一个初始为空的可重集,给出n种操作:
1 t x:在t时刻插入一个x
2 t x:在t时刻删除一个x
3 t x:查询t时刻x的数量

题解:

考虑到时间和x的范围都是1e9,但时间只有先后关系,可以对时间进行离散化,然后用树状数组套数据结构处理,这里的数据结构支持快速查询指定数的出现次数即可。

 /*
 *author: zlc
 *zucc_acm_lab
 *just do it
 */
#include<bits/stdc++.h> 
using namespace std;
typedef long long ll;
const double pi=acos(-1.0);
const double eps=1e-6;
const int mod=1e9+7;
const int inf=1e9;
const int maxn=1e5+100;
inline int read () {int x=0;int f=1;char ch=getchar();while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}while (ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();}return x*f;}
ll qpow (ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
map<int,int> c[maxn];
int lowbit (int x) {
    return x&-x;
}
void up (int x,int p,int v) {
    for (int i=p;i<maxn;i+=lowbit(i)) c[i][x]+=v;
}
int getsum (int x,int p) {
    int ans=0;
    for (int i=p;i;i-=lowbit(i)) ans+=c[i][x];
    return ans;
}
int n;
struct query {
    int op;
    int t;
    int x;
}q[maxn];
int T[maxn];
int tot;
int main () {
    n=read();
    for (int i=1;i<=n;i++) {
        q[i].op=read();
        q[i].t=read();
        q[i].x=read();
        T[++tot]=q[i].t;
    }
    sort(T+1,T+n+1);
    int m=unique(T+1,T+n+1)-T-1;
    for (int i=1;i<=n;i++) q[i].t=upper_bound(T+1,T+m+1,q[i].t)-T;
    for (int i=1;i<=n;i++) {
        if (q[i].op==1)
            up(q[i].x,q[i].t,1);
        if (q[i].op==2)
            up(q[i].x,q[i].t,-1);
        if (q[i].op==3)
            printf("%d
",getsum(q[i].x,q[i].t));
    }
}
原文地址:https://www.cnblogs.com/zhanglichen/p/13652998.html