题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3974

题意:给定一棵树,50000个节点,50000个操作,C x表示查询x节点的值,T x y表示更新x节点及其子节点的值为y

大致把边存一下那一棵树来举例子

    2

  3    5

4    1

例如像这样的一棵树,可以将2->1,3->2,4->3,1->4,5->5按照dfs序来编号,然后用线段树进行区间修改,稍微想一想

应该都会了。

#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
const int M = 5e4 + 10;
vector<int>vc[M];
int pre[M] , dig[M] , length[M] , cnt;
void dfs(int pos) {
if(vc[pos].size() == 0) {
return ;
}
int len = vc[pos].size();
for(int i = 0 ; i < len ; i++) {
dig[vc[pos][i]] = ++cnt;
dfs(vc[pos][i]);
length[vc[pos][i]] = cnt;
}
}
struct TnT {
int l , r , num , add;
}T[M << 2];
void build(int l , int r , int p) {
int mid = (l + r) >> 1;
T[p].l = l , T[p].r = r , T[p].add = -1 , T[p].num = -1;
if(T[p].l == T[p].r) {
return ;
}
build(l , mid , p << 1);
build(mid + 1 , r , (p << 1) | 1);
}
void pushdown(int p) {
if(T[p].add != -1) {
T[p << 1].num = T[p].add;
T[(p << 1) | 1].num = T[p].add;
T[p << 1].add = T[p].add;
T[(p << 1) | 1].add = T[p].add;
T[p].add = -1;
}
}
void updata(int l , int r , int p , int ad) {
int mid = (T[p].l + T[p].r) >> 1;
if(T[p].l == l && T[p].r == r) {
T[p].num = ad;
T[p].add = ad;
return ;
}
pushdown(p);
if(mid >= r) {
updata(l , r , p << 1 , ad);
}
else if(mid < l) {
updata(l , r , (p << 1) | 1 , ad);
}
else {
updata(l , mid , p << 1 , ad);
updata(mid + 1 , r , (p << 1) | 1 , ad);
}
}
int query(int pos , int p) {
int mid = (T[p].l + T[p].r) >> 1;
if(T[p].l == T[p].r && T[p].l == pos) {
return T[p].num;
}
pushdown(p);
if(mid >= pos) {
return query(pos , p << 1);
}
else {
return query(pos , (p << 1) | 1);
}
}
int main() {
int t;
int ans = 0;
scanf("%d" , &t);
while(t--) {
int n;
ans++;
scanf("%d" , &n);
for(int i = 0 ; i <= n ; i++) {
vc[i].clear();
pre[i] = -1;
}
for(int i = 0 ; i < n - 1 ; i++) {
int x , y;
scanf("%d%d" , &x , &y);
vc[y].push_back(x);
pre[x] = y;
}
int temp = -1;
for(int i = 1 ; i <= n ; i++) {
if(pre[i] == -1) {
temp = i;
break;
}
}
cnt = 0;
dig[temp] = ++cnt;
length[temp] = n;
dfs(temp);
int m;
scanf("%d" , &m);
char cp[2];
printf("Case #%d:\n" , ans);
build(1 , n , 1);
while(m--) {
int x , y;
scanf("%s" , cp);
if(cp[0] == 'C') {
scanf("%d" , &x);
printf("%d\n" , query(dig[x] , 1));
}
if(cp[0] == 'T') {
scanf("%d%d" , &x , &y);
updata(dig[x] , length[x] , 1 , y);
}
}
}
return 0;
}

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

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

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

  2. HDU 3974 Assign the task 暴力/线段树

    题目链接: 题目 Assign the task Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...

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

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

  4. Assign the task HDU - 3974(dfs序+线段树)

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

  5. 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 ...

  6. HDU 3974 Assign the task(简单线段树)

    Assign the task Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

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

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

  8. HDU - 3974 Assign the task (线段树区间修改+构建模型)

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

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

    题意:给定一棵树的公司职员管理图,有两种操作, 第一种是 T x y,把 x 及员工都变成 y, 第二种是 C x 询问 x 当前的数. 析:先把该树用dfs遍历,形成一个序列,然后再用线段树进行维护 ...

随机推荐

  1. Ping、Traceroute工作原理

    在工作开发过程中,我们经常会使用到ping和traceroute.在这里,我们将细述其工作原理,让你在会用的基础之上理解其内部工作过程. ICMP应用实例--Ping Ping 是 ICMP 的一个重 ...

  2. Unity经典游戏教程之:雪人兄弟

    版权声明: 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客&qu ...

  3. 二叉查找树(查找、插入、删除)——C语言

    二叉查找树 二叉查找树(BST:Binary Search Tree)是一种特殊的二叉树,它改善了二叉树节点查找的效率.二叉查找树有以下性质: (1)若左子树不空,则左子树上所有节点的值均小于它的根节 ...

  4. codeforces1088D_Ehab and another another xor problem交互题

    传送门 一道考验思维的交互题 大致思路就是从最高的二进制位向下询问 代入例子比如: 5 6 6 5 7 4 6 4 讨论一下 交互题的重点学会推理和归纳 #include <bits/stdc+ ...

  5. Unity的弱联网Json数据传输

    注意事项: 关于dictionary转json的工程中遇到一点问题:要手动添加双引号. 关于json转dictionary:同样需要手动去掉双引号,否则添加到dictionary中的字符串会带有双引号 ...

  6. Spring入门编程问题集锦Top10

    我写的一篇文章,希望对spring初学者有所帮助: 1.如何学习Spring? 你可以通过下列途径学习spring: ①. spring下载包中doc目录下的MVC-step-by-step和samp ...

  7. Linux curl 表单登录或提交与cookie使用

    本文主要讲解通过curl 实现表单提交登录.单独的表单提交与表单登录都差不多,因此就不单独说了. 说明:针对curl表单提交实现登录,不是所有网站都适用,原因是有些网站后台做了限制或有其他校验.我们不 ...

  8. MapReduce on Yarn运行原理

    一.概念综述 MapReduce是一种可用于数据处理的编程模型(或计算模型),该模型可以比较简单,但想写出有用的程序却不太容易.MapReduce能将大型数据处理任务分解成很多单个的.可以在服务器集群 ...

  9. 洛谷 P2657 [SCOI2009]windy数

    题意简述 求l~r之间不含前导零且相邻两个数字之差至少为2的正整数的个数 题解思路 数位DP 代码 #include <cstdio> #include <cstring> # ...

  10. Git原理入门简析

    为了获得更好的阅读体验,建议访问原地址:传送门 前言: 之前听过公司大佬分享过 Git 原理之后就想来自己总结一下,最近一忙起来就拖得久了,本来想塞更多的干货,但是不喜欢拖太久,所以先出一版足够入门的 ...