HDU 3974 Assign the task (DFS + 线段树)
2017-11-03 11:49
316 查看
加2017新生赛(点击红色的Registerring) |
Assign the taskTime Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3816 Accepted Submission(s): 1581 Problem Description There is a company that has N employees(numbered from 1 to N),every employee in the company has a immediate boss (except for the leader of whole company).If you are the immediate boss of someone,that person is your subordinate, and all his subordinates are your subordinates as well. If you are nobody's boss, then you have no subordinates,the employee who has no immediate boss is the leader of whole company.So it means the N employees form a tree. The company usually assigns some tasks to some employees to finish.When a task is assigned to someone,He/She will assigned it to all his/her subordinates.In other words,the person and all his/her subordinates received a task in the same time. Furthermore,whenever a employee received a task,he/she will stop the current task(if he/she has) and start the new one. Write a program that will help in figuring out some employee’s current task after the company assign some tasks to some employee. Input The first line contains a single positive integer T( T <= 10 ), indicates the number of test cases. For each test case: The first line contains an integer N (N ≤ 50,000) , which is the number of the employees. The following N - 1 lines each contain two integers u and v, which means the employee v is the immediate boss of employee u(1<=u,v<=N). The next line contains an integer M (M ≤ 50,000). The following M lines each contain a message which is either "C x" which means an inquiry for the current task of employee x or "T x y"which means the company assign task y to employee x. (1<=x<=N,0<=y<=10^9) Output For each test case, print the test case number (beginning with 1) in the first line and then for every inquiry, output the correspond answer per line. Sample Input 1 5 4 3 3 2 1 3 5 2 5 C 3 T 2 1 C 3 T 3 2 C 3 Sample Output Case #1: -1 1 2 |
给 个树 关系图, 每次 修改和查询, 查询 当前点 的状态,
实质为区间修改 单点查询
【思路】
DFS 先将树的关系 列出, 然后 区间线段树
【代码实现】
//#include <bits/stdc++.h>
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <stdlib.h>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <vector>
#define mem(a,b) memset(a,b,sizeof(a))
#define findx(x) lower_bound(b+1,b+1+bn,x)-b
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w",stdout)
#define S1(n) scanf("%d",&n)
#define SL1(n) scanf("%I64d",&n)
#define S2(n,m) scanf("%d%d",&n,&m)
#define SL2(n,m) scanf("%I64d%I64d",&n,&m)
#define Pr(n) printf("%d\n",n)
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
using namespace std;
typedef long long ll;
const double PI=acos(-1);
const int INF=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int MAXN=50010;
const int MOD=1e9+7;
const int mod=1e9+7;
int add[MAXN<<2],sum[MAXN<<2];
int head[MAXN],flag[MAXN];
int le[MAXN],ri[MAXN];
int cot,key;
struct node
{
int v,next;
}edge[MAXN];
void init()
{
cot=0;
mem(head,-1);
}
void add_edge(int u,int v)
{
edge[cot].v=v;
edge[cot].next=head[u];
head[u]=cot++;
}
void PushDown(int rt)
{
if(add[rt]!=-1)
{
sum[rt<<1]=add[rt];
sum[rt<<1|1]=add[rt];
add[rt<<1]=add[rt];
add[rt<<1|1]=add[rt];
add[rt]=-1;
}
}
void dfs(int rt)
{
le[rt]=++key;
for(int i=head[rt];i!=-1;i=edge[i].next)
dfs(edge[i].v);
ri[rt]=key;
}
void build(int rt,int l,int r)
{
sum[rt]=add[rt]=-1;
if(l==r)
return;
int mid=(l+r)>>1;
build(lson);
build(rson);
}
void update(int rt,int l,int r,int L,int R,int C)
{
if(L<=l&&R>=r)
{
sum[rt]=C;
add[rt]=C;
return;
}
PushDown(rt);
int mid=(l+r)>>1;
if(L<=mid) update(lson,L,R,C);
if(R>mid) update(rson,L,R,C);
}
int query(int rt,int l,int r,int L,int R)
{
if(L<=l&&R>=r)
{
return sum[rt];
}
PushDown(rt);
int mid=(l+r)>>1;
int ans=0;
if(L<=mid) ans+=query(lson,L,R);
if(R>mid) ans+=query(rson,L,R);
return ans;
}
int main()
{
int t;
int n,m,q,x,y;
int cont=0;
cin>>t;
while(t--)
{
cin>>n;
init();
mem(flag,0);
mem(ri,0);
mem(le,0);
for(int i=1;i<n;i++)
{
scanf("%d %d",&x,&y);
add_edge(y,x);
flag[x]=1;
}
key=0;
for(int i=1;i<=n;i++)
{
if(!flag[i])
{
dfs(i);
break;
}
}
build(1,1,key);
cin>>m;
printf("Case #%d:\n",++cont);
while(m--)
{
char op[5];
scanf("%s",op);
if(op[0]=='C')
{
scanf("%d",&x);
printf("%d\n",query(1,1,key,le[x],le[x]));
}
else
{
scanf("%d %d",&x,&y);
//printf("%d %d\n",le[x],ri[x]);
update(1,1,key,le[x],ri[x],y);
}
}
}
return 0;
}
123
相关文章推荐
- HDU 3974 Assign the task(dfs时间戳+线段树成段更新)
- HDU-3974-Assign the task(线段树维护dfs序)
- J - Assign the task - hdu 3974(DFS建树+简单线段树)
- HDU - 3974 Assign the task(线段树区间更新+DFS序)
- HDU 3974 Assign the task(dfs时间戳+线段树成段更新)
- HDU 3974 Assign the task (DFS序 + 线段树)
- HDU 3974-Assign the task(dfs构建结构+裸线段树)
- Assign the task HDU - 3974 (dfs + 线段树)
- 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(DFS建树+线段树)
- HDU 3794 Assign the task (时间戳dfs序线段树)
- HDU 3974 Assign the task (并查集 || 线段树)
- HDU 3974 Assign the task(线段树 单点更新+lazy)
- HDU 3974 Assign the task 线段树(树映射到区间)
- hdu 3974 Assign the task 线段树(时间戳)
- HDU - 3974 Assign the task (dfs序+区间更新 单点查询)
- hdu 3974 Assign the task (线段树)