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. flowable6.4.1+springboot使用dmn

    resources/dmn/strings_1.dmn <?xml version="1.0" encoding="UTF-8"?> <def ...

  2. The Python Challenge 谜题全解(持续更新)

    Python Challenge(0-2) The Python Challengehttp://www.pythonchallenge.com/ 是个很有意思的网站,可以磨练使用python的技巧, ...

  3. Android如何在一个TextView中实现多种文本风格?

     本文选自StackOverflow(简称:SOF)精选问答汇总系列文章之一,本系列文章将为读者分享国外最优质的精彩问与答,供读者学习和了解国外最新技术,本文为大家讲解Android中,如何在一个 ...

  4. 使用matplotlib.pylab绘制分段函数

    1.安装matplotlib pip3 install matplotlib sudo apt install python3-tk 2.分段函数 from pylab import * x = li ...

  5. 全局变量 static变量

    变量 作用域 全局变量( external linkage ) 定义在函数外 Int a=1 作用于整个工程 在连接两个文件时若有两个a会报错 Staic 函数外(internal linkage) ...

  6. String Reconstruction (并查集)

    并查集维护和我这个位置的字母连续的已经被填充的字母能到达的最右边的第一个还没有填充的位置,然后把这个位置填上应该填的东西,然后把这个位置和下一个位置连接起来,如果下一个位置还没有填,我就会把下一个位置 ...

  7. [agc016B][Colorful Hats]

    题目链接 思路 首先,如果没人说谎那么序列中肯定只有一大一小两种数,假设大的数为x,小的数为y.因为对于每个人只有两种情况,要么自己与除自己外的某个人拥有相同的颜色,此时总颜色数就是这个人所能看到的颜 ...

  8. 关于form与表单操作

    form表单自动提交规则 form表单中只有一个type=text的input,在input中按enter键,会自动提交: form表单中有多个type=text的input,且无type=submi ...

  9. matlab图片 latex显示

    matlab图片另存为bmp格式,用在线网站转换格式为png. 莫要直接另存为jpg格式,在latex中显示,否则图片显示的质量不高.

  10. 构造代码块、this关键字、静态变量、静态代码块、主函数

    一.构造代码块: 作用:给对象进行初始化. 特点:对象一经运行就执行(与变量声明时赋初值同级别,此处注意 非法前向引用) 优先于构造函数的执行. 与构造函数的区别: 构造代码块是给所有对象统一初始化. ...