BZOJ4419: [Shoi2013]发微博(洛谷P3998)

292 篇文章 1 订阅
281 篇文章 1 订阅

STL

BZOJ题目传送门
洛谷题目传送门

数据结构蒙蔽了我的双眼。

对每个人用一个set维护当前和他成为好友的人,并记录他发的微博数。那么对于两个人 x , y x,y x,y x x x y y y的贡献即为从成为好友到解除好友中 x x x发的微博数量, y y y同理。所以减一减答案就出来了。

注意最后结束后还要算一遍。因为有些人还没有解除好友。

代码:

#include<set>
#include<cctype>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 200005
#define F inline
using namespace std;
int n,m,k,a[N],ans[N];
set <int> s[N];
set <int>::iterator it;
F char readc(){
	static char buf[100000],*l=buf,*r=buf;
	if (l==r) r=(l=buf)+fread(buf,1,100000,stdin);
	return l==r?EOF:*l++;
}
#define pd(x) (x=='!'||x=='+'||x=='-')
F int _read(){
	int x=0; char ch=readc();
	while (!isdigit(ch)&&!pd(ch)) ch=readc();
	if (pd(ch)) return ch;
	while (isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=readc();
	return x;
}
int main(){
	for (n=_read(),m=_read();m;m--){
		int f=_read(),x=_read(),y;
		if (f=='!'){ a[x]++; continue; } y=_read();
		if (f=='-') ans[x]+=a[y],s[x].erase(y),ans[y]+=a[x],s[y].erase(x);
		else ans[x]-=a[y],s[x].insert(y),ans[y]-=a[x],s[y].insert(x);
	}
	for (int i=1;i<=n;i++)
		for (it=s[i].begin();it!=s[i].end();it++) ans[i]+=a[*it];
	for (int i=1;i<n;i++) printf("%d ",ans[i]);
	return printf("%d",ans[n]),0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值