这题根本没想过线段树,如果不是kuangbin的专题,我想用并查集做。这题想不到的根本就是不会建树,把每个boss的小弟都一个一个按小弟等级排在boss后面,通过dfs找到每个人拥有的小弟数量,顺便再按等级把这些人排在a数组上,例如11管22,33 22管1,2,3,4,5 33管7,8,9,10,13,45,那么排在a数组上就是这样:(11){ (22) 1 2 3 4 5 } { (33) 7 8 9 10 13 45 } 那么在已经知道每个人拥有的小弟数量之后,给x分配任务就是更新从x开始到x+(小弟数量)-1的区间,那线段树就可以做了。数组作用在代码里讲解
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <math.h>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <time.h>
#include <set>
#include <list>
#include <iostream>
#define ll long long
#define ull unsigned long long
#define cls(x) memset(x,0,sizeof(x))
#define clslow(x) memset(x,-1,sizeof(x))
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=5e4+100;
const int mod=1e9+7;
vector<int>bos[maxn];
int vis[maxn],tot=1,cnt[maxn],pos[maxn];
int lazy[maxn<<2];
int dfs(int x)
{
pos[x]=tot;
tot++;
int num=1;
for(int i=0;i<bos[x].size();i++){
num+=dfs( bos[x][i] );
}
cnt[x]=num;
return num;
}
void Push_down(int rt)
{
if(lazy[rt]!=-1){
lazy[rt<<1]=lazy[rt];
lazy[rt<<1|1]=lazy[rt];
lazy[rt]=-1;
}
}
void Build(int l,int r,int rt)
{
lazy[rt]=-1;
if(l==r)
{
return;
}
int mid=(l+r)>>1;
Build(l,mid,rt<<1);
Build(mid+1,r,rt<<1|1);
}
void Update(int L,int R,int l,int r,int rt,int C)
{
if(L<=l && r<=R)
{
lazy[rt]=C;
return;
}
Push_down(rt);
int mid=(l+r)>>1;
if(L<=mid)Update(L,R,l,mid,rt<<1,C);
if(R>mid)Update(L,R,mid+1,r,rt<<1|1,C);
}
int Query(int L,int R,int l,int r,int rt)
{
if(L<=l && r<=R)
{
return lazy[rt];
}
Push_down(rt);
int mid=(l+r)>>1;
if(L<=mid)return Query(L,R,l,mid,rt<<1);
else return Query(L,R,mid+1,r,rt<<1|1);
}
int main()
{
int t,cas=1;
scanf("%d",&t);
while(t--){
printf("Case #%d:\n",cas++);
int n;
scanf("%d",&n);
memset(vis,0,sizeof vis);
tot=1;
for(int i=0;i<maxn;i++)bos[i].clear();
Build(1,n,1);
for(int i=0;i<n-1;i++){
int u,v;
scanf("%d%d",&u,&v);
bos[v].push_back(u);
vis[u]=1;
}
for(int i=1;i<=n;i++){
if(vis[i]==0){
dfs(i);
}
}
int m;
scanf("%d",&m);
while(m--){
char c[5];
int x,y;
scanf("%s",c);
if(c[0]=='C'){
scanf("%d",&x);
cout<<Query( pos[x],pos[x],1,n,1 )<<endl;
}
else if(c[0]=='T'){
scanf("%d%d",&x,&y);
Update( pos[x],pos[x]+cnt[x]-1,1,n,1,y );
}
}
}
return 0;
}