[lct][最小生成树][主席树] Bzoj P4046 Pork barre

Description

Winning the election was simpler than you expected: it was enough to promise to finally build 
a good quality, country-wide road infrastructure, of course without crippling the budget_ Your 
happiness did not last long, however: it seems, that the citizens have found a way to actually 
hold you accountable for your promise! 
There are n major cities in your country. The Ministry of Transport has prepared a detailed 
map, outlining m possible highway connections, together with their costs. The Quality Assurance 
Committee will not let you build a highway cheaper than l, and the National Spendings 
Regulatory Committee will not let you build a highway more expensive than h. To claim a 
"country-wide" network, you have to connect (possibly indirectly) as many pairs of cities, as it is 
possible within these two constraints. You have to find the cheapest way to do it, and you have 
to find it quickly! Of all networks that meet the constraints and connect the most pairs of cities, 
compute the cost of the cheapest one. 
To make things worse, both committees are heavily influenced by your angry competitors: 
each time you publish your hard-prepared plan, they immediatelv change their rulings l and矗, 
and vou are forced to start from scratch.

 

题解

  • 按边权从大到小加边,用lct维护最小生成树。

  • 对于当前要加的边i,最小生成树上边权在[1,r]范围内的和就是询问[e[i].w,r]的答案。

  • 因为强制在线,所以用主席树存下所有历史版本即可

代码

  1 #include <cstdio>
  2 #include <iostream>
  3 #include <algorithm>
  4 using namespace std;
  5 const int N=200010,M=3600010;
  6 struct node{int x,y,w;}e[N];
  7 bool cmp(node a,node b){ return a.w>b.w; }
  8 int T,n,m,Q,x,y,ans,op,root[N],lson[M],rson[M],v[M],tot,f[N],son[N][2],val[N],sum[N],from[N],tmp[N],fa[N];
  9 bool rev[N];
 10 void Rev(int x) { if (!x) return; swap(son[x][0],son[x][1]),rev[x]^=1; }
 11 bool nroot(int x) { return !f[x]||(son[f[x]][0]!=x&&son[f[x]][1]!=x); }
 12 void pushback(int x){ if (rev[x]) Rev(son[x][0]),Rev(son[x][1]),rev[x]=0; }
 13 void pushup(int x)
 14 {
 15     sum[x]=val[x],from[x]=x;
 16     if (son[x][0]) if (sum[son[x][0]]>sum[x]) sum[x]=sum[son[x][0]],from[x]=from[son[x][0]];
 17     if (son[x][1]) if (sum[son[x][1]]>sum[x]) sum[x]=sum[son[x][1]],from[x]=from[son[x][1]];
 18 }
 19 int insert(int d,int l,int r,int L,int R)
 20 {
 21     int x=++tot;
 22     v[x]=v[d]+R,lson[x]=lson[d],rson[x]=rson[d];
 23     if (l==r) return x;
 24     int mid=l+r>>1;
 25     if (L<=mid) lson[x]=insert(lson[d],l,mid,L,R); else rson[x]=insert(rson[d],mid+1,r,L,R);
 26     return x;
 27 }
 28 int query(int d,int l,int r,int L,int R)
 29 {
 30     if (!d||L>R) return 0;
 31     if (L<=l&&r<=R) return v[d];
 32     int mid=l+r>>1,res=0;
 33     if (L<=mid) res=query(lson[d],l,mid,L,R);
 34     if (R>mid) res+=query(rson[d],mid+1,r,L,R);
 35     return res;
 36 }
 37 void rotate(int x)
 38 {
 39     int y=f[x],w=son[y][1]==x; son[y][w]=son[x][w^1];
 40     if (son[x][w^1]) f[son[x][w^1]]=y;
 41     if (f[y])
 42     {
 43         int z=f[y];
 44         if (son[z][0]==y) son[z][0]=x; else if (son[z][1]==y) son[z][1]=x;
 45     }
 46     f[x]=f[y],f[y]=x,son[x][w^1]=y,pushup(y);
 47 }
 48 void splay(int x)
 49 {
 50     int s=1,i=x,y;tmp[1]=i;
 51     while (!nroot(i)) tmp[++s]=i=f[i];
 52     while (s) pushback(tmp[s--]);
 53     while (!nroot(x)) 
 54     {
 55         y=f[x];
 56         if (!nroot(y)) if ((son[f[y]][0]==y)^(son[f[y]][0]==x)) rotate(x); else rotate(y);
 57         rotate(x);
 58     }
 59     pushup(x);
 60 }
 61 void access(int x) { for (int y=0;x;y=x,x=f[x]) splay(x),son[x][1]=y,pushup(x); }
 62 void makeroot(int x) { access(x),splay(x),Rev(x); }
 63 int split(int x,int y) { makeroot(x),access(y),splay(y); return from[y]; }
 64 void cut(int x,int y) { makeroot(x),access(y),splay(y),f[son[y][0]]=0,son[y][0]=0,pushup(y); }
 65 int find(int x) { return fa[x]==x?x:fa[x]=find(fa[x]); }
 66 void link(int x,int y) { makeroot(x),f[x]=y,access(x); }
 67 int ef(int x)
 68 {
 69     int l=1,r=m,mid,t=0;
 70     while (l<=r) if (e[mid=(l+r)>>1].w<=x) r=(t=mid)-1; else l=mid+1;
 71     return t;
 72 }
 73 int getid(int x)
 74 {
 75     int l=1,r=m,mid,t=0;
 76     while (l<=r) if (e[mid=(l+r)>>1].w>=x) l=(t=mid)+1; else r=mid-1;
 77     return t;
 78 }
 79 int main()
 80 {
 81     scanf("%d",&T);
 82     while (T--)
 83     {
 84         scanf("%d%d",&n,&m);
 85         for (int i=1;i<=n;i++) fa[i]=i;
 86         for (int i=1;i<=m;i++) scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].w);
 87         sort(e+1,e+m+1,cmp);
 88         for (int i=1;i<=m;i++) val[n+i]=sum[n+i]=e[i].w,from[n+i]=n+i;
 89         for (int i=1;i<=m;i++)
 90         {
 91             root[i]=root[i-1];
 92             if (find(e[i].x)==find(e[i].y)) 
 93             { 
 94                 int p=split(e[i].x,e[i].y); 
 95                 cut(p,e[p-n].x),cut(p,e[p-n].y),root[i]=insert(root[i],1,m,p-n,-e[p-n].w); 
 96             }
 97             else fa[fa[e[i].x]]=fa[e[i].y];
 98             link(e[i].x,n+i),link(e[i].y,n+i),root[i]=insert(root[i],1,m,i,e[i].w);
 99         }
100         scanf("%d",&Q);
101         for (int x,y;Q;Q--) scanf("%d%d",&x,&y),x=x-ans,y=y-ans,printf("%d\n",ans=query(root[getid(x)],1,m,ef(y),m));
102         for (int i=0;i<=n+m;i++) f[i]=son[i][0]=son[i][1]=val[i]=sum[i]=from[i]=rev[i]=0;
103         for (int i=1;i<=n;i++) fa[i]=0;
104         for (int i=1;i<=m;i++) root[i]=0;
105         tot=ans=0;
106     }
107 }

 

nlogn)

posted @ 2019-08-15 19:43  BEYang_Z  阅读(164)  评论(0编辑  收藏  举报