KuangBing线段树--分配任务 && DFS序

分析:
树形结构转换为线性结构(DFS序)
DFS序看这里
#include <bits/stdc++.h>
using namespace std;
#define mst(x, y) memset(x, y, sizeof x)
#define endl '\n'
#define INF LONG_LONG_MAX
#define pb push_back
#define x first
#define y second
#define int long long
#define Lson u << 1, l, mid
#define Rson u << 1 | 1, mid + 1, r
#define FAST ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
const int N = 50010, MOD = 1e9 + 7;
const double EPS = 1e-6;
typedef pair<int, int> PII;
typedef unordered_map<int, int> Ump;
int T;
int n, m;
vector<int> g[N];
int in[N], out[N];
bool st[N];
int root, tamp;
int Case;
struct Node
{
int l, r;
int f, lazy;
} tr[N << 2];
void dfs(int u, int fa)
{
if (u == fa)
return;
in[u] = ++tamp;
for (auto t : g[u])
dfs(t, u);
out[u] = tamp;
}
void build(int u, int l, int r)
{
if (l == r)
tr[u] = {l, r, -1, -1};
else
{
tr[u] = {l, r, -1, -1};
int mid = l + r >> 1;
build(Lson), build(Rson);
}
}
void pushdown(int u)
{
if (tr[u].lazy != -1)
{
tr[u << 1].lazy = tr[u << 1 | 1].lazy = tr[u].lazy;
tr[u << 1].f = tr[u << 1 | 1].f = tr[u].lazy;
tr[u].lazy = -1;
}
}
void modify(int u, int l, int r, int k)
{
if (tr[u].l >= l && tr[u].r <= r)
{
tr[u].f = tr[u].lazy = k;
}
else
{
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
if (l <= mid)
modify(u << 1, l, r, k);
if (r > mid)
modify(u << 1 | 1, l, r, k);
}
}
Node query(int u, int pos)
{
if (tr[u].l == pos && tr[u].r == pos)
return tr[u];
else
{
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
if (pos <= mid)
return query(u << 1, pos);
else
return query(u << 1 | 1, pos);
}
}
void solve()
{
tamp = 0;
Case++;
cout << "Case #" << Case << ":" << endl;
mst(st, false);
cin >> n;
build(1, 1, n);
for (int i = 1; i <= n; i++)
g[i].clear();
for (int i = 1, u, v; i < n; i++)
{
cin >> u >> v;
g[v].pb(u);
st[u] = true;
}
for (int i = 1; i <= n; i++)
if (!st[i])
root = i;
dfs(root, -1);
cin >> m;
string op;
while (m--)
{
int a, b;
cin >> op;
if (op == "C")
{
cin >> a;
cout << query(1, in[a]).f << endl;
}
else
{
cin >> a >> b;
modify(1, in[a], out[a], b);
}
}
}
signed main()
{
FAST;
// T = 1;
cin >> T;
while (T--)
solve();
return 0;
}
相反的:由DFS序推出树形结构
#include <bits/stdc++.h>
using namespace std;
#define mst(x, y) memset(x, y, sizeof x)
#define endl '\n'
#define INF LONG_LONG_MAX
#define pb push_back
#define x first
#define y second
#define int long long
#define Lson u << 1, l, mid
#define Rson u << 1 | 1, mid + 1, r
#define FAST ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
const int N = 2000010, MOD = 1e9 + 7;
const double EPS = 1e-6;
typedef pair<int, int> PII;
typedef unordered_map<int, int> Ump;
int T;
int n;
int idx = 1;
int a[N], w[N];
struct P
{
int u, v;
} p[N];
bool cmp(P a, P b)
{
if (a.u == b.u)
return a.v < b.v;
return a.u < b.u;
}
void dfs(int u)
{
w[u]--;
while (w[u])
{
int t = a[++idx];
p[idx - 1] = {u, t};
w[u] -= w[t];
dfs(t);
}
}
void solve()
{
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++)
cin >> w[i];
dfs(a[1]);
for (int i = 1; i < idx; i++)
{
if (p[i].u > p[i].v)
swap(p[i].u, p[i].v);
}
sort(p + 1, p + idx, cmp);
for (int i = 1; i < idx; i++)
cout << p[i].u << " " << p[i].v << endl;
}
signed main()
{
FAST;
T = 1;
// cin >> T;
while (T--)
solve();
return 0;
}

浙公网安备 33010602011771号