Assign the task

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 636    Accepted Submission(s): 322

Problem Description
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
 
Source
 

用线段树修改区间值,查询单点值。

好久没写线段树了,这都写挫。。。

 /* ***********************************************
Author :kuangbin
Created Time :2013-11-17 19:50:24
File Name :E:\2013ACM\比赛练习\2013-11-17\C.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std; const int MAXN = ;
struct Edge
{
int to,next;
}edge[MAXN];
int head[MAXN],tot;
int cnt;
int start[MAXN],end[MAXN];
void init()
{
cnt = ;
tot = ;
memset(head,-,sizeof(head));
}
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);
}
end[u] = cnt;
}
struct Node
{
int l,r;
int val;
int 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 Build(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)/;
Build(i<<,l,mid);
Build((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);
}
bool used[MAXN];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n;
int T;
scanf("%d",&T);
int iCase = ;
while(T--)
{
iCase++;
printf("Case #%d:\n",iCase);
int u,v;
memset(used,false,sizeof(used));
init();
scanf("%d",&n);
for(int i = ;i < n;i++)
{
scanf("%d%d",&u,&v);
used[u] = true;
addedge(v,u);
}
for(int i = ;i <= n;i++)
if(!used[i])
{
dfs(i);
break;
}
Build(,,cnt);
char op[];
int m;
scanf("%d",&m);
while(m--)
{
scanf("%s",op);
if(op[] == 'C')
{
scanf("%d",&u);
printf("%d\n",query(,start[u]));
}
else
{
scanf("%d%d",&u,&v);
update(,start[u],end[u],v);
}
}
}
return ;
}

HDU 3974 Assign the task(简单线段树)的更多相关文章

  1. HDU 3974 Assign the task 暴力/线段树

    题目链接: 题目 Assign the task Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...

  2. hdu 3974 Assign the task(线段树)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3974 题意:给定一棵树,50000个节点,50000个操作,C x表示查询x节点的值,T x y表示更 ...

  3. HDU 3974 Assign the task (DFS+线段树)

    题意:给定一棵树的公司职员管理图,有两种操作, 第一种是 T x y,把 x 及员工都变成 y, 第二种是 C x 询问 x 当前的数. 析:先把该树用dfs遍历,形成一个序列,然后再用线段树进行维护 ...

  4. HDU - 3974 Assign the task (线段树区间修改+构建模型)

    https://cn.vjudge.net/problem/HDU-3974 题意 有一棵树,给一个结点分配任务时,其子树的所有结点都能接受到此任务.有两个操作,C x表示查询x结点此时任务编号,T ...

  5. hdu 3974 Assign the task (线段树+树的遍历)

    Description There is a company that has N employees(numbered from 1 to N),every employee in the comp ...

  6. J - Assign the task - hdu 3974(DFS建树+简单线段树)

    题意:给一些节点简单额对应关系,可以组成一个树,如果树的某一个节点更新那么他的所有子节点都要更新,中间,会有一些查询 分析:题意倒也不难理解,但是但是不知道怎么建树...于是自能百度,看了kuangb ...

  7. HDU 3974 Assign the task 简单搜索

    根据Rex 的思路才知道可以这么写. 题目意思还是很好理解的,就是找到当前雇员最近的任务. 做法是,可以开辟一个 tim 变量,每次有雇员得到昕任务时候 ++tim 然后取寻找最近的任务的时候写一个搜 ...

  8. HDU 3974 Assign the task 并查集/图论/线段树

    Assign the task Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...

  9. HDU 3974 Assign the task (DFS序 + 线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3974 给你T组数据,n个节点,n-1对关系,右边的是左边的父节点,所有的值初始化为-1,然后给你q个操 ...

随机推荐

  1. UVALive 7456 Least Crucial Node

    题目链接 题意: 给定一个无向图,一个汇集点,问哪一个点是最关键的,如果有多个关键点,输出序号最小的那个. 因为数据量比较小,所以暴力搜索就行,每去掉一个点,寻找和汇集点相连的还剩几个点,以此确定哪个 ...

  2. ssh-copy-id 复制公钥到远程server

    ssh-copy-id -i ~/.ssh/mykey.pub user@host 复制完成后可以测试: ssh -i ~/.ssh/mykey user@host

  3. JAVA中Collection接口和Map接口的主要实现类

    Collection接口 Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的元素(Elements).一些Collection允许相同的元素 ...

  4. linux下简单的备份的脚本 2 【转】

    转自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=26807463&id=4577034 之前写过linux下简单的 ...

  5. npm tls证书报错

    Error: unable to verify the first certificate 设置node环境(零时使用,再次使用需要重新使用) set NODE_TLS_REJECT_UNAUTHOR ...

  6. Java 基本语法---流程控制

    Java 基本语法---流程控制 0. 概述 三大流程控制语句:顺序.选择.循环. 选择结构: if 结构,if - else结构: 多重 if - else 语句 ; 嵌套 if - else 语句 ...

  7. 中文分词-jieba

    支持三种分词模式: 精确模式,试图将句子最精确地切开,适合文本分析: 全模式,把句子中所有的可以成词的词语都扫描出来, 速度非常快,但是不能解决歧义: 搜索引擎模式,在精确模式的基础上,对长词再次切分 ...

  8. tensorflow-作用域

    变量名字由两部分组成:scope/变量name. name 参数才是对象的唯一标识. 1.tf.name_scope() Graph中保存着一个属性_name_stack(string类型),_nam ...

  9. 欢迎来到abc2237512422的博客

    这是第一篇博文. 本博客已迁移到 abc233.site

  10. spring-cloud-sleuth+zipkin追踪服务实现(四)

    1.前言 在上一篇spring-cloud-sleuth+zipkin追踪服务实现(三)的处理实现后,很多朋友告诉我,在zipkin server的管理页面无法看到项目依赖关系. 当时也没有多想,以为 ...