题目大意:
有n个人,m个组,接下来会进行q次操作:
1.第x个人 加入 第y个组
2.第x个人 退出 第y个组
3.第x个人 对 第y个组内 所有的人(x除外)发送一条信息
最后输出每个人都收到了多少条信息
考场上这道题队友直接set差分秒了...然而我并没懂。
事后补题给了另外一种二分的想法。
Guang巨给了虚点的想法(真好,又有新的东西要补了quq
思路:开出map和vector维护每个人进每个组的时间和每个组发消息的时间。
然后统计的时候放在这个人退出组的时候统计(如果结束了还没退出就强制让他退出
统计的时候在退出的组中二分,因为我们记录了这个人进该组的时间,那么在这个时间后面该组推送的消息的时间是单调的,也都记录了,二分查一下就是这个人在这个组收到的短信条数。
代码中最后那部分的复杂度是n+mlogm的
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=2e5+100;
typedef long long LL;
map<LL,LL>map1[maxn];
vector<LL>v[100100];
LL ans[maxn];
int main(void)
{
cin.tie(0);std::ios::sync_with_stdio(false);
LL n,m,s;cin>>n>>m>>s;
for(LL i=1;i<=s;i++){
LL t,x,y;cin>>t>>x>>y;
if(t==1){
map1[x][y]=i;///xth学生在i时刻加入y社团
}
else if(t==2){
LL p=v[y].end()-lower_bound(v[y].begin(),v[y].end(),map1[x][y]);///找在xth加入y社团后,y社团发了多少条消息,即x在这个社团收到的消息
ans[x]+=p;
map1[x].erase(y);
}
else if(t==3){
ans[x]--;///提前减去一个,自己发自己的影响
v[y].push_back(i);
}
}
///让所有没有退出的人全部退出统计每个人收到的短信
for(LL i=1;i<=m;i++){
for(auto j:map1[i]){
LL p=v[j.first].end()-lower_bound(v[j.first].begin(),v[j.first].end(),j.second);
ans[i]+=p;
}
}
for(LL i=1;i<=m;i++){
cout<<ans[i]<<endl;
}
return 0;
}