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. 后台CRUD管理jqGrid 插件下载、使用、demo演示

    jqGrid:demo?version=5.2.1download src: http://www.trirand.com/blog/ github:https://github.com/tonyto ...

  2. Mysql 操作技巧

    复制表结构 + 表数据Mysql> create tables t2 like t1;Mysql> insert into t2 select * from t1; mysql 索引a.A ...

  3. 【LOJ6053】简单的函数(min_25筛)

    题面 LOJ 题解 戳这里 #include<iostream> #include<cstdio> #include<cstdlib> #include<cs ...

  4. 【BZOJ4543】Hotel加强版(长链剖分)

    [BZOJ4543]Hotel加强版(长链剖分) 题面 BZOJ,没有题面 洛谷,只是普通版本 题解 原来我们的\(O(n^2)\)做法是设\(f[i][j]\)表示以\(i\)为根的子树中,距离\( ...

  5. HR_Hash Tables: Ransom Note

    1 题目重点:whole words | case-sensitive #!/bin/python3 import math import os import random import re imp ...

  6. luogu5021 [NOIp2018]赛道修建 (二分答案+dp(贪心?))

    首先二分一下答案,就变成了找长度>=m的 不相交的路径的个数 考虑到在一个子树中,只有一个点能出这个子树去和别的点搞 所以我这个子树里尽量自我满足是不会有坏处的 而且要在自我满足数最大的条件下, ...

  7. CDQ分治与整体二分学习笔记

     CDQ分治部分 CDQ分治是用分治的方法解决一系列类似偏序问题的分治方法,一般可以用KD-tree.树套树或权值线段树代替. 三维偏序,是一种类似LIS的东西,但是LIS的关键字只有两个,数组下标和 ...

  8. 华东交通大学2018年ACM“双基”程序设计竞赛部分题解

    链接:https://ac.nowcoder.com/acm/contest/221/C来源:牛客网 C-公式题(2) 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其 ...

  9. yd的汇总

    因为是我这只蒟蒻个人的汇总嘛,可能有些奇♂怪的东西或者不规范的语言出现啦,见谅见谅 搬了一些到知识汇总里,删了一些过时和无用的,少了好多=.= 1.STL_queue 经实践验证,!qs.empty( ...

  10. HTTPS笔记:使用 SSLEngine 为 aioserver 服务器提供 SSL 访问支持

    现在 HTTPS 的普及率是越来越高,闲来无事,花了二三天时间,为五年前写的 aioserver 服务器提供了 SSL 访问支持. 查看网上资料,为了提高服务器的高并发,建议使用:SSLEngine ...