hdu3974(dfs序+区间修改)
题目描述:
Assign the task
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.
题意:一颗树,有时会对整颗子树进行修改操作,有时会查询某节点的值
思路:dfs序列求出子树的区间范围,然后操作的时候就是区间修改,查询为单点查询,剩下的也就是线段树基本操作
AC代码:
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 50200; const int inf = 0x3f3f3f3f; #define ls(x) x<<1 #define rs(x) x<<1|1 #define lson rt<<1,l,mid #define rson rt<<1|1,mid+1,r struct edge { int t, nxt; }e[maxn]; int hd[maxn], tot; void add(int f, int t) { e[++tot] = { t,hd[f] }; hd[f] = tot; } struct node { int l, r, task,tag; }tree[maxn<<2]; int len; int du[maxn],st[maxn],ed[maxn]; void init() { memset(du, 0, sizeof(du)); tot = len = 0; memset(hd, 0, sizeof(hd)); memset(tree, 0, sizeof(tree)); memset(st, 0, sizeof(st)); memset(ed, 0, sizeof(ed)); } void dfs(int u) {//求出该树的区间范围 st[u] = ++len; for (int i = hd[u]; i; i = e[i].nxt) { int v = e[i].t; dfs(v); } ed[u] = len; } void build(int rt, int l, int r) { tree[rt].l = l, tree[rt].r = r; tree[rt].task = -1; tree[rt].tag = 0; if (l == r)return; int mid = (l + r) >> 1; build(lson); build(rson); } void push_down(int rt) { if (!tree[rt].tag)return; tree[ls(rt)].tag = 1; tree[rs(rt)].tag = 1; tree[ls(rt)].task = tree[rt].task; tree[rs(rt)].task = tree[rt].task; tree[rt].tag = 0; } void update(int rt, int L, int R,int k) { int l = tree[rt].l, r = tree[rt].r; push_down(rt); if (L<= l&& r<= R) { tree[rt].tag = 1; tree[rt].task = k; return; } int mid = (l + r) >> 1; if (L <= mid) { update(ls(rt), L, R, k); } if (R > mid) { update(rs(rt), L, R, k); } } int qurry(int rt, int p) { int l = tree[rt].l, r = tree[rt].r; push_down(rt); if (l==r) { return tree[rt].task; } int mid = (l + r) >> 1; if (p <= mid) { return qurry(ls(rt), p); } else { return qurry(rs(rt), p); } } char cmd[20]; int main() { //freopen("test.txt", "r", stdin); int t; scanf("%d", &t); for (int s = 1; s <= t; s++) { printf("Case #%d:\n", s); init(); int n; scanf("%d", &n); for (int i = 1; i < n; i++) { int a, b; scanf("%d%d", &a, &b); add(b, a); du[a]++; } int rt = 0; for (int i = 1; i <= n; i++) {//找根建线段树 if (!du[i]) { dfs(i); rt = i; break; } } build(1,st[rt],ed[rt]);//初始化线段树 int m; scanf("%d", &m); while (m--) { scanf("%s", &cmd); int x; scanf("%d", &x); if (cmd[0] == 'C') { printf("%d\n", qurry(1, st[x])); } else { int tsk; scanf("%d", &tsk); update(1, st[x], ed[x], tsk); } } } return 0; }