HDU 3974-Assign the task(dfs构建结构+裸线段树)
来源:互联网 发布:java和scala 编辑:程序博客网 时间:2024/05/16 23:52
题意:
一棵树的结构,父节点是老板,子节点是员工,每次给父节点分配的任务,立即会下分到他所有的子节点,有更新和查询命令。
思路:
第一次知道dfs可以用来维护某节点所管理的子节点的范围,因为dfs在回到开始点时,必定已经走过了所有子节点。在构建出一个可管理的区间后,就可以用裸线段树了
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>using namespace std;const int maxn= 50005;struct node{ int left,right; int val,lazy;}tree[maxn*4];int cnt;int st[maxn],ed[maxn];int vis[maxn];vector<int> vec[maxn];void dfs(int u){ ++cnt; vis[u]=1; st[u]=cnt; for(int i=0;i<vec[u].size();i++) dfs(vec[u][i]); ed[u]=cnt;}void build(int i,int left,int right){ tree[i].left=left; tree[i].right=right; tree[i].val=-1; tree[i].lazy=0; if(left==right) return; int mid=(left+right)>>1; build(i<<1,left,mid); build(i<<1|1,mid+1,right);}void push(int i){ tree[i<<1].lazy=tree[i<<1|1].lazy=1; tree[i<<1].val=tree[i<<1|1].val=tree[i].val; tree[i].lazy=0;}void update(int i,int left,int right,int w){ if(tree[i].left==left&&tree[i].right==right) { tree[i].lazy=1; tree[i].val=w; return ; } if(tree[i].lazy) { push(i); } int mid=(tree[i].left+tree[i].right)>>1; if(right<=mid) { update(i<<1,left,right,w); } else if(left>mid) { update(i<<1|1,left,right,w); } else { update(i<<1,left,mid,w); update(i<<1|1,mid+1,right,w); }}int ans=0;void query(int i,int aim){ if(tree[i].left==tree[i].right&&tree[i].left==aim) { ans=tree[i].val; return ; } if(tree[i].lazy) { push(i); } int mid=(tree[i].left+tree[i].right)>>1; if(aim<=mid) query(i<<1,aim); else query(i<<1|1,aim);}int main(){ int n,t; scanf("%d",&t); for(int ic=1;ic<=t;ic++) { scanf("%d",&n); cnt=0; int u,v; memset(vis,0,sizeof(vis)); memset(st,0,sizeof(st)); memset(ed,0,sizeof(ed)); for(int i=1;i<=n;i++) vec[i].clear(); for(int i=1;i<n;i++) { scanf("%d%d",&v,&u); vec[u].push_back(v); vis[v]=1; } for(int i=1;i<=n;i++) { if(!vis[i]) { dfs(i); break; } } build(1,1,cnt); char s[5]; int m; printf("Case #%d:\n",ic); scanf("%d",&m); while(m--) { int id,w; scanf("%s",s); if(s[0]=='C') { scanf("%d",&id); ans=-1; query(1,st[id]); printf("%d\n",ans); } else { scanf("%d%d",&id,&w); update(1,st[id],ed[id],w); } } } return 0;}
0 0
- HDU 3974-Assign the task(dfs构建结构+裸线段树)
- HDU 3974 Assign the task (线段树+dfs序)
- HDU 3974 Assign the task(DFS建树+线段树)
- HDU 3974 Assign the task (DFS + 线段树)
- HDU 3974 Assign the task (线段树)
- HDU 3974 Assign the task(线段树)
- hdu 3974 Assign the task (线段树)
- hdu 3974 Assign the task
- hdu 3974 Assign the task
- hdu 3974 Assign the task
- HDU 3974 Assign the task
- HDU 3974 Assign the task
- hdu 3974 Assign the task
- hdu 3974 Assign the task
- Assign the task(hdu 3974)
- HDU-3974-Assign the task(线段树维护dfs序)
- HDU 3974 Assign the task(dfs序 + 线段树区间赋值,单点查询)
- HDU-3974 Assign the task题解报告【dfs序+线段树】
- 机器学习
- Linux上面增加swap空间
- 当数据量较大时,两个tab切换的效率问题
- python-22-构建xml文档
- C语言条件编译详解
- HDU 3974-Assign the task(dfs构建结构+裸线段树)
- 收集service源码分析文章
- 1074: 人见人爱A+B
- 字符编码笔记:ASCII,Unicode和UTF-8
- python-23-读写excel文件
- SharePreference 跨进程使用测试
- bzoj1084: [SCOI2005]最大子矩阵
- SSAS:OLE DB 错误: OLE DB 或 ODBC 错误 : Login failed for user 'NT Service\MSSQLServerOLAPService'
- c++数据结构 共享栈的实现