[BZOJ4419][SHOI2013]发微博

考虑要让复杂度和操作数同阶,每次删除时统计下与对方成为好友的时刻到现在对方一共发了多少条微博即可,set维护,最后将所有仍为好友的全部处理掉。

考虑不用set,操作倒序即可,类似差分思想。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #define rep(i,l,r) for (int i=(l); i<=(r); i++)
 6 typedef long long ll;
 7 using namespace std;
 8 
 9 const int N=500010;
10 char ch;
11 int n,m,ans[N],num[N];
12 struct P{ int op,x,y; }a[N];
13 
14 int main(){
15     freopen("bzoj4419.in","r",stdin);
16     freopen("bzoj4419.out","w",stdout);
17     scanf("%d%d",&n,&m);
18     rep(i,1,m){
19         scanf(" %c",&ch); scanf("%d",&a[i].x);
20         if (ch=='!') a[i].op=1;
21         else{
22             if (ch=='+') a[i].op=2; else a[i].op=3;
23             scanf("%d",&a[i].y);
24         }
25     }
26     for (int i=m; i; i--){
27         if (a[i].op==1) num[a[i].x]++;
28         if (a[i].op==2) ans[a[i].x]+=num[a[i].y],ans[a[i].y]+=num[a[i].x];
29         if (a[i].op==3) ans[a[i].x]-=num[a[i].y],ans[a[i].y]-=num[a[i].x];
30     }
31     rep(i,1,n) printf("%d ",ans[i]);
32     return 0;
33 }

 

posted @ 2018-11-10 21:32  HocRiser  阅读(193)  评论(0编辑  收藏  举报