计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)

Distance on the tree

DSM(Data Structure Master) once learned about tree when he was preparing for NOIP(National Olympiad in Informatics in Provinces) in Senior High School. So when in Data Structure Class in College, he is always absent-minded about what the teacher says.

The experienced and knowledgeable teacher had known about him even before the first class. However, she didn't wish an informatics genius would destroy himself with idleness. After she knew that he was so interested in ACM(ACM International Collegiate Programming Contest), she finally made a plan to teach him to work hard in class, for knowledge is infinite.

This day, the teacher teaches about trees." A tree with nn nodes, can be defined as a graph with only one connected component and no cycle. So it has exactly n-1n1 edges..." DSM is nearly asleep until he is questioned by teacher. " I have known you are called Data Structure Master in Graph Theory, so here is a problem. "" A tree with nn nodes, which is numbered from 11to nn. Edge between each two adjacent vertexes uu and vv has a value w, you're asked to answer the number of edge whose value is no more than kk during the path between uu and vv."" If you can't solve the problem during the break, we will call you DaShaMao(Foolish Idiot) later on."

The problem seems quite easy for DSM. However, it can hardly be solved in a break. It's such a disgrace if DSM can't solve the problem. So during the break, he telephones you just for help. Can you save him for his dignity?

Input

In the first line there are two integers n,mn,m, represent the number of vertexes on the tree and queries(2 \le n \le 10^5,1 \le m \le 10^52n105,1m105)

The next n-1n1 lines, each line contains three integers u,v,wu,v,w, indicates there is an undirected edge between nodes uu and vv with value ww. (1 \le u,v \le n,1 \le w \le 10^91u,vn,1w109)

The next mm lines, each line contains three integers u,v,ku,v,k , be consistent with the problem given by the teacher above. (1 \le u,v \le n,0 \le k \le 10^9)(1u,vn,0k109)

Output

For each query, just print a single line contains the number of edges which meet the condition.

样例输入1

3 3
1 3 2
2 3 7
1 3 0
1 2 4
1 2 7

样例输出1

0
1
2

样例输入2

5 2
1 2 1000000000
1 3 1000000000
2 4 1000000000
3 5 1000000000
2 3 1000000000
4 5 1000000000

样例输出2

2
4

题意就是给你一棵树,查询求a到b路径上,边权小于等于k的边有几条。

树链剖分+可持久化线段树。

查询的时候,将树链对应到可持久化线段树上的时候要传到上一个历史版本,所以查询的时候要判断一下。然后就是更新的时候,dfs进行更新,因为tid对应的rt是通过dfs得到的,直接遍历通过dep找爸爸和儿子然后更新是不对的,tid对应的rt是不正确的。其他的代码里写了注释。

两种写法:

1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理

2.树上第k大(主席树)+二分+离散化+在线查询

第二个写法是因为有板子,但是是点权的,直接边权下放到点权,然后二分找k大,然后判断找个数。第二个写法跑的慢,但是能过题就是好代码。

第二个写法模板参考来源:SPOJ COT Count on a tree 树上第k大(主席树)

