/*
translation:一个公司里面每个员工都有一个顶头上司,一旦给某个员工分配任务后,这个员工以及该员工的所有下属都在做该任务。有若干操作,分配给员工任务以及查询该员工正在执行的任务。
solution:线段树,时间戳一般化很明显该公司的所有员工间的关系可以用一颗多叉树来表示。然后就是dfs给这棵树打上时间戳。根据新分配的id号码将其节点对应映射到线段树上面。这样分配任务就相当与更新一段连续的节点,查询任务相当与单点查询。
*/
#include<bits/stdc++.h>
using namespace std;
#define N 50010
struct Edge
{int to, nxt;Edge(){};Edge(int _to, int _nxt):to(_to), nxt(_nxt){};
}edge[N];
struct SegTree
{int l, r, val, lazy;
}seg[N<<2];
int head[N], start[N], en[N], tol, cnt;
bool used[N];
void init()
{tol=cnt=0;memset(head, -1, sizeof(head));
}
void addedge(int u, int v)
{edge[tol]=Edge(v, head[u]);head[u]=tol++;
}
void dfs(int u)
{++cnt;start[u]=cnt;for(int i=head[u]; i!=-1; i=edge[i].nxt){dfs(edge[i].to);}en[u]=cnt;
}
void build(int l, int r, int rt)
{seg[rt].l=l;seg[rt].r=r;seg[rt].val=-1;seg[rt].lazy=0;if(l==r) return ;int mid=(l+r)>>1;build(l, mid, rt<<1);build(mid+1, r, rt<<1|1);
}
void pushdown(int rt)
{if(seg[rt].lazy){seg[rt].lazy=0;seg[rt<<1].val=seg[rt<<1|1].val=seg[rt].val;seg[rt<<1].lazy=seg[rt<<1|1].lazy=1;}
}
void update(int rt, int l, int r, int val)
{if(l<=seg[rt].l&&r>=seg[rt].r){seg[rt].val=val;seg[rt].lazy=1;return ;}pushdown(rt);int mid=(seg[rt].l+seg[rt].r)>>1;if(r<=mid) update(rt<<1, l, r, val);else if(l>mid) update(rt<<1|1, l, r, val);else{update(rt<<1, l, mid, val);update(rt<<1|1, mid+1, r, val);}
}
int query(int rt, int u)
{if(seg[rt].l==u&&seg[rt].r==u) return seg[rt].val;pushdown(rt);int mid=(seg[rt].l+seg[rt].r)>>1;if(u<=mid) return query(rt<<1, u);else return query(rt<<1|1, u);
}
int main()
{int T, u, v, n, m;scanf("%d", &T);for(int kase=1; kase<=T; kase++){init();memset(used, false, sizeof(used));scanf("%d", &n);for(int i=1; i<n; i++){scanf("%d%d", &u, &v);used[u]=true;addedge(v, u);}for(int i=1; i<=n; i++)if(!used[i]){dfs(i);break;}build(1, cnt, 1);char op[10];scanf("%d", &m);printf("Case #%d:\n", kase);while(m--){scanf("%s", op);if(op[0]=='C'){scanf("%d", &u);printf("%d\n", query(1, start[u]));}else{scanf("%d%d", &u, &v);update(1, start[u], en[u], v);}}}return 0;
}
详细解决方案
HDU3974Assign the task(DFS序+线段树)
热度:68 发布时间:2023-12-06 19:24:50.0
相关解决方案
- 任务调度-task schedular
- Task's如何插到字符型里
- : 一个 Execute SQL Task 的 ResultSet 怎么传递到一个 For-each Container 中使用
- Android中Activity的启动流程跟组织管理方式(Backstack、Task)
- Android错误总结-ActivityManager: Warning: Activity not started,its current task has been brought to th
- Android错误总结-ActivityManager: Warning: Activity not started, its current task has been brought to
- Android上affinities和任务(task)
- Task.FromResult应用场景举例来说
- SQL Agent job, Windows Task, 跟Oracle job管理
- Flink 1.12 Task 故障恢复
- hdu 4907 Task schedule
- * What went wrong: Execution failed for task ':app:lintVitalRelease'. Lint found fatal errors whil
- Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'. java.
- Execution failed for task ‘:app:transformClassesWithMultidexlistForRelease
- Warning: Task “server“ not found. Use --force to continue.Aborted due to warnings.
- 关于android studio打开后自动弹出:Error:Execution failed for task ':app:preDebugAndroidTestBuild'
- 微信小程序开发Invoking Page() in async task.
- Continual and Multi-task Reinforcement Learning With Shared Episodic Memory
- uva 10795 A Different Task
- HDU3974Assign the task(DFS序+线段树)
- multi-class, multi-label, multi-task classfication
- SSIS PACKAGE excute sql task 选 single row 模式的一个bug
- C#并行开发_Thread/ThreadPool, Task/TaskFactory, Parallel
- HDU 4907 Task schedule(标记+二分或直接处理也行)BestCoder Round #3
- Codeforces 959D D. Mahmoud and Ehab and another array construction task
- 故障:在 Windows Server 2008 中不能创建 Task
- 贪心 hdu4864 Task
- Spark Streaming中序列化问题:org.apache.spark.SparkException: Task not serializable
- HDU - 3974 Assign the task(线段树+dfs序)
- Ant的任务(task)