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表示查询某员工的任务.本题的难度在于建树,一开始百思不得 ...
随机推荐
- JS中的一些遍历方法
1.遍历数组 以下遍历方法中for循环性能最好,而且优化版for循环性能最高.只有forEach不能跳出循环. 在循环数组时,如果在循环过程中对数组进行了增删改,那么在后面的每次循环中,进行操作的都是 ...
- Java总结第二期
大家好,我又来了!!啦啦,我知道你们很想我,很想我赶快写更多的文章来提高自己的水平,好吧,我就从了你们.下面跟我一起来光顾Java第二期,掌声,掌声!!! 第二章: 这章,我要给大家讲得内容有变量,常 ...
- 关于设置shadowPath的重要性
这是超级容易添加阴影到iOS中的任何视图.所有您需要做的是 添加QuartzCore框架到项目中(如果不存在的话) 导入QuartzCore到您的执行文件 添加一行如[myView.layer set ...
- 113、TensorFlow变量集合
#一个tensorflow程序断开的部分可能要创建变量 # 如果有一种方法来访问所有的变量是非常有用的 #因为这个原因TensorFlow提供了集合,是一些张量的集合 #或者是其他的对象,就像tf.V ...
- inline-block,inline,block,table-cell,float
float:left ---------------------------------------------------------------------------------------- ...
- 网络命令-nc(一)
一直在linux环境下编程,但却没有用过nc命令,不过最近发现Netcat这个命令-nc,发现真的蛮强大的, 为了备忘,就写了这个博客吧,不求全,只求把自己觉得很有用的命令整理出来,这篇文章估计要长期 ...
- 2019.7.26 NOIP 模拟赛
这次模拟赛真的,,卡常赛. The solution of T1: std是打表,,考场上sb想自己改进匈牙利然后wei了(好像匈牙利是错的. 大力剪枝搜索.代码不放了. 这是什么神仙D1T1,爆蛋T ...
- Android深度探索-卷1第三章心得体会
第三章整章介绍了git,git是一个开源的分布式版本控制系统,用以有效.高速的处理从很小到非常大的项目版本管理.通过配置git后可以很方便的找到需要的资源,更多的是代码和包,可以在本地建立版本库,为了 ...
- git笔记十:本地仓库同步到gitlab
本地仓库同步到gitlab 帮助文档 git remote --help 操作场景: 本地创建git仓库(含有readme.md文件), commit了三次 gitlab网站创建了一个项目 添加了re ...
- for语句基础求和练习
结构 for(初始化表达式;条件表达式;循环后的操作表达式) { 循环体; } 1.求出1-10之间数据之和: class Hello2 { public static void main(Strin ...