第一种写法代码:

  1 //做法一:
  2 //树链剖分(边权)+可持久化线段树
  3 #include<bits/stdc++.h>
  4 using namespace std;
  5 typedef long long ll;
  6 const int maxn=1e5+10;
  7 const int inf=0x3f3f3f3f;
  8 
  9 #define lson l,m
 10 #define rson m+1,r
 11 
 12 int sum[maxn<<5],b[maxn],value[maxn<<5],ls[maxn<<5],rs[maxn<<5];
 13 int top[maxn],tid[maxn],pos[maxn],fa[maxn],rt[maxn];
 14 int siz[maxn],dep[maxn],son[maxn];
 15 int head[maxn],cnt,num,n,m,d,sz;
 16 
 17 struct Edge{
 18     int to,next,val;
 19 }edge[maxn<<1];
 20 
 21 void init()//初始化
 22 {
 23     memset(head,-1,sizeof(head));
 24     memset(son,-1,sizeof(son));
 25     cnt=num=sz=0;
 26 }
 27 
 28 void add(int u,int v,int w)//存图
 29 {
 30     edge[cnt].to=v;
 31     edge[cnt].val=w;
 32     edge[cnt].next=head[u];
 33     head[u]=cnt++;
 34 }
 35 
 36 //树链剖分部分
 37 void dfs1(int u,int father)//第一遍dfs,找父节点,深度,重儿子
 38 {
 39     siz[u]=1;//当前节点的size
 40     fa[u]=father;//保存父节点
 41     dep[u]=dep[father]+1;//记录深度
 42     for(int i=head[u];~i;i=edge[i].next){//遍历
 43         int v=edge[i].to;
 44         int w=edge[i].val;
 45         if(v!=father){//如果连接的是当前节点的父节点,不进行处理,不是就进行处理
 46             value[v]=w;//节点下放,边权下放到节点
 47             dfs1(v,u);
 48             siz[u]+=siz[v];//子树size直接加进来
 49             if(son[u]==-1||siz[v]>siz[son[u]])//如果没有设置过重节点son或者子节点v的size大于之前记录的重节点,更新
 50                 son[u]=v;//保存重儿子
 51         }
 52     }
 53 }
 54 
 55 void dfs2(int u,int tp)//第二遍dfs,将各个重节点连接成重链,轻节点连接成轻链,并且将重链(区间)用数据结构去维护,并且为每个节点进行编号,就是dfs的顺序(tid数组),以及当前节点所在的链的起点(top),以及当前节点在树中的位置(pos)
 56 {
 57     top[u]=tp;//保存当前节点所在链的顶端节点,当前节点在的链的起点
 58     tid[u]=++num;//保存树中每个节点剖分之后的新编号,
 59     pos[tid[u]]=value[u];//当前节点的权值在树中的位置,设置新节点为当前节点对应的权值
 60     if(son[u]==-1) return ;//如果当前节点不在重链上,不进行处理
 61     dfs2(son[u],tp);//将这条链的所有节点都更新成tp,(就是当前节点所在链的起始节点的重儿子)
 62     for(int i=head[u];~i;i=edge[i].next){//遍历
 63         int v=edge[i].to;
 64         if(v!=son[u]&&v!=fa[u]) dfs2(v,v);//如果当前节点不是所在链的重节点也不是u的父节点,就把top设置成自己,进一步递归
 65     }
 66 }
 67 
 68 //void build(int &rt,int l,int r)//建一棵空树,也可以不建树,不建树跑的快
 69 //{
 70 //    rt=++sz;sum[rt]=0;
 71 //    if(l==r){
 72 //        return ;
 73 //    }
 74 //
 75 //    int m=(l+r)>>1;
 76 //    build(ls[rt],lson);
 77 //    build(rs[rt],rson);
 78 //}
 79 
 80 void update(int pre,int &rt,int l,int r,int p)//可持久化线段树更新操作
 81 {
 82     rt=++sz;sum[rt]=sum[pre]+1;
 83     ls[rt]=ls[pre];rs[rt]=rs[pre];
 84     if(l==r){
 85         return ;
 86     }
 87 
 88     int m=(l+r)>>1;
 89     if(p<=m) update(ls[pre],ls[rt],lson,p);
 90     else update(rs[pre],rs[rt],rson,p);
 91 }
 92 
 93 int query(int pre,int &rt,int L,int R,int l,int r)//查找就可以了
 94 {
 95     if(L>R) return 0;
 96     if(L<=l&&r<=R){
 97         return sum[rt]-sum[pre];
 98     }
 99 
100     int m=(l+r)>>1;
101     int ret=0;
102     if(L<=m) ret+=query(ls[pre],ls[rt],L,R,lson);
103     if(R> m) ret+=query(rs[pre],rs[rt],L,R,rson);
104     return ret;
105 }
106 
107 //当时就是查询写捞了。。。
108 int getnum(int u,int v,int l,int r)//最重要的操作,将剖分下来的链与数据结构联系起来,lca的操作通过top往上跳实现,top相同的话就是lca的节点
109 {
110     int ans=0;
111     while(top[u]!=top[v]){//如果两点的top节点不同,所在链的起始点相同就表示找到了lca
112         if(dep[top[u]]<dep[top[v]]) swap(u,v);//始终让top[u]的深度大于top[v]的,查询深度大的
113 //        ans+=query(rt[tid[top[u]]],rt[tid[u]],l,r,1,d);
114         if(top[u]==1) ans+=query(rt[tid[top[u]]],rt[tid[u]],l,r,1,d);//查询对应数据结构上的部分,找到对应的左右区间
115         else ans+=query(rt[tid[fa[top[u]]]],rt[tid[u]],l,r,1,d);
116         u=fa[top[u]];//将其修改为起始节点的父节点,走轻边,继续循环
117     }
118 
119     if(dep[v]<dep[u]) swap(u,v);//如果查询的是在一个完整的区间的一部分,直接查询就可以
120 //    ans+=query(rt[tid[son[u]]],rt[tid[v]],l,r,1,d);
121     ans+=query(rt[tid[u]],rt[tid[v]],l,r,1,d);//因为是可持久化线段树,虽然是查询tid[son[u]],tid[v],但是要左边再往上一个版本,就是son[u]的爸爸,就是u自己,所以是rt[tid[u]]],rt[tid[v]],
122     return ans;
123 }
124 
125 void dfs(int u,int father)//将剖下来的链对应到可持久化线段树上
126 {
127     update(rt[tid[father]],rt[tid[u]],1,d,value[u]);
128     for(int i=head[u];~i;i=edge[i].next){
129         int v=edge[i].to;
130         if(v!=father){
131             dfs(v,u);
132         }
133     }
134 }
135 
136 struct Eg{
137     int u,v,val;
138 }eg[maxn];
139 
140 struct Ask{
141     int l,r,k;
142 }ask[maxn];
143 
144 int main()
145 {
146     scanf("%d%d",&n,&m);
147     init();
148     for(int i=1;i<n;i++){
149         int u,v,w;
150         scanf("%d%d%d",&eg[i].u,&eg[i].v,&eg[i].val);
151         b[i]=eg[i].val;
152     }
153     for(int i=1;i<=m;i++){
154         scanf("%d%d%d",&ask[i].l,&ask[i].r,&ask[i].k);
155         b[i+n-1]=ask[i].k;
156     }
157     sort(b+1,b+n+m);
158     d=unique(b+1,b+n+m)-b;
159     for(int i=1;i<n;i++){
160         eg[i].val=lower_bound(b+1,b+1+d,eg[i].val)-b;
161         add(eg[i].u,eg[i].v,eg[i].val);
162         add(eg[i].v,eg[i].u,eg[i].val);
163     }
164     dfs1(1,0);
165     dfs2(1,1);
166 //    build(rt[0],1,d);
167     dfs(1,0);
168 //    for(int i=1;i<=n;i++)
169 //        cout<<rt[i]<<endl;
170 //    for(int i=1;i<n;i++){//不能遍历更新,如果不是可持久化线段树可以遍历更新,可持久化线段树更新和上一个历史版本有关,直接更新,tid对应的历史版本不一定对,只能dfs的时候进行更新,历史版本和在dfs上更新是一样的,因为历史版本就是通过dfs的时候求出来的。历史版本和tid才能对应上。
171 //        if(dep[eg[i].u]>dep[eg[i].v]) swap(eg[i].u,eg[i].v);
172 ////        cout<<rt[tid[eg[i].u]]<<" "<<rt[tid[eg[i].v]]<<" "<<eg[i].val<<endl;
173 //        update(rt[tid[eg[i].u]],rt[tid[eg[i].v]],1,d,eg[i].val);
174 //    }
175 //    for(int i=1;i<=n;i++){
176 //        cout<<rt[i]<<endl;
177 //    }
178     for(int i=1;i<=m;i++){
179         ask[i].k=lower_bound(b+1,b+1+d,ask[i].k)-b;
180         int ans=getnum(ask[i].l,ask[i].r,1,ask[i].k);
181         printf("%d\n",ans);
182     }
183 }
184 
185 /*
186 4 2
187 1 2 1
188 2 4 2
189 1 3 3
190 1 4 2
191 3 4 3
192 
193 2
194 3
195 */
View Code

