zoukankan      html  css  js  c++  java
  • hdu_3804_Query on a tree(树链剖分)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3804

    题意:给你一棵树,然后给出树上边的价值,然后给出x,y,问从1到x的边上不超过y的最大值为多少

    题解:这题如果直接写裸线段树来在线更新会T飞,正解是把边的值存下来,把询问也存下来,然后都按照价值从小到大排序,然后从最小的开始找,找之前把边的值插进线段树,这样就成了查询区间最大值了,很巧妙的方法。还有就是这题的内存卡的很紧,用前向星爆内存了,我没想通,用vector就过了。很尴尬

     1 #include<cstdio>
     2 #include<vector>
     3 #include<algorithm>
     4 #define F(i,a,b) for(int i=a;i<=b;++i)
     5 #define root 1,n,1
     6 #define ls l,m,rt<<1
     7 #define rs m+1,r,rt<<1|1
     8 using namespace std;
     9 const int N=1e5+5;
    10 vector<int>g[N];
    11 struct vs{
    12     int val,u,v;
    13     bool operator<(const vs &b)const{return val<b.val;}
    14 }va[N];
    15 struct ser{
    16     int x,k,id;
    17     bool operator<(const ser &b)const{return k<b.k;}
    18 }ask[N];
    19 int t,n,m,x,y,c,tot,ans[N],sum[N*4],sz[N],fa[N],dep[N],hs[N],top[N],tid[N],idx;
    20 //线段树部分
    21 void update(int x,int k,int l,int r,int rt){
    22     if(l==r){sum[rt]=k;return;}
    23     int m=(l+r)>>1;
    24     if(x<=m)update(x,k,ls);
    25     else update(x,k,rs);
    26     sum[rt]=max(sum[rt<<1],sum[rt<<1|1]);
    27 }
    28 
    29 int query(int L,int R,int l,int r,int rt){
    30     if(L<=l&&r<=R)return sum[rt];
    31     int m=(l+r)>>1,maxs=0;
    32     if(L<=m)maxs=max(query(L,R,ls),maxs);
    33     if(m<R)maxs=max(query(L,R,rs),maxs);
    34     return maxs;
    35 }
    36 //树链部分
    37 void dfs1(int u,int pre){
    38     sz[u]=1,fa[u]=pre,dep[u]=dep[pre]+1,hs[u]=0;
    39     for(int i=0;i<g[u].size();i++){
    40         int vv=g[u][i];
    41         if(vv!=pre){
    42             dfs1(vv,u);
    43             if(sz[vv]>sz[hs[u]])hs[u]=vv;
    44             sz[u]+=sz[vv];
    45         }
    46     }
    47 }
    48 
    49 void dfs2(int u,int tp){
    50     tid[u]=++idx,top[u]=tp;
    51     if(hs[u])dfs2(hs[u],tp);
    52     for(int i=0;i<g[u].size();i++){
    53         int vv=g[u][i];
    54         if(vv!=fa[u]&&vv!=hs[u])dfs2(vv,vv);
    55     }
    56 }
    57 
    58 int findmax(int x){
    59     int fx=top[x],an=0;
    60     while(fx!=1)an=max(query(tid[fx],tid[x],root),an),x=fa[fx],fx=top[x];
    61     if(x==1)return an;
    62     return max(query(2,tid[x],root),an);
    63 }
    64 
    65 int main(){
    66     scanf("%d",&t);
    67     while(t--){
    68         scanf("%d",&n);
    69         F(i,1,n)g[i].clear();
    70         dep[0]=0,sz[0]=0;
    71         F(i,0,4*N-1)sum[i]=0;
    72         F(i,1,n-1)scanf("%d%d%d",&va[i].u,&va[i].v,&va[i].val),g[va[i].u].push_back(va[i].v),g[va[i].v].push_back(va[i].u);
    73         dfs1(1,0),idx=0,dfs2(1,1);
    74         scanf("%d",&m),tot=1;
    75         F(i,1,m)scanf("%d%d",&ask[i].x,&ask[i].k),ask[i].id=i;
    76         sort(va+1,va+n),sort(ask+1,ask+1+m);
    77         F(i,1,m){
    78             while(tot<n&&va[tot].val<=ask[i].k){
    79                 if(dep[va[tot].u]>dep[va[tot].v])c=va[tot].u;else c=va[tot].v;
    80                 update(tid[c],va[tot].val,root),tot++;
    81             }
    82             ans[ask[i].id]=findmax(ask[i].x);
    83         }
    84         F(i,1,m)printf("%d
    ",ans[i]==0?-1:ans[i]);
    85     }
    86     return 0;
    87 }
    View Code


  • 相关阅读:
    初来乍到
    windows小技巧:远程桌面连接rdp文件
    CSS小试牛刀
    Windows phone 7之样式与模板
    Pulldowntorefresh a WP7 ListBox or ScrollViewer(向上向下的手势)
    Windows Phone 7 文件下载进度和速度显示
    WPF Binding
    wp7学习笔记(转)
    WP7 网络请求之WebClient
    Windows Phone Tilt effect on HubTile and other Controls
  • 原文地址:https://www.cnblogs.com/bin-gege/p/5696115.html
Copyright © 2011-2022 走看看