1. 程式人生 > >暫時不想調的代碼 2325

暫時不想調的代碼 2325

public tree str fine swap print modify can efi

#include <cstdio>
#include <map>
#define N 200000

using std::map;
map<int,bool>q;
int dep[N],fa[N],siz[N],pos[N],dfn[N],tim,top[N],n,cnt,a[N],m;
struct Segment
{
    int l,r,dis,up;
    Segment * ch[2];
};
struct Edge
{
    int to;
    Edge * next;
}edge[N<<1],*head[N];
class SLPF { private: inline void pushup(Segment *&k) {k->up=k->ch[0]->up+k->ch[1]->up;} public: inline void ins(int u,int v) { edge[++cnt].next=head[u]; edge[cnt].to=v; head[u]=edge+cnt; }
void dfs1(int x) { dep[x]=dep[fa[x]]+1; siz[x]=1; for(Edge * u=head[x];u;u=u->next) { int v=u->to; if(fa[x]!=v) { fa[v]=x; dfs1(v); siz[x]
+=siz[v]; } } } void dfs2(int x) { int p=0; pos[x]=++tim; dfn[tim]=x; for(Edge * u=head[x];u;u=u->next) { int v=u->to; if(fa[x]!=v&&siz[p]<siz[v]) p=v; } if(p) dfs2(p); for(Edge * u=head[x];u;u=u->next) { int v=u->to; if(fa[x]!=v&&v!=p) dfs2(v); } } void build(Segment *&k,int l,int r) { k=new Segment(); k->l=l;k->r=r; if(l==r) { if(!q[a[dfn[l]]]) { k->dis=a[dfn[l]]; k->up=1; q[a[dfn[l]]]=1; } else { k->dis=0; k->up=0; } return; } int mid=(l+r)>>1; build(k->ch[0],l,mid); build(k->ch[1],mid+1,r); pushup(k); } int Section_Query(Segment *&k,int l,int r) { if(k->l==l&&k->r==r) return k->up; int mid=(k->l+k->r)>>1; if(l>mid) return Section_Query(k->ch[1],l,r); else if(r<=mid) return Section_Query(k->ch[0],l,r); else return Section_Query(k->ch[0],l,mid)+Section_Query(k->ch[1],mid+1,r); } void modify(Segment *&k,int t,int v,int f) { if(k->l==k->r) {k->dis=v;k->up=f;return;} int mid=(k->l+k->r)>>1; if(t<=mid) modify(k->ch[0],t,v,f); else modify(k->ch[1],t,v,f); pushup(k); } int Signle_Query(Segment *&k,int t) { if(k->l==k->r) return k->dis; int mid=(k->l+k->r)>>1; if(t<=mid) return Signle_Query(k->ch[0],t); else return Signle_Query(k->ch[1],t); } void swap(int &m,int &n) { int tmp=n; n=m; m=tmp; } }; class SLPF *abc; int main() { freopen("zytree1.in","r",stdin);freopen("zytree.out","w",stdout); Segment *root=new Segment(); scanf("%d",&n); for(int i=1;i<=n;++i) scanf("%d",&a[i]); for(int x,y,i=1;i<n;++i) { scanf("%d%d",&x,&y); if(x>y) abc->swap(x,y); abc->ins(x,y);abc->ins(y,x); } abc->dfs1(1);abc->dfs2(1); abc->build(root,1,n); scanf("%d",&m); for(int opt,x,y;m--;) { scanf("%d%d",&opt,&x); if(opt) printf("%d\n",abc->Section_Query(root,pos[x],pos[x]+siz[x]-1)); else { scanf("%d",&y); int v=abc->Signle_Query(root,pos[x]); q[v]=0; if(!q[y]) q[y]=1,abc->modify(root,pos[x],y,1); else abc->modify(root,pos[x],0,0); } } return 0; }

暫時不想調的代碼 2325