https://cn.vjudge.net/problem/HDU-3974

题意

有一棵树,给一个结点分配任务时,其子树的所有结点都能接受到此任务。有两个操作,C x表示查询x结点此时任务编号,T x y表示给x结点分配编号为y的任务。

分析

题目读起来就很有区间修改的味道,将一个区间变为一个值。问题在于怎么把这棵树对应到区间上。

对于一个结点,其控制的范围是它的子树,对应区间范围可以看作是以dfs序表示的区间。好像有点绕。。就是给每个结点再对应一个dfs序,然后在dfs时把这个点控制的子树看作是一段连续区间。然后就跑线段树啦。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define random(a, b) rand()*rand()%(b-a+1)+a
#define pi acos(-1)
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
const int maxn = 5e4 + ;
const int maxm = + ;
const int mod = ; struct Edge{
int to,nxt;
Edge(){}
Edge(int x,int y):to(x),nxt(y){}
}e[maxn];
int head[maxn],tot;
int cnt,start[maxn],ed[maxn];
void init(){
tot=cnt=;
memset(head,-,sizeof(head));
}
void addedge(int u,int v){
e[tot]=Edge(v,head[u]);head[u]=tot++;
}
void dfs(int u){
++cnt;
start[u]=cnt;
for(int i=head[u];~i;i=e[i].nxt){
dfs(e[i].to);
}
ed[u]=cnt;
}
struct ND{
int l,r;
int val,lazy;
}tree[maxn<<];
int n,m;
void pushup(int rt){
}
void pushdown(int rt){
if(tree[rt].lazy){
tree[rt<<].lazy=tree[rt<<|].lazy=tree[rt].lazy;
tree[rt<<].val=tree[rt<<|].val=tree[rt].val;
tree[rt].lazy=;
}
}
void build(int rt,int l,int r){
tree[rt].l=l,tree[rt].r=r;
tree[rt].lazy=;
tree[rt].val=-;
if(l==r) return;
int mid=(l+r)>>;
build(rt<<,l,mid);
build(rt<<|,mid+,r);
}
void update(int rt,int L,int R,int val){
if(L<=tree[rt].l&&tree[rt].r<=R){
tree[rt].val=val;
tree[rt].lazy=;
return;
}
pushdown(rt);
int mid=(tree[rt].l+tree[rt].r)>>;
if(mid>=L) update(rt<<,L,R,val);
if(mid<R) update(rt<<|,L,R,val);
} int query(int rt,int x){
if(tree[rt].l==x&&tree[rt].r==x){
return tree[rt].val;
}
pushdown(rt);
int mid=(tree[rt].l+tree[rt].r)>>;
if(mid>=x) return query(rt<<,x);
else return query(rt<<|,x);
}
bool used[maxn];
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int t,cas=;
char op[];
scanf("%d",&t);
while(t--){
scanf("%d",&n);
printf("Case #%d:\n",cas++);
init();
int u,v;
memset(used,false,sizeof(used));
for(int i=;i<n;i++){
scanf("%d%d",&u,&v);
used[u]=true;
addedge(v,u);
}
for(int i=;i<=n;i++)
if(!used[i])
dfs(i);
build(,,cnt);
scanf("%d",&m);
while(m--){
scanf("%s",op);
if(op[]=='C'){
scanf("%d",&u);
printf("%d\n",query(,start[u]));
}else{
scanf("%d%d",&u,&v);
update(,start[u],ed[u],v);
}
}
}
return ;
}

HDU - 3974 Assign the task (线段树区间修改+构建模型)的更多相关文章

  1. hdu 1166 敌兵布阵 线段树区间修改、查询、单点修改 板子题

    题目链接:敌兵布阵 题目: C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视 ...

  2. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  3. HDU.1689 Just a Hook (线段树 区间替换 区间总和)

    HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...

  4. 题解报告:hdu 1698 Just a Hook(线段树区间修改+lazy懒标记的运用)

    Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for m ...

  5. Codeforces Round #442 (Div. 2) E Danil and a Part-time Job (dfs序加上一个线段树区间修改查询)

    题意: 给出一个具有N个点的树,现在给出两种操作: 1.get x,表示询问以x作为根的子树中,1的个数. 2.pow x,表示将以x作为根的子树全部翻转(0变1,1变0). 思路:dfs序加上一个线 ...

  6. poj 2528 线段树区间修改+离散化

    Mayor's posters POJ 2528 传送门 线段树区间修改加离散化 #include <cstdio> #include <iostream> #include ...

  7. HDU 1698 Just a Hook(线段树 区间替换)

    Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...

  8. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  9. (简单) HDU 1698 Just a Hook , 线段树+区间更新。

    Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...

随机推荐

  1. Data truncation: Data too long for column 'content' at row 1

    项目运行报错: Data truncation: Data too long for column 'content' at row 1 是由于字段长度太小导致的 搜索mysql 中text 字段长度 ...

  2. python3 集合set

    set是一种集合的数据类型,使用{}表示 集合中元素是无序的,并且不可重复,集合最重要的作用就是可以去重 set是不可哈希的,set中的元素必须是可哈希的 可以切片,可以迭代 交集.并集.差集.对称差 ...

  3. 【BZOJ4005】[JLOI2015] 骗我呢(容斥,组合计数)

    [BZOJ4005][JLOI2015] 骗我呢(容斥,组合计数) 题面 BZOJ 洛谷 题解 lalaxu #include<iostream> using namespace std; ...

  4. IP地址等价类测试用例

    下面是一个比较完善的设计方案,这个方案中,首先把IP地址分成有效可用的IP地址和有效但不可用的IP地址两个等价类:其中有效可用的IP地址中包括IP地址的A,B,C三类地址,有效但不可用的IP地址包括D ...

  5. BZOJ3133[Baltic2013]ballmachine

    题目描述 https://www.lydsy.com/JudgeOnline/problem.php?id=3133 题解 还是分两个操作来说吧. 先看第一个操作,放球,可以发现,对于祖先节点和后代节 ...

  6. 利用LVS+Keepalived搭建Mysql双主复制高可用负载均衡环境

    应用背景: MySQL复制(主主,主从...)能在保证数据的备份的同时也能够做读写分离分摊系统压力,但是发生单点故障时,需要手动 切换到另外一台主机.LVS和Keppalived可以设定一个VIP来实 ...

  7. D: Starry的神奇魔法(矩阵快速幂)

    题目链接:https://oj.ismdeep.com/contest/Problem?id=1284&pid=3 D: Starry的神奇魔法 Time Limit: 1 s      Me ...

  8. multimap多重映照容器

    //multimap的基本用法 #include<map> #include<iostream> #include<string> using namespace ...

  9. Ubuntu 18.04 安装微信(Linux通用)

    Linux相关的知识:https://www.cnblogs.com/dunitian/p/4822808.html#linux 新增谷歌浏览器添加到桌面的彻底删除:https://www.cnblo ...

  10. navicat primium 快捷键与命令

    1.ctrl+q          打开查询窗口 2.ctrl+/           注释sql语句 3.ctrl+shift +/  解除注释 4.ctrl+r          运行查询窗口的s ...