hdoj 3974 Assign the task 【DFS + 线段树区间修改】
来源:互联网 发布:pyqt4 linux安装 编辑:程序博客网 时间:2024/05/21 15:40
Assign the task
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1457 Accepted Submission(s): 667
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.
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)
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
题意:给你一棵有N个节点的树,有M次操作。
C x y 表示把x点权值修改为y,该效应会影响到x的所有孩子、孙子节点。Q x 查询x的权值。
思路:求出DFS序,建立一棵线段树,找到每个节点第一次出现的位置left和最后一次出现的位置right,更新时更新区间[left, right],查询时单点查询即可。
AC代码:
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <set>#include <vector>#include <string>#define INF 0x3f3f3f3f#define eps 1e-8#define MAXN (100000+10)#define MAXM (500000)#define Ri(a) scanf("%d", &a)#define Rl(a) scanf("%lld", &a)#define Rf(a) scanf("%lf", &a)#define Rs(a) scanf("%s", a)#define Pi(a) printf("%d\n", (a))#define Pf(a) printf("%.2lf\n", (a))#define Pl(a) printf("%lld\n", (a))#define Ps(a) printf("%s\n", (a))#define W(a) while(a--)#define CLR(a, b) memset(a, (b), sizeof(a))#define MOD 1000000007#define LL long long#define lson o<<1, l, mid#define rson o<<1|1, mid+1, r#define ll o<<1#define rr o<<1|1#define PI acos(-1.0)using namespace std;struct Tree{ int l, r, len; int lazy, sum;};Tree tree[MAXN<<2];void PushUp(int o){ tree[o].sum = tree[ll].sum + tree[rr].sum;}void PushDown(int o){ if(tree[o].lazy != -1) { tree[ll].lazy = tree[rr].lazy = tree[o].lazy; tree[ll].sum = tree[o].lazy * tree[ll].len; tree[rr].sum = tree[o].lazy * tree[rr].len; tree[o].lazy = -1; }}void Build(int o, int l, int r){ tree[o].l = l; tree[o].r = r; tree[o].len = r-l+1; tree[o].sum = -1; tree[o].lazy = -1; if(l == r) return ; int mid = (l + r) >> 1; Build(lson); Build(rson);}void Update(int o, int L, int R, int v){ if(tree[o].l >= L && tree[o].r <= R) { tree[o].lazy = v; tree[o].sum = tree[o].len * v; return ; } PushDown(o); int mid = (tree[o].l + tree[o].r) >> 1; if(R <= mid) Update(ll, L, R, v); else if(L > mid) Update(rr, L, R, v); else { Update(ll, L, mid, v); Update(rr, mid+1, R, v); } PushUp(o);}int Query(int o, int pos){ if(tree[o].l == tree[o].r) return tree[o].sum; PushDown(o); int mid = (tree[o].l + tree[o].r) >> 1; if(pos <= mid) return Query(ll, pos); else return Query(rr, pos);}struct Edge{ int from, to, next;};Edge edge[MAXM];int head[MAXN], edgenum;void init(){ edgenum = 0; CLR(head, -1);}void addEdge(int u, int v){ Edge E = {u, v, head[u]}; edge[edgenum] = E; head[u] = edgenum++;}int vs[MAXN], dfs_clock;void DFS(int u, int fa, int d){ vs[dfs_clock++] = u; for(int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; if(v == fa) continue; DFS(v, u, d+1); vs[dfs_clock++] = u; }}int pre[MAXN];int lp[MAXN], rp[MAXN];int main(){ int t, kcase = 1; Ri(t); W(t) { int N, Q; Ri(N); init(); for(int i = 1; i <= N; i++) pre[i] = i; for(int i = 0; i < N-1; i++) { int u, v; Ri(u); Ri(v); pre[u] = v; addEdge(u, v); addEdge(v, u); } int root; for(int i = 1; i <= N; i++) { if(pre[i] == i) { root = i; break; } } dfs_clock = 1; DFS(root, -1, 1); CLR(lp, 0); CLR(rp, 0); for(int i = 1; i < dfs_clock; i++) { printf("%d ", vs[i]); if(lp[vs[i]] == 0) lp[vs[i]] = rp[vs[i]] = i; else rp[vs[i]] = i; } printf("\n"); Ri(Q); Build(1, 1, dfs_clock-1); printf("Case #%d:\n", kcase++); W(Q) { char op[2]; Rs(op); int x, y; if(op[0] == 'T') { Ri(x); Ri(y); Update(1, lp[x], rp[x], y); } else { Ri(x); Pi(Query(1, lp[x])); } } } return 0;}
0 0
- hdoj 3974 Assign the task 【DFS + 线段树区间修改】
- HDU-3974 Assign the task (dfs序+线段树区间修改点查询)
- HDU - 3974 Assign the task(线段树 区间修改)
- HDU 3974 Assign the task(dfs序 + 线段树区间赋值,单点查询)
- Assign the task(dfs+线段树_
- HDU 3974 Assign the task (线段树+dfs序)
- HDU 3974 Assign the task(DFS建树+线段树)
- HDU 3974 Assign the task (DFS + 线段树)
- HDU3974-Assign the task(线段树+区间建树)
- J - Assign the task(dfs序+线段树)J
- HDU 3974 Assign the task (线段树)
- HDU 3974 Assign the task(线段树)
- hdu 3974 Assign the task (线段树)
- HDU 3974 Assign the task 线段树(树映射到区间)
- hdu 3974 Assign the task(线段树+将树映射到区间)
- 杭电3974 Assign the task(线段树区间更新点查询)
- HDU 3974 Assign the task 图论/线段树区间更新,单点查询
- HDU-3974-Assign the task(线段树维护dfs序)
- UINvigation,导航条
- UITextFiled的输入框改成一条下划线
- Axure VS Mockplus VS Balsamiq - 原型图设计工具对比
- Missing Ranges
- iOS二级tabView,左右两个tableView,点左边,刷新右边
- hdoj 3974 Assign the task 【DFS + 线段树区间修改】
- 彻底搞明白Calendar中的一些易混淆的属性和方法
- iOS常用第三方框架/库
- VC Web打印解决方案概述
- Windows 7中的“帮助和支持”无法打开怎么办?
- Debug your Java code with ease using JPDA
- Android_编程开发规范
- 20160108HTML学习笔记html的四大基本结构
- C#实现SFTP文件传输