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. Zookeeper笔记之使用zk实现集群选主

    一.需求 在主从结构的集群中,我们假设硬件机器是很脆弱的,随时可能会宕机,当master挂掉之后需要从slave中选出一个节点作为新的master,使用zookeeper可以很简单的实现集群选主功能. ...

  2. pytorch之LSTM

    from:http://pytorch-cn.readthedocs.io/zh/latest/package_references/torch-nn/#recurrent-layers class ...

  3. MAVEN:不能互相引用

    工程A,工程B,工程C,这三个工程:C依赖B,B依赖A,这是没有问题的. 但是不能A依赖B,B又依赖A,这是不允许的.

  4. easyui表格,单元格合并

    easyui的合并单元格比较麻烦,官网提供一下方法 $('#tt').datagrid({ onLoadSuccess:function(){ var merges = [{ index:2, row ...

  5. window BIOS设置硬盘启动模式

      bios如何设置硬盘启动模式?BIOSD硬盘模试主是要针对IDE接口的硬盘和SATA接口的硬盘来设置的.以前的主板只支持一种类型.现在的智能笔记本主板支持:IDE Mode.AHCI Mode.下 ...

  6. laravel中redis队列的使用

    一.配置文件 首先我们需要在配置文件中配置默认队列驱动为Redis,队列配置文件是config/queue.php: return [ 'default' => env('QUEUE_DRIVE ...

  7. sed的一些使用技巧

    一.当一个文件里有两行相同的内容,但这时只想修改第一行的内容或者第二行的内容,而不是全部修改,以下例子说明下: 1.修改匹配到第一行为port的内容(若要真修改前面记得-i): [root suppo ...

  8. sed正则表达式匹配,各种括号的转义和不转义

    https://blog.csdn.net/zl87758539/article/details/77481679

  9. advStringGrid单元格文字垂直居中

    1.必须设置advStringGrid属性WordWrap = false, 2.在OnGetAlignment事件中,添加以下代码 procedure Tfrm_book_input.StringG ...

  10. 【AtCoder】Tenka1 Programmer Contest(C - F)

    C - Align 考的时候,我大胆猜了结论,就是一小一大一小一大这么排 证明的话,由于我们总是要加上相邻的最大值而减去最小值,我们就让最大值都保持在前面 如果长度为奇数,要么就是大小大小大,要么是小 ...