首页 > 其他 > 详细

hdu3974(dfs序+区间修改)

时间:2021-03-27 12:38:13      阅读:27      评论:0      收藏:0      [点我收藏+]

题目描述:

Assign the task

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


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
题意:一颗树,有时会对整颗子树进行修改操作,有时会查询某节点的值
思路: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;
}

 

hdu3974(dfs序+区间修改)

原文:https://www.cnblogs.com/MYMYACMer/p/14585128.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!