HDU - 3974
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 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)
题意为给出n个人n-1个关系,u-v表示v是u的上司,最后会形成树结构,q次操作,C x为查询x的当前任务id,T x y为分配给x id为y的任务,他的下属也会改为为这个任务工作
根据dfs序,每次修改时将x和它的子树修改即可,查询即为单点查询,需要注意只有修改的时候需要pushdown
pos数组为u在dfs序的位置,sz数组保存u的子树数量,修改时即为pos[u]->pos[u] + sz[u] - 1
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
const int maxn = 5e4 + 3;
typedef long long ll;
vector<int> E[maxn];
int pos[maxn], sz[maxn], tot;
void dfs(int u) {
pos[u] = tot++;
sz[u] = 1;
for(int v : E[u]) {
dfs(v);
sz[u] += sz[v];
}
}
int tre[maxn << 2];
void pushdown(int rt, int l, int r) {
if(tre[rt] == -1) return;
tre[rt << 1] = tre[rt << 1 | 1] = tre[rt];
if(l == r) return;
int mid = l + r >> 1;
pushdown(rt << 1, l, mid);
pushdown(rt << 1 | 1, mid + 1, r);
}
void Build(int rt, int l, int r) {
tre[rt] = -1;
if(l == r) return;
int mid = l + r >> 1;
Build(rt << 1, l, mid);
Build(rt << 1 | 1, mid + 1, r);
}
void Update(int rt, int L, int R, int x, int l, int r) {
if(L <= l && r <= R) {
tre[rt] = x;
pushdown(rt, l, r);
return;
}
int mid = l + r >> 1;
if(mid >= R) Update(rt << 1, L, R, x, l, mid);
else if(mid < L) Update(rt << 1 | 1, L, R, x, mid + 1, r);
else {
Update(rt << 1, L, R, x, l, mid);
Update(rt << 1 | 1, L, R, x, mid + 1, r);
}
}
int Query(int rt, int x, int l, int r) {
if(l == r) return tre[rt];
int mid = l + r >> 1;
if(mid >= x) return Query(rt << 1, x, l, mid);
else return Query(rt << 1 | 1, x, mid + 1, r);
}
int in[maxn];
int main()
{
int t, n = 0, q;
scanf("%d", &t);
for(int cas = 1; cas <= t; ++cas) {
printf("Case #%d:\n", cas);
for(int i = 1; i <= n; i++) E[i].clear();
tot = 1;
memset(in, 0, sizeof(in));
scanf("%d", &n);
int u, v;
for(int i = 1; i < n; ++i) {
scanf("%d%d", &u, &v);
E[v].push_back(u);
in[u]++;
}
for(int i = 1; i <= n; ++i)
if(in[i] == 0) dfs(i);
Build(1, 1, n);
scanf("%d", &q);
char op[4];
int x, y;
while(q--) {
scanf("%s", op);
if(op[0] == 'C') {
scanf("%d", &x);
// printf("pos = %d\n", pos[x]);
printf("%d\n", Query(1, pos[x], 1, n));
}
else {
scanf("%d%d", &x, &y);
// printf("update : %d - %d\n", pos[x], pos[x] + sz[x] - 1);
Update(1, pos[x], pos[x] + sz[x] - 1, y, 1, n);
}
}
}
}