hdu 3974 Assign the task (线段树+树的遍历)
Description
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
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
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 代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
const int MAXN = ;
int n,Q,t,topboss,cnt;
int head[MAXN],tot;
int start[MAXN],endd[MAXN];
bool used[MAXN];
struct Edge
{
int to,next;
}edge[MAXN];
void init()
{
cnt=;
tot=;
memset(head,-,sizeof head);
memset(used,false,sizeof used);
}
void addedge (int u,int v)
{
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
}
void dfs(int u)
{
++cnt;
start[u]=cnt;
for (int i=head[u];i!=-;i=edge[i].next)
{
dfs(edge[i].to);
}
endd[u]=cnt;
}
struct Node
{
int l,r,val,lazy;
}segTree[MAXN<<];
void upDate_Same (int r,int v)
{
if (r)
{
segTree[r].val=v;
segTree[r].lazy=;
}
}
void push_down (int r)
{
if (segTree[r].lazy)
{
upDate_Same(r<<,segTree[r].val);
upDate_Same(r<<|,segTree[r].val);
segTree[r].lazy=;
}
}
void buildTree (int i,int l,int r)
{
segTree[i].l=l;
segTree[i].r=r;
segTree[i].val=-;
segTree[i].lazy=;
if (l==r)
return ;
int mid =(l+r)>>;
buildTree(i<<,l,mid);
buildTree(i<<|,mid+,r);
}
void update (int i,int l,int r,int v)
{
if (segTree[i].l==l&&segTree[i].r==r)
{
upDate_Same(i,v);
return ;
}
push_down(i);
int mid =(segTree[i].l+segTree[i].r)/;
if (r<=mid) update(i<<,l,r,v);
else if (l>mid) update(i<<|,l,r,v);
else
{
update(i<<,l,mid,v);
update(i<<|,mid+,r,v);
}
}
int query (int i,int u)
{
if (segTree[i].l==u&&segTree[i].r==u)
return segTree[i].val;
push_down(i);
int mid =(segTree[i].l+segTree[i].r)/;
if (u<=mid)
return query(i<<,u);
else
return query(i<<|,u);
}
int main()
{
//freopen("de.txt","r",stdin);
cin>>t;
int casee=;
while (t--){
printf("Case #%d:\n",++casee);
int u,v;
init();
scanf("%d",&n);
for (int i=;i<n-;++i){
cin>>u>>v;
used[u]=true;
addedge(v,u);
}
for (int i=;i<=n;++i){
if (!used[i]){
dfs(i);
break;
}
}
cin>>Q;
buildTree(,,cnt);
char op[];
while (Q--){
scanf("%s",op);
if (op[]=='C'){
scanf("%d",&u);
printf("%d\n",query(,start[u]));
}
else{
scanf("%d%d",&u,&v);
update(,start[u],endd[u],v);
}
}
}
return ;
}
700ms,有人200ms过了,正在研究。http://vjudge.net/contest/source/7108995 http://vjudge.net/contest/source/6308790
hdu 3974 Assign the task (线段树+树的遍历)的更多相关文章
- HDU 3974 Assign the task 并查集/图论/线段树
Assign the task Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...
- HDU 3974 Assign the task 暴力/线段树
题目链接: 题目 Assign the task Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- HDU 3974 Assign the task(简单线段树)
Assign the task Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 3974 Assign the task(线段树)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3974 题意:给定一棵树,50000个节点,50000个操作,C x表示查询x节点的值,T x y表示更 ...
- HDU 3974 Assign the task (DFS+线段树)
题意:给定一棵树的公司职员管理图,有两种操作, 第一种是 T x y,把 x 及员工都变成 y, 第二种是 C x 询问 x 当前的数. 析:先把该树用dfs遍历,形成一个序列,然后再用线段树进行维护 ...
- HDU 3974 Assign the task
Assign the task Problem Description There is a company that has N employees(numbered from 1 to N),ev ...
- HDU 3974 Assign the task (DFS序 + 线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3974 给你T组数据,n个节点,n-1对关系,右边的是左边的父节点,所有的值初始化为-1,然后给你q个操 ...
- HDU 3974 Assign the task(DFS序+线段树单点查询,区间修改)
描述There is a company that has N employees(numbered from 1 to N),every employee in the company has a ...
- hdu 3974 Assign the task(dfs序上线段树)
Problem Description There is a company that has N employees(numbered from 1 to N),every employee in ...
- HDU 3974 Assign the task(dfs建树+线段树)
题目大意:公司里有一些员工及对应的上级,给出一些员工的关系,分配给某员工任务后,其和其所有下属都会进行这项任务.输入T表示分配新的任务, 输入C表示查询某员工的任务.本题的难度在于建树,一开始百思不得 ...
随机推荐
- Security基础(三):OpenSSL及证书服务、邮件TLS/SSL加密通信
一.OpenSSL及证书服务 目标: 本案例要求熟悉OpenSSL工具的基本使用,完成以下任务操作: 使用OpenSSL加密/解密文件 搭建企业自有的CA服务器,为颁发数字证书提供基础环境 方案: 使 ...
- I2C总线协议详解
I2C总线定义 I2C(Inter-Integrated Circuit)总线是一种由PHILIPS公司开发的两线式串行总线,用于连接微控制器及其外围设备.I2C总线产生于在80年代,最初为音 ...
- 测开之路四十七:Django之请求静态资源与模板
框架必要的配置 import sysfrom django.conf.urls import urlfrom django.conf import settingsfrom django.http i ...
- 测开之路四十:jQuery基本用法
从cdn引入jQuery库:https://www.bootcdn.cn/,搜索jQuery 在html里面(使用之前计算器的脚本),把复制的标签粘贴到引入js标签的前面:<script src ...
- Elasticsearch后台运行步骤
Elasticsearch后台运行步骤 1.cmd 到elasticsearch 中bin目录下 2.elasticsearch-service 出现 3.安装服务 elasticsearch-se ...
- 0.OpenCV框架
reference: https://docs.opencv.org/4.1.2/ 基本使用 1.图片和视频,读写(2,8) 2.OpenCV基本数据类型(3) 3.OpenCV大型数据类型及操作:图 ...
- VUE不能对新增属性监测更新
data () { return { data:{}, } }, method:{ if(data.code==0){ this.loading = false; this.data = data.d ...
- 嵌入式C语言4.1 C语言内存空间的使用-指针
指针:就是内存资源的地址.门牌号的代名词 假如你所在的城市是一个内存(存储器),如果找到你家,就是通过你的家庭住址(指针)寻找,而你家里的摆设面积之类的就是内存的内容(指针指向的内容). 指针变量:存 ...
- django-3-视图(views.py)与网址(urls.py)
视图与网址 操作文件:urls.py.views.py urls.py 作用:用于处理前台的链接(如前台访问:127.0.0.1:8080/index/1212/21212),其实永远访问的是同一个文 ...
- 用webdriver模仿浏览器 爬取豆瓣python书单
用webdriver模仿浏览器 爬取豆瓣python书单 其中运用到os 模块 作用是生成文件夹 存储爬取的信息 etree 用于xpath解析内容 详细代码如下 可用我的上一篇博客存取到excel当 ...