1. 程式人生 > >HDU - 3974 Assign the task (線段樹區間修改+構建模型)

HDU - 3974 Assign the task (線段樹區間修改+構建模型)

add algorithm als struct 它的 stream time ons pda

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 + 10; const int maxm = 200000 + 10; const int mod = 998244353; 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=0; memset(head,-1,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<<2]; int n,m; void pushup(int rt){ } void pushdown(int rt){ if(tree[rt].lazy){ tree[rt<<1].lazy=tree[rt<<1|1].lazy=tree[rt].lazy; tree[rt<<1].val=tree[rt<<1|1].val=tree[rt].val; tree[rt].lazy=0; } } void build(int rt,int l,int r){ tree[rt].l=l,tree[rt].r=r; tree[rt].lazy=0; tree[rt].val=-1; if(l==r) return; int mid=(l+r)>>1; build(rt<<1,l,mid); build(rt<<1|1,mid+1,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=1; return; } pushdown(rt); int mid=(tree[rt].l+tree[rt].r)>>1; if(mid>=L) update(rt<<1,L,R,val); if(mid<R) update(rt<<1|1,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)>>1; if(mid>=x) return query(rt<<1,x); else return query(rt<<1|1,x); } bool used[maxn]; int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); // freopen("output.txt", "w", stdout); #endif int t,cas=1; char op[5]; 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=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); build(1,1,cnt); scanf("%d",&m); 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],ed[u],v); } } } return 0; }

HDU - 3974 Assign the task (線段樹區間修改+構建模型)