第二种写法代码:

  1 //做法二:
  2 //树链剖分+主席树-变权下放,二分找K大找数
  3 #include<bits/stdc++.h>
  4 using namespace std;
  5 typedef long long ll;
  6 const int maxn=2e5+10;
  7 const int inf=0x3f3f3f3f;
  8 
  9 #define lson l,m
 10 #define rson m+1,r
 11 
 12 struct edge{
 13     int to,next,val;
 14 }g[maxn<<1];
 15 
 16 int n,m,sz;
 17 int cnt,head[maxn];
 18 int len,root[maxn],ls[maxn*20],rs[maxn*20],val[maxn*20];
 19 int num,dep[maxn<<1],ver[maxn<<1],fir[maxn],dp[20][maxn<<1],fa[maxn];
 20 bool vis[maxn];
 21 int a[maxn],b[maxn];
 22 
 23 void init()
 24 {
 25     memset(vis,false,sizeof vis);
 26     memset(head,-1,sizeof head);
 27     cnt=num=sz=0;
 28 }
 29 
 30 void add(int u,int v,int w)
 31 {
 32     g[cnt].to=v;
 33     g[cnt].val=w;
 34     g[cnt].next=head[u];
 35     head[u]=cnt++;
 36 }
 37 
 38 //void build(int &rt,int l,int r)
 39 //{
 40 //    rt=++sz;val[rt]=0;
 41 //    if(l==r){
 42 //        return ;
 43 //    }
 44 //
 45 //    int m=(l+r)>>1;
 46 //    build(ls[rt],lson);
 47 //    build(rs[rt],rson);
 48 //}
 49 
 50 void update(int pre,int &rt,int l,int r,int p)
 51 {
 52     rt=++sz;val[rt]=val[pre]+1;
 53     ls[rt]=ls[pre];rs[rt]=rs[pre];
 54     if(l==r){
 55         return ;
 56     }
 57 
 58     int m=(l+r)>>1;
 59     if(p<=m) update(ls[pre],ls[rt],lson,p);
 60     else     update(rs[pre],rs[rt],rson,p);
 61 }
 62 
 63 void dfs(int u,int father,int d)
 64 {
 65     vis[u]=true;
 66     fa[u]=father;
 67     ver[++num]=u;
 68     dep[num]=d;
 69     fir[u]=num;//dfs序的编号
 70     update(root[father],root[u],1,len,a[u]);//在父节点的基础上新建一棵树
 71     for(int i=head[u];i!=-1;i=g[i].next){
 72         int v=g[i].to;
 73         if(!vis[v]){
 74             dfs(v,u,d+1);
 75             ver[++num]=u;
 76             dep[num]=d;
 77         }
 78     }
 79 }
 80 
 81 void ST(int n)
 82 {
 83     for(int i=1;i<=n;i++){
 84         dp[0][i]=i;
 85     }
 86     for(int i=1;(1<<i)<=n;i++){
 87         for(int j=1;j<=n-(1<<i)+1;j++){
 88             int a=dp[i-1][j],b=dp[i-1][j+(1<<(i-1))];
 89             dp[i][j]=dep[a]<dep[b]? a:b;
 90         }
 91     }
 92 }
 93 
 94 int RMQ(int l,int r)
 95 {
 96     int k=log(r-l+1)/log(2);
 97     int a=dp[k][l],b=dp[k][r-(1<<k)+1];
 98     return dep[a]<dep[b]? a:b;
 99 }
