#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define ll long long
using namespace std;
const int MAXN = 50000 + 10;
struct Edge
{
int to, next;
}edge[MAXN];
int head[MAXN], tot;
int S[MAXN], E[MAXN];
int n;
void init()
{
memset(head, -1, sizeof(head));
tot = 0;
n = 0;
}
void AddEdge(int u, int v)
{
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
}
void dfs(int u)
{
S[u] = ++n;
for(int i=head[u];i!=-1;i=edge[i].next)
{
dfs(edge[i].to);
}
E[u] = n;
}
int vis[MAXN];
struct Tree
{
int l, r;
int val;
int lazy;
}tree[MAXN<<2];
void pushdown(int rt)
{
if(tree[rt].lazy)
{
tree[rt<<1].val = tree[rt].val;
tree[rt<<1|1].val = tree[rt].val;
tree[rt<<1].lazy = 1;
tree[rt<<1|1].lazy = 1;
tree[rt].lazy = 0;
}
}
void build(int l, int r, int rt)
{
tree[rt].l = l;
tree[rt].r = r;
tree[rt].val = -1;
tree[rt].lazy = 0;
if(l == r) return ;
int m = (l + r) >> 1;
build(l, m, rt<<1);
build(m + 1, r, rt<<1|1);
}
void update(int l, int r, int val, int rt)
{
if(tree[rt].l == l && tree[rt].r == r)
{
tree[rt].val = val;
tree[rt].lazy = 1;
return ;
}
pushdown(rt);
int m = (tree[rt].l + tree[rt].r) >> 1;
if(r <= m) update(l, r, val, rt<<1);
else if(l > m) update(l, r, val, rt<<1|1);
else
{
update(l, m, val, rt<<1);
update(m + 1, r, val, rt<<1|1);
}
}
int Query(int p, int rt)
{
if(tree[rt].l == p && tree[rt].r == p)
{
return tree[rt].val;
}
pushdown(rt);
int m = (tree[rt].l + tree[rt].r) >> 1;
if(p <= m) return Query(p, rt<<1);
else return Query(p, rt<<1|1);
}
int main()
{
int N, T, kcase = 1;
scanf("%d", &T);
while(T--)
{
printf("Case #%d:\n", kcase++);
init();
scanf("%d", &N);
int u, v;
memset(vis, 0, sizeof(vis));
for(int i=1;i<N;i++)
{
scanf("%d%d", &u, &v);
vis[u] = 1;
AddEdge(v, u);
}
for(int i=1;i<=N;i++)
{
if(!vis[i])
{
dfs(i);
break;
}
}
build(1, N, 1);
int m = 0;
scanf("%d", &m);
char op[10];
while(m--)
{
scanf("%s", op);
if(op[0] == 'C')
{
scanf("%d", &u);
//cout << S[u] << endl;
printf("%d\n", Query(S[u], 1));
}
else
{
scanf("%d%d", &u, &v);
update(S[u], E[u], v, 1);
}
}
}
return 0;
}
HDU 3974 Assign the task(线段树)
最新推荐文章于 2019-09-25 17:40:05 发布