题意:

染色问题:给一个固定结构的树,现在有两个操作:
(1) y 将结点x及其所有后代结点染成颜色y;
(2)查询结点x当前的颜色。
其实就是区间染色问题,不过需要dfs预处理,

题目:

There is a company that has N employees(numbered from 1 to N),every employee in the company has a immediate boss (except for the leader of whole company).If you are the immediate boss of someone,that person is your subordinate, and all his subordinates are your subordinates as well. If you are nobody’s boss, then you have no subordinates,the employee who has no immediate boss is the leader of whole company.So it means the N employees form a tree.

The company usually assigns some tasks to some employees to finish.When a task is assigned to someone,He/She will assigned it to all his/her subordinates.In other words,the person and all his/her subordinates received a task in the same time. Furthermore,whenever a employee received a task,he/she will stop the current task(if he/she has) and start the new one.

Write a program that will help in figuring out some employee’s current task after the company assign some tasks to some employee.

Input

The first line contains a single positive integer T( T <= 10 ), indicates the number of test cases.

For each test case:

The first line contains an integer N (N ≤ 50,000) , which is the number of the employees.

The following N - 1 lines each contain two integers u and v, which means the employee v is the immediate boss of employee u(1<=u,v<=N).

The next line contains an integer M (M ≤ 50,000).

The following M lines each contain a message which is either

“C x” which means an inquiry for the current task of employee x

or

"T x y"which means the company assign task y to employee x.

(1<=x<=N,0<=y<=10^9)

Output

For each test case, print the test case number (beginning with 1) in the first line and then for every inquiry, output the correspond answer per line.

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

分析:

1.由于题目给的是一个固定的树,所以我们需要对它进行处理,使之成为我们好处理的区间问题。
2.关于预处理我采用的是先用vector容器进行存边,之后dfs,用两个数组分别存储以某点为“”祖宗节点“”的区间起始(类似时间戳的思想,将某个点的编号)
3.预处理之后就是线段树模板啦嘿嘿,这个没啥好说的,这里不再赘述: 区间修改+单点查询

AC代码:

