题意:给定一棵树的公司职员管理图,有两种操作,

第一种是 T x y,把 x 及员工都变成 y,

第二种是 C x 询问 x 当前的数。

析:先把该树用dfs遍历,形成一个序列,然后再用线段树进行维护,很简单的线段树。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 5e4 + 10;
const int mod = 1000000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} vector<int> G[maxn];
int in[maxn], out[maxn];
int cnt;
int sum[maxn<<2], setv[maxn<<2]; void dfs(int u){
in[u] = ++cnt;
for(int i = 0; i < G[u].size(); ++i)
dfs(G[u][i]);
out[u] = cnt;
} void push_down(int rt){
if(setv[rt] == -1) return ;
int l = rt<<1, r = rt<<1|1;
sum[l] = sum[r] = setv[rt];
setv[l] = setv[r] = setv[rt];
setv[rt] = -1;
} void update(int L, int R, int val, int l, int r, int rt){
if(L <= l && r <= R){
sum[rt] = val;
setv[rt] = val;
return ;
}
push_down(rt);
int m = l + r >> 1;
if(L <= m) update(L, R, val, lson);
if(R > m) update(L, R, val, rson);
} int query(int M, int l, int r, int rt){
if(l == r) return sum[rt];
push_down(rt);
int m = l + r >> 1;
return M <= m ? query(M, lson) : query(M, rson);
} int main(){
int T; cin >> T;
for(int kase = 1; kase <= T; ++kase){
scanf("%d", &n);
for(int i = 1; i <= n; ++i) G[i].clear();
memset(in, 0, sizeof in);
for(int i = 1; i < n; ++i){
int u, v;
scanf("%d %d", &u, &v);
G[v].push_back(u);
++in[u];
}
cnt = 0;
for(int i = 1; i <= n; ++i)
if(!in[i]){ dfs(i); break; }
memset(setv, -1, sizeof setv);
memset(sum, -1, sizeof sum); scanf("%d", &m);
char s[5];
printf("Case #%d:\n", kase);
while(m--){
int x, y;
scanf("%s %d", s, &x);
if(s[0] == 'C') printf("%d\n", query(in[x], 1, n, 1));
else {
scanf("%d", &y);
update(in[x], out[x], y, 1, n, 1);
}
}
}
return 0;
}

  

HDU 3974 Assign the task (DFS+线段树)的更多相关文章

  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 (线段树区间修改+构建模型)

    https://cn.vjudge.net/problem/HDU-3974 题意 有一棵树,给一个结点分配任务时,其子树的所有结点都能接受到此任务.有两个操作,C x表示查询x结点此时任务编号,T ...

  4. hdu 3974 Assign the task (线段树+树的遍历)

    Description There is a company that has N employees(numbered from 1 to N),every employee in the comp ...

  5. HDU 3974 Assign the task (DFS序 + 线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3974 给你T组数据,n个节点,n-1对关系,右边的是左边的父节点,所有的值初始化为-1,然后给你q个操 ...

  6. HDU 3974 Assign the task(DFS序+线段树单点查询,区间修改)

    描述There is a company that has N employees(numbered from 1 to N),every employee in the company has a ...

  7. HDU 3974 Assign the task(dfs建树+线段树)

    题目大意:公司里有一些员工及对应的上级,给出一些员工的关系,分配给某员工任务后,其和其所有下属都会进行这项任务.输入T表示分配新的任务, 输入C表示查询某员工的任务.本题的难度在于建树,一开始百思不得 ...

  8. hdu 3974 Assign the task(dfs序上线段树)

    Problem Description There is a company that has N employees(numbered from 1 to N),every employee in ...

  9. HDU 3974 Assign the task 并查集/图论/线段树

    Assign the task Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...

随机推荐

  1. HihoCoder1403 后缀数组一·重复旋律1

    后缀数组一·重复旋律 时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为长度为 N 的数构成的数列. 小Hi ...

  2. angular track by $index

    这个东西配合删除数组时会出现永远删除uoloadPicArr这个数组的最后一个一项 其用法是主要用在当ng-repeat循环时如果其内部的dom元素有id属性存在 可以避免页面出现相同的id  只是一 ...

  3. php system()

    学习源头: https://blog.csdn.net/ltx06/article/details/53992905 system(“nohup ./test.py $s &”); 这个不会在 ...

  4. JDK7和JDK8新特性

    参考:http://www.cnblogs.com/langtianya/p/3757993.html JDK 1.7 新特性 1,switch中可以使用字串了 String s = "te ...

  5. java代码异常处理篇-----循环

    总结:注意一个方法:nextLine();它表示:执行当前行,返回跳过的输入信息. package com.da; import java.util.InputMismatchException; i ...

  6. 杂项:VS调试技巧之附加进程

    ylbtech-杂项:VS调试技巧之附加进程 1. 摘录返回顶部 1. 用过VS一段时间的程序员们相信都有过这种调试经历:每次按下F5进行断点调试时,都要等待好长时间:先让解决方式编译通过,然后启动V ...

  7. Angular常犯的错误

    ng-app="name名称" name名称  == 一定要写对 其次 angular.min导入一定要正确,一定要导入正确的angular.min的库 再次js中要写自调用 (f ...

  8. C++11 引用叠加规则和模板参数类型推导规则

    http://zm8.sm-img2.com/?src=http%3A%2F%2F***%2FArticle%2F38320&uid=57422b713ac761e653af7b327bfd9 ...

  9. UIView显示原理和过程

    一.UIView显示原理         一个控件,UIView之所以可以显示,是因为内部在UIView的内部有一个layer属性作为根图层,根图层上可以放其他子图层,在UIView中所有能够看到的内 ...

  10. c#.net常用字符串函数 字符串常用方法 string

    RegionsStr = RegionsStr.Remove(RegionsStr.LastIndexOf(","), 1);   //去掉最后一个逗号 string html = ...