100 
101 int LCA(int u,int v)
102 {
103     u=fir[u];v=fir[v];
104     if(u>v) swap(u,v);
105     int res=RMQ(u,v);
106     return ver[res];
107 }
108 
109 int query(int ss, int tt, int lca, int lca_fa, int l, int r, int k)
110 {
111     if(l == r){
112         return l;
113     }
114 
115     int m=(l+r)>>1;
116     int tmp=val[ls[ss]]+val[ls[tt]]-val[ls[lca]]-val[ls[lca_fa]];
117     if(k<=tmp) return query(ls[ss], ls[tt], ls[lca], ls[lca_fa],lson, k);
118     else       return query(rs[ss], rs[tt], rs[lca], rs[lca_fa], rson, k - tmp);
119 }
120 
121 void dfss(int u,int father)
122 {
123     for(int i=head[u];i!=-1;i=g[i].next){
124         int v=g[i].to;
125         int w=g[i].val;
126         if(v!=father){
127             a[v]=w;
128             dfss(v,u);
129         }
130     }
131 }
132 
133 int main()
134 {
135     scanf("%d%d",&n,&m);
136     init();
137     for(int i=1;i<n;i++){
138         int u,v,w;
139         scanf("%d%d%d",&u,&v,&w);
140         add(u,v,w);
141         add(v,u,w);
142     }
143     dfss(1,0);
144     a[1]=inf;
145     for(int i=1;i<=n;i++){
146         b[i]=a[i];
147     }
148     sort(b+1,b+n+1);
149     len=unique(b+1,b+n+1)-b-1;
150     for(int i=1;i<=n;i++){
151         a[i]=lower_bound(b+1,b+1+len,a[i])-b;
152     }
153     for(int i=len+1;i<=1e5+100;i++){
154         b[i]=inf;
155     }
156 //    build(root[0],1,len);
157     dfs(1,0,1);
158     ST(2*n-1);
159     for(int i=1;i<=m;i++){
160         int u,v,k;
161         scanf("%d%d%d",&u,&v,&k);
162         int lca=LCA(u,v);
163         int l=1,r=1e5+100,ans=0;
164         while(l<=r){
165             int mid=(l+r)/2;
166             if(k>=b[query(root[u],root[v],root[lca],root[fa[lca]],1,len,mid)])
167                 l=mid+1,ans=mid;
168             else
169                 r=mid-1;
170         }
171         if(b[a[lca]]<=k) ans--;
172         printf("%d\n",ans);
173     }
174     return 0;
175 }
176 
177 
178 /*
179 7 1
180 1 2 10
181 1 3 8
182 3 4 9
183 3 5 20
184 4 6 100
185 4 7 80
186 6 7 30
187 */
View Code

好菜啊,自闭,第二种写法队友过的,比赛时改不来板子,队友直接按点权的写的,然后二分判断的,Orz。

第一种写法赛后补题过的,查询函数对应查询范围写捞了,自闭。。。

菜到变形,难受。

猜你喜欢

转载自www.cnblogs.com/ZERO-/p/10746821.html