#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
const int M=5e4+10;
int t,n,m,ans,a,b,k;
vector<int>ve[M];
int L[M],R[M],dp[M<<2],lazy[M<<2];
bool vis[M];
char s[5];
void dfs(int x)
{L[x]=++ans;for(int i=0; i<ve[x].size(); i++)dfs(ve[x][i]);R[x]=ans;
}
void pushdown(int x)
{if(lazy[x]){lazy[x<<1]=lazy[x<<1|1]=lazy[x];dp[x<<1]=dp[x<<1|1]=dp[x];lazy[x]=0;}
}
void update(int mi,int ma,int l,int r,int x,int value)
{if(l>=mi&&r<=ma){dp[x]=lazy[x]=value;return;}pushdown(x);int mid=(l+r)>>1;if(mid>=mi)update(mi,ma,l,mid,x<<1,value);if(mid<ma)update(mi,ma,mid+1,r,x<<1|1,value);
}
int query(int now,int l,int r,int x)//单点修改
{if(l==r)return dp[x];pushdown(x);int mid=(l+r)>>1;if(mid>=now)return query(now,l,mid,x<<1);elsereturn query(now,mid+1,r,x<<1|1);
}
int main()
{scanf("%d",&t);k=0;while(t--){ans=0;memset(dp,-1,sizeof(dp));memset(lazy,0,sizeof(lazy));memset(vis,false,sizeof(vis));scanf("%d",&n);for(int i=1; i<=n; i++)ve[i].clear();for(int i=0; i<n-1; i++){int u,v;scanf("%d%d",&u,&v);ve[v].push_back(u);//接下来的N-1行每行包含两个整数u和v,这表示雇员v是雇员u(1 <= u,v <= N)的直接老板。vis[u]=true;}for(int i=1; i<=n; i++)if(!vis[i])dfs(i);scanf("%d",&m);printf("Case #%d:\n",++k);while(m--){scanf("%s",s);if(s[0]=='C'){scanf("%d",&a);printf("%d\n",query(L[a],1,ans,1));}else{scanf("%d%d",&a,&b);update(L[a],R[a],1,ans,1,b);}}}return 0;
}

备战ing,题目分析简略,见谅,转载请注明出处。。。。。

Assign the task HDU - 3974(线段树+dfs建树+单点查询+区间修改)相关推荐

  1. 树状数组的建树 单点修改 单点查询 区间修改 区间查询

    单点修改  单点查询   用普通数组就能写出来 单点修改  区间查询   用线段树  树状数组: 区间修改  区间查询   用线段树  树状数组: 区间修改  单点查询   用线段树  树状数组: 建 ...

  2. HDU ACM 4031 Attack (树状数组--单点查询+区间更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=4031 用了树状数组的区间更新 单点查找(一般为单点更新 区间查找) 例如 区间(2,4)加1 则Updata(2 ...

  3. 线段树之延时标记(区间修改)及lazy思想

    暴力求解 1.最简单的方法是:在主函数中添加一个循环  进行 r-l+1次单点修改实现区间修改,对于单个元素修改时间复杂度为 O(log2(n)) 所以对于单个区间修改的时间复杂度为 O(n*log( ...

  4. 线段树基础操作--单点or区间更新+查询

    问题概述:先输入一个数n,表示一个班有n个人,一开始每人手中都有一个糖果,后输入一个数m并进行m次操作,每次操作a.b.c表示从第a个人到第b个人每个人重新分配得到c个糖果,求最后一个班所有人的总糖果 ...

  5. 线段树的建树 单点修改 区间查询

    (未经允许,严禁抄袭转载)(若有错误,请多多指教) 看了这张图后,大家可能明白建树的意思了 看代码以及注释 #include<bits/stdc++.h> using namespace ...

  6. bzoj3252攻略(线段树+dfs序)或者(树链剖分+dfs)

    3252: 攻略 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 1341 Solved: 642 [Submit][Status][Discuss] ...

  7. POJ 2777 ZOJ 1610 HDU 1698 --线段树--区间更新

    直接将这3题 放一起了  今天在做线段树的东西 这3个都是区间更新的 查询方式互相不同 反正都可以放到一起吧 直接先上链接了 touch me touch me touch me 关于涉及到区间的修改 ...

  8. Codeforces 877 E Danil and a Part-time Job(线段树+dfs序)

    题目地址 题意:给你一棵树,1为根节点,每个节点都有应该状态0或者1,你有两种操作,pow的操作是把该节点以及他的所有子树的每个节点进行自身异或的操作,get就是查询该节点以及他的所有子树的每个节点有 ...

  9. BZOJ_3252_攻略_线段树+dfs序

    BZOJ_3252_攻略_线段树+dfs序 Description 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神模式后,他可以同时攻略k部游戏.今天他得到了一款新游戏< ...

最新文章

  1. 计算机代码坑人小程序bat,批处理写的关机小程序--bat
  2. Nginx/LVS/HAProxy 负载均衡软件的优缺点详解
  3. 机器学习中有监督学习——回归
  4. Py之qrcode:调用python的qrcode库两种方式生成二维码、带logo的二维码
  5. MathExam任务一
  6. 单词背诵【CodeVS3013】 哈希
  7. 解决:无法获取实体类com.xxx.xx.xx.xx.dto.XxxDTO对应的表名
  8. 软考网络工程师笔记-综合知识2
  9. 全志R40 串口485 控制操作
  10. I/O读写的另一种方式-NIO
  11. 命名 —— 函数、类的命名
  12. 基于SpringBoot+Mybatis+Thymeleaf的信息管理系统
  13. IntelliJ idea 主题包下载以及安装
  14. 电磁场有限元基本原理(5)
  15. 爱国者u盘linux驱动,爱国者u盘驱动
  16. 【WiFi】WiFi 2.4G信道国家码对应关系
  17. 脾气与冲突--试用ymlf下wine跑windows程序有感
  18. 小红书618品牌营销蓄水阶段告一段落,5月即将进入冲刺期
  19. 手机突然提示无服务,无法使用蜂窝移动 解决流程
  20. mysql中varbinary什么意思_MySQL中的数据类型binary和varbinary详解

热门文章

  1. C语言试题四十五之把第1到第p个字符,平移到字符串的最后,把第p+1到最后的字符移到字符串的前部。
  2. Android之jni编译出现error: jump to label ‘XXXX’ [-fpermissive]解决办法
  3. Android官方开发文档Training系列课程中文版:Android的安全建议 .
  4. solidity bytes 智能合约开发知识浅学(五点一)bytes基本概念
  5. pythonweb服务器怎么让别人访问_Django配置让其他电脑访问网站
  6. c语言十佳运动员有奖评选系统_2019年沃德十佳内饰解读
  7. 做生意最重要的诚信呢??? | 今日最佳
  8. 数学建模,还得这样学!
  9. 大道至简,大数据的小窍门
  10. java 线程 获取消息_获取java线程中信息