【bzoj1103】【POI2007】【大都市】【meg】【人工栈】

题目大意

一棵有根树,每次修改一条边或询问跟到一个点的路径上有多少条边没被修改。

解题思路

可以用括号序表示树上的一条路径,访问时间戳+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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值