C:Set — STL

C:Set

总时间限制: 5000ms 内存限制: 100000kB

描述

现有一整数集(允许有重复元素),初始为空。我们定义如下操作:
add x 把x加入集合
del x 把集合中所有与x相等的元素删除
ask x 对集合中元素x的情况询问
对每种操作,我们要求进行如下输出。
add 输出操作后集合中x的个数
del 输出操作前集合中x的个数
ask 先输出0或1表示x是否曾被加入集合(0表示不曾加入),再输出当前集合中x的个数,中间用空格格开。

输入

第一行是一个整数n,表示命令数。0<=n<=100000。
后面n行命令,如Description中所述。

输出

共n行,每行按要求输出。

样例输入

7
add 1
add 1
ask 1
ask 2
del 2
del 1
ask 1

样例输出

1
2
1 2
0 0
0
2
1 0

提示

Please use STL’s set and multiset to finish the task

set模板练习
注意需要使用multiset
要求用set,map记录是否出现过多妙啊(

#include<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define inf 1000000007
#define ll long long
#define N 10010
inline int rd()
{
    int x=0,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;
}
int n;
char op[10];
multiset<int>a,ra;
int main()
{
    n=rd();
    int x,y;
    while(n--){
        scanf("%s",op);
        if(op[1]=='d'){//add
            x=rd();
            a.insert(x);ra.insert(x);
            printf("%lu\n",a.count(x));
        }else if(op[1]=='s'){//ask
            x=rd();
            printf("%d %lu\n",(ra.find(x)==ra.end()?0:1),a.count(x));
        }else{//del
            x=rd();
            printf("%lu\n",a.count(x));
            a.erase(a.lower_bound(x),a.upper_bound(x));
        }
    }
    return 0;
}

评论

还没有任何评论,你来说两句吧

发表评论

衫小寨 出品