HDU 3974 Assign the task (dfs number + segment tree segment update), hdu1_4
It specifies the relationship between the upper and lower levels of a point. If task a is assigned to I, all its subordinates will be assigned. They all stopped their work and started to do.
Operate T x y to assign x task y and C x to ask about the current task of x;
Sample Input
1 5 4 3 3 2 1 3 5 2 5 C 3 T 2 1 C 3 T 3 2 C 3
Sample Output
Case #1:-1 1 2
Ideas:
The dfs depth is used to traverse and rename the node to make the son of a node continuous. Then update the data in segments.
2:1-5
5: 2-2
PM-5
-4
PM-5 pm
#include<iostream>#include<cstring>#include<cstdio>#include<cstdlib>#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define N 50001using namespace std;int lazy[N<<2];int task[N<<2];int next[N],pre[N],to[N];int uset[N];int num;int l[N],r[N];int find_set(int x){ while(uset[x]!=x) x=uset[x]; return x;}void dfs(int root){ int i=pre[root]; l[root]=(++num); while(i!=-1) { dfs(to[i]); i=next[i]; } r[root]=num;}void pushdown(int rt){ if(lazy[rt]!=-1) { task[rt<<1]=lazy[rt]; lazy[rt<<1]=lazy[rt]; task[rt<<1|1]=lazy[rt]; lazy[rt<<1|1]=lazy[rt]; lazy[rt]=-1; }}void build(int l,int r,int rt){ task[rt]=-1; lazy[rt]=-1; if(l==r) return; int m=(l+r)>>1; build(lson); build(rson);}void update(int L,int R,int l,int r,int rt,int val){ if(L<=l&&r<=R) { lazy[rt]=val; task[rt]=val; return; } pushdown(rt); int m=(r+l)>>1; if(L<=m) update(L,R,lson,val); if(R>m) update(L,R,rson,val);}void query(int L,int R,int l,int r,int rt){ if(L<=l&&r<=R) { printf("%d\n",task[rt]); return; } pushdown(rt); int m=(l+r)>>1; if(L<=m) query(L,R,lson); if(R>m) query(L,R,rson);}int main(){ int T; scanf("%d",&T); for(int cas=1;cas<=T;cas++) { int n; int u,v; scanf("%d",&n); for(int i=1;i<=n;i++) { uset[i]=i; pre[i]=-1; } for(int i=1;i<n;i++) { scanf("%d%d",&u,&v); uset[u]=v; to[i]=u; next[i]=pre[v]; pre[v]=i; } int root=find_set(1); num=0; dfs(root); //for(int i=1;i<=num;i++) // printf("%d -- %d %d\n",i,l[i],r[i]); //system("pause"); build(1,num,1); int m; int a,b; char op[10]; scanf("%d",&m); printf("Case #%d:\n",cas); for(int i=0;i<m;i++) { scanf("%s",&op); if(op[0]=='C') { scanf("%d",&a); query(l[a],l[a],1,num,1); } else { scanf("%d %d",&a,&b); update(l[a],r[a],1,num,1,b); } } } return 0;}