【bzoj1103】【POI2007】【大都市】【meg】【人工栈】
2016-08-17 22:46
465 查看
题目大意
一棵有根树,每次修改一条边或询问跟到一个点的路径上有多少条边没被修改。
解题思路
可以用括号序表示树上的一条路径,访问时间戳+1,离开时间戳-1,修改一条边就是将深度大的点的时间戳都改为0,表示对后面没影响。最恶心的是要打人工栈。
code
#include<set> #include<cmath> #include<cstdio> #include<cstring> #include<algorithm> #define LL long long #define fo(i,j,k) for(int i=j;i<=k;i++) #define fd(i,j,k) for(int i=j;i>=k;i--) using namespace std; int const maxn=250000,maxa=1000000000; int n,m,gra,time,top,to[maxn+10],next[maxn+10],begin[maxn+10],dfn[maxn+10],lea[maxn+10],st[maxn+10],t[maxn*2+10]; void insert(int u,int v){ to[++gra]=v; next[gra]=begin[u]; begin[u]=gra; } void dfs(int s){ st[++top]=s; for(;top;){ if(begin[st[top]])st[top+1]=to[begin[st[top]]],begin[st[top]]=next[begin[st[top]]],top++,dfn[st[top]]=++time; else lea[st[top]]=++time,top--; } } void change(int p,int v){ for(;p<=time;){ t[p]+=v; p+=p&(-p); } } int qury(int p){ int ans=0; for(;p>=1;){ ans+=t[p]; p-=p&(-p); } return ans; } int main(){ freopen("d.in","r",stdin); freopen("d.out","w",stdout); scanf("%d",&n); fo(i,1,n-1){ int u,v;scanf("%d%d",&u,&v); if(u>v)swap(u,v); insert(u,v); } dfs(1); fo(i,2,n){ change(dfn[i],1); change(lea[i],-1); } scanf("%d\n",&m); fo(i,1,n+m-1){ char ch=getchar();int a,b; if(ch=='W'){ scanf("%d\n",&a); printf("%d\n",qury(dfn[a])); } else{ scanf("%d%d\n",&a,&b); change(dfn[b],-1); change(lea[b],1); } } return 0; }
相关文章推荐
- BZOJ1103【POI2007】大都市meg <树上差分+树状数组>
- Hdu 3887 Counting Offspring \ Poj 3321 Apple Tree \BZOJ 1103 [POI2007]大都市meg
- 【BZOJ1103】[POI2007]大都市meg【树链剖分】【线段树】【或 树状数组 + dfs序】
- bzoj 1103: [POI2007]大都市meg【dfs序+树状数组】
- bzoj1103 [POI2007]大都市meg
- bzoj 1103: [POI2007]大都市meg (dfs序)
- [dfs序 树状数组] BZOJ 1103 [POI2007]大都市meg
- 【BZOJ1103】【POI2007】大都市meg
- 【POI2007】【BZOJ1103】大都市meg
- 树状数组【bzoj1103】: [POI2007]大都市meg
- bzoj-1103 [POI2007]大都市meg
- BZOJ 1103 [POI2007]大都市meg
- bzoj1103 [POI2007]大都市meg
- [BZOJ]1103: [POI2007]大都市meg
- 【DFS序+线段树】BZOJ1103 [POI2007]大都市meg
- 数据结构(线段树):BZOJ 1103 [POI2007]大都市meg
- bzoj1103 [POI2007]大都市meg
- Bzoj1103 [POI2007]大都市meg
- BZOJ1103: [POI2007]大都市meg
- bzoj 1103: [POI2007]大都市meg(树链剖分)