HDU - 3974 Assign the task (线段树区间修改+构建模型)
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 (线段树区间修改+构建模型)的更多相关文章
- hdu 1166 敌兵布阵 线段树区间修改、查询、单点修改 板子题
题目链接:敌兵布阵 题目: C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视 ...
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- HDU.1689 Just a Hook (线段树 区间替换 区间总和)
HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...
- 题解报告: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 ...
- 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序加上一个线 ...
- poj 2528 线段树区间修改+离散化
Mayor's posters POJ 2528 传送门 线段树区间修改加离散化 #include <cstdio> #include <iostream> #include ...
- HDU 1698 Just a Hook(线段树 区间替换)
Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...
- HDU 1556 Color the ball(线段树区间更新)
Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...
- (简单) HDU 1698 Just a Hook , 线段树+区间更新。
Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...
随机推荐
- THUSC2017 Day1题解
THUSC2017 Day1题解 巧克力 题目描述 "人生就像一盒巧克力,你永远不知道吃到的下一块是什么味道." 明明收到了一大块巧克力,里面有若干小块,排成n行m列.每一小块都有 ...
- 诶西,JavaScript学习记录。。。。。。
由于大学课程缘故,老师巨爱叫人问问题,还记分呢,随便记录一下Js的学习情况,以后复习什么的也比较方便吧...... 开始咯,就按照C语言学习那样的方法来吧! ===================== ...
- 解决忘记mysql中的root用户密码问题
如果忘了数据库中的root密码,无法登陆mysql. 解决步骤: 1. 使用“--skip-grant-tables”启动数据库 ~]#systemctl stop mysql ~]#mysqld_s ...
- centos7/RHEL7最小化系统安装gnome图形界面
应用场景:对于比较熟悉linux系统的用户来说,全命令行系统可能来的比较简单明了高效,也存在某些情况下需要有像winodws下弹出对话框的情形需求,或者对于初识linux习惯windows界面的用户来 ...
- Gym - 100989E
Islam is usually in a hurry. He often types his passwords incorrectly. He hates retyping his passwor ...
- 绝对音乐No.1
最近儿子在练天空之城钢琴曲.为了方便他听久石让的原版,绝对做张cd.另外加入了自己比较喜欢的几首乐曲.在家音响上聆听时发现,不管是中国乐曲,还是西洋乐,都很美,耳朵都出油了.放到网盘供喜爱之人欣赏,喜 ...
- How To Install WildFly as a Service on Linux
Installing WildFly as a service on Linux has multiple advantages like automatic start on system boot ...
- [WC2019] 数树
[WC2019] 数树 Zhang_RQ题解(本篇仅概述) 前言 有进步,只做了半天.... 一道具有极强综合性的数数好题! 强大的多合一题目 精确地数学推导和耐心. 有套路又不失心意. 融合了: 算 ...
- 洛谷P3620 数据备份
好吧,我一开始说这是个神级数据结构毒瘤题,后来改成神题了. 主要是贪心做法的巧妙转化: 首先发现选择的一对必须相邻,于是我们搞出差分. 然后考虑选取最小值时,最小值两侧的数要么同时选,要么都不选. 然 ...
- 洛谷P5112 FZOUTSY
卡map还行.....手写hash表即可. 我一开始以为这个k会变......在sam上想各种奇技淫巧. k不变就是问一段区间有多少对长度为k的子串相同. 然后hash把子串转化为数字,就是区间有多少 ...