CodeForces 893F Subtree Minimum Query (主席树+BFS|DFS)

题目链接:http://codeforces.com/problemset/problem/893/F

题目大意

给定一棵树和根,每个节点上
都有权值,先给定若干个查询(强制在线,要求在题目中)
(x,k),问x节点的子树中其距离x不超过k的节点权值的最小值,
其中x自己规定距离为0.

题目分析 

思维蛮巧妙的主席树了,,
我也是参照了别人的思路才恍然大悟,
在数据结构的灵活使用方面还远远不够。。。
主席树本质上就是时间影响的叠加,所以关键是维护的前缀性质和时间序。
考虑对什么建立时间序,在题目中,其实把dp式子列一下就发现,
其影响是随深度变化而转转移的,那么我们试着以深度为时间序,
这里我们要把它映射成单值,详见代码,那么我们往这个时间序里丢的就是
节点数值了,但是什么又是节点的下标呢?以什么建立系来体现
时间关系呢?这里DFS序又被灵活用起来了,,太巧妙了。。。
不难发现我们对于深度d的树,假设我们一直以dfs序的左值建坐标丢值,
那么我们在深度d的树中可以通过节点x的DFS序来查询到,在这个深度之前的
在序区间中的所有点,这样就符合题目建模要求了。

#include<bits/stdc++.h>
using namespace std;

#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long

#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<int,int>
#define fi first
#define se second
#define mk(x,y) make_pair(x,y)
const int mod=1e9+7;
const int maxn=2e5+5;
const int inf=1e9+7;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
/*
题目大意:
给定一棵树和根,每个节点上
都有权值,先给定若干个查询(强制在线,要求在题目中)
(x,k),问x节点的子树中其距离x不超过k的节点权值的最小值,
其中x自己规定距离为0.

题目分析:
思维蛮巧妙的主席树了,,
我也是参照了别人的思路才恍然大悟,
在数据结构的灵活使用方面还远远不够。。。
主席树本质上就是时间影响的叠加,所以关键是维护的前缀性质和时间序。
考虑对什么建立时间序,在题目中,其实把dp式子列一下就发现,
其影响是随深度变化而转转移的,那么我们试着以深度为时间序,
这里我们要把它映射成单值,详见代码,那么我们往这个时间序里丢的就是
节点数值了,但是什么又是节点的下标呢?以什么建立系来体现
时间关系呢?这里DFS序又被灵活用起来了,,太巧妙了。。。
不难发现我们对于深度d的树,假设我们一直以dfs序的左值建坐标丢值,
那么我们在深度d的树中可以通过节点x的DFS序来查询到,在这个深度之前的
在序区间中的所有点,这样就符合题目建模要求了。
*/
int n,r,m,x,y,maxv,last;
int a[maxn],v[maxn];
int pl[maxn],pr[maxn],dep[maxn];///DFS序
vector<int> g[maxn];
int rt[maxn],ls[maxn*20],rs[maxn*20],sum[maxn*20],tot=0;
void build(int& o,int l,int r){
    o=++tot;
    sum[o]=inf;
    if(l==r) return;
    int mid=l+r>>1;
    build(ls[o],l,mid);
    build(rs[o],mid+1,r);
}
void update(int& o,int last,int l,int r,int p,int v){
    o=++tot;
    ls[o]=ls[last],rs[o]=rs[last];
    if(l==r) {sum[o]=v;return;}
    int mid=l+r>>1;
    if(p<=mid) update(ls[o],ls[o],l,mid,p,v);
    else update(rs[o],rs[o],mid+1,r,p,v);
    sum[o]=min(sum[ls[o]],sum[rs[o]]);
    return ;
}
int query(int o,int l,int r,int L,int R){
    if(L<=l&&r<=R) return sum[o];
    int mid=l+r>>1,ans=inf;
    if(L<=mid) ans=min(ans,query(ls[o],l,mid,L,R));
    if(mid<R) ans=min(ans,query(rs[o],mid+1,r,L,R));
    return ans;
}
///深搜
void dfs(int u,int pre){
    pl[u]=++tot;
    dep[u]=dep[pre]+1;
    maxv=max(maxv,dep[u]);
    for(int i=0;i<g[u].size();i++){
        int v=g[u][i];
        if(v==pre) continue;
        dfs(v,u);
    }
    pr[u]=tot;
}
///广搜
int vis[maxn],mp[maxn];
void bfs(){
    int tmp=0;
    queue<int> p;p.push(r);
    while(!p.empty()){
        int tp=p.front();p.pop();
        if(vis[tp]) continue;
        else vis[tp]=1;
        update(rt[tmp+1],rt[tmp],1,2*n,pl[tp],a[tp]);
        mp[dep[tp]]=++tmp;
        for(int i=0;i<g[tp].size();i++){
            if(vis[g[tp][i]]) continue;
            p.push(g[tp][i]);
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin>>n>>r;
    rep(i,1,n+1) cin>>a[i];
    rep(i,1,n){
        cin>>x>>y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    dfs(r,0);tot=0;
    build(rt[0],1,n<<1);
    bfs();
    cin>>m;
    rep(i,0,m){
        cin>>x>>y;
        x=(x+last)%n+1,y=(y+last)%n;
        ///cout<<min(dep[x]+y,maxv)<<endl;
        last=query(rt[mp[min(dep[x]+y,maxv)]],1,n<<1,pl[x],pr[x]);
        cout<<last<<'\n';
    }
    return 0;
}

 

Sure, here's the previous implementation of BST: ```python class Node: def __init__(self, key): self.left = None self.right = None self.val = key class BST: def __init__(self): self.root = None def insert(self, key): self.root = self._insert(self.root, key) def _insert(self, node, key): if node is None: return Node(key) else: if key < node.val: node.left = self._insert(node.left, key) else: node.right = self._insert(node.right, key) return node def search(self, key): return self._search(self.root, key) def _search(self, node, key): if node is None or node.val == key: return node elif key < node.val: return self._search(node.left, key) else: return self._search(node.right, key) ``` And here's the implementation of `range_search`: ```python def range_search(bst, r): result = [] _range_search(bst.root, r[0], r[1], result) return result def _range_search(node, min_val, max_val, result): if node is None: return if min_val <= node.val <= max_val: _range_search(node.left, min_val, max_val, result) result.append(node.val) _range_search(node.right, min_val, max_val, result) elif node.val < min_val: _range_search(node.right, min_val, max_val, result) else: _range_search(node.left, min_val, max_val, result) ``` The `range_search` function takes a BST and a range `r` as input. It initializes an empty list `result` which will contain the nodes within the range. `_range_search` is a recursive helper function. It takes a node, a minimum value `min_val`, a maximum value `max_val`, and the `result` list as input. If the node is within the range, it recursively calls itself on the left and right subtrees, adds the node's value to the result list, and then recursively calls itself on the left and right subtrees again. If the node's value is less than the minimum value of the range, it only recursively calls itself on the right subtree. If the node's value is greater than the maximum value of the range, it only recursively calls itself on the left subtree. Finally, the `range_search` function calls `_range_search` on the root node of the BST and returns the result list.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值