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

第一种是 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. mysql 在Windows下自动备份

    1.一般备份方法主要为两种: 直接保存数据库data文件夹 mysqldump 用sql命令备份(文件存放目录必须存在) 首先cmd进入MySQL\bin,然后mysqldump --default- ...

  2. Swap Adjacent Elements

    You have an array a consisting of n integers. Each integer from 1 to n appears exactly once in this ...

  3. loj 6084.「美团 CodeM 资格赛」跳格子

    题目: link 题解: 尽量走\(a\). 只要保证走\(a\)后到达的点一定可以到终点就可以走. 所以从终点开始\(dfs\)出所有能够到达终点的点. 然后再从起点开始\(dfs\)路径即可. 如 ...

  4. 【LeetCode】002 Add Two Numbers

    题目: You are given two non-empty linked lists representing two non-negative integers. The digits are ...

  5. vertex shader must minimally write all four components of POSITION

    Though the POSITION semantic must be written out by the vertex shader, it cannot be read in by the p ...

  6. BZOJ4009:[HNOI2015]接水果(整体二分版)

    浅谈离线分治算法:https://www.cnblogs.com/AKMer/p/10415556.html 题目传送门:https://lydsy.com/JudgeOnline/problem.p ...

  7. 自己写的工具:把Evernote(印象笔记)的笔记导入到博客(Blog)中

    Evernote是个强大的工具, 这个伴随了我快4年的工具让我积累好多笔记.但是,如何把evernote(印象笔记)中的笔记发布到博客中呢? 自己空闲时候用python 3写了个工具Evernote2 ...

  8. Python学习笔记 - 用VSCode写python的正确姿势

    最近在学习python,之前一直用notepad++作为编辑器,偶然发现了VScode便被它的颜值吸引.用过之后发现它启动快速,插件丰富,下载安装后几乎不用怎么配置就可以直接使用,而且还支持markd ...

  9. IE版本的标准定义

    解决方案 首页加代码把IE浏览器的标准改了,无论客户用的什么IE,都是按照IE7的标准来的. <meta http-equiv="X-UA-Compatible" conte ...

  10. POJ2976(最大化平均值)

    Dropping tests Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9329   Accepted: 3271 De ...