【线段树合并】联通分量计数
给定一棵n个节点的树,从1到n标号。选择编号连续的若干个点,你需要选择一些边使得这些点通过选择的边联通,目标是使得选择的边数最少。
现需要计算对于n*(n+1)/2种选择的情况,最小选择边数的总和为多少。
Input
第一行一个数n(1<=n<=100000) 接下来n-1行,每行两个数x,y描述一条边(1<=x,y<=n)
Output
一个数,表示答案
Input示例
3 3 1 3 2
Output示例
5
注:若有n棵含单个元素的树,经过n-1次合并,合并成一棵树,总代价为O(nlogn)或O(nlogU)
定义线段树a的势能f(a)为 线段树a的总节点数
合并两棵值域相同的线段树的时间复杂度为O(两棵线段树重合节点的个数), 即势能的减少数量。
单叶节点线段树的节点数为logU,即f = logU
故合并线段树的时间复杂度为 O(nlogU)
1 #include <bits/stdc++.h> 2 3 #define ll long long 4 #define ull unsigned long long 5 #define st first 6 #define nd second 7 #define pii pair<int, int> 8 #define pil pair<int, ll> 9 #define pli pair<ll, int> 10 #define pll pair<ll, ll> 11 #define tiii tuple<int, int, int> 12 #define pw(x) ((1LL)<<(x)) 13 #define lson l, m, rt<<1 14 #define rson m+1, r, rt<<1|1 15 #define FIN freopen("a.in","r",stdin); 16 #define FOUT freopen("a.out","w",stdout); 17 using namespace std; 18 /***********/ 19 template <class T> 20 bool scan (T &ret) { 21 char c; 22 int sgn; 23 if (c = getchar(), c == EOF) return 0; //EOF 24 while (c != '-' && (c < '0' || c > '9') ) c = getchar(); 25 sgn = (c == '-') ? -1 : 1; 26 ret = (c == '-') ? 0 : (c - '0'); 27 while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0'); 28 ret *= sgn; 29 return 1; 30 } 31 template<typename N,typename PN>inline N flo(N a,PN b){return a>=0?a/b:-((-a-1)/b)-1;} 32 template<typename N,typename PN>inline N cei(N a,PN b){return a>0?(a-1)/b+1:-(-a/b);} 33 template<typename T> 34 inline int sgn(T a) {return a>0?1:(a<0?-1:0);} 35 template <class T1, class T2> 36 bool gmax(T1 &a, const T2 &b) { return a < b? a = b, 1:0;} 37 template <class T1, class T2> 38 bool gmin(T1 &a, const T2 &b) { return a > b? a = b, 1:0;} 39 template <class T> inline T lowbit(T x) {return x&(-x);} 40 41 template<class T1, class T2> 42 ostream& operator <<(ostream &out, pair<T1, T2> p) { 43 return out << "(" << p.st << ", " << p.nd << ")"; 44 } 45 template<class A, class B, class C> 46 ostream& operator <<(ostream &out, tuple<A, B, C> t) { 47 return out << "(" << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t) << ")"; 48 } 49 template<class T> 50 ostream& operator <<(ostream &out, vector<T> vec) { 51 out << "("; for(auto &x: vec) out << x << ", "; return out << ")"; 52 } 53 void testTle(int &a){ 54 while(1) a = a*(ll)a%1000000007; 55 } 56 const int inf = 0x3f3f3f3f; 57 const ll INF = 1e17; 58 const ll mod = 1000000007; 59 const double eps = 1e-6; 60 const int N = 3e6+5; 61 /***********/ 62 63 64 struct Node{ 65 int l, r;//子树指针 66 ll ans; 67 int lsize, rsize;//左侧连续长度,右侧连续长度 68 bool lnum, rnum;//最左值 69 }; 70 Node T[N]; 71 72 void debug(Node &A, int l, int r){ 73 puts("******"); 74 printf("l: %d, r: %d\n", l, r); 75 printf("ans: %lld\n", A.ans); 76 printf("lsize:%d, rsize:%d\n", A.lsize, A.rsize); 77 printf("lnum:%d rnum:%d\n", A.lnum*1, A.rnum*1); 78 puts("******"); 79 if(A.l) debug(T[A.l], l, (l+r)/2); 80 if(A.r) debug(T[A.r], (l+r)/2+1, r); 81 } 82 83 int cnt; 84 int n; 85 void pushup(int rt, int l, int r){//更新rt节点信息 86 int lp = T[rt].l, rp = T[rt].r; 87 int m = (l+r) >> 1; 88 //更新 89 T[rt].ans = (lp? T[lp].ans: 0) + (rp? T[rp].ans: 0) + (m-l+1LL)*(r-m);/////// 90 if( (lp? T[lp].rnum: 0) == (rp? T[rp].lnum: 0) ){ 91 T[rt].ans -= (lp? T[lp].rsize: m-l+1LL)*(rp? T[rp].lsize: r-m); 92 if(lp == 0||T[lp].lsize == m-l+1) T[rt].lsize = m-l+1+(rp? T[rp].lsize: r-m); 93 else T[rt].lsize = T[lp].lsize; 94 if(rp == 0||T[rp].rsize == r-m) T[rt].rsize = r-m+(lp? T[lp].rsize: m-l+1); 95 else T[rt].rsize = T[rp].rsize; 96 } 97 else T[rt].lsize = lp? T[lp].lsize: m-l+1, T[rt].rsize = rp? T[rp].rsize: r-m; 98 T[rt].lnum = lp? T[lp].lnum: 0, T[rt].rnum = rp? T[rp].rnum: 0; 99 } 100 101 int update(int x, int l, int r, int &rt){//插入一个节点 102 rt = ++cnt; 103 if(cnt == N){ 104 testTle(x); 105 } 106 T[rt].l = T[rt].r = 0; 107 T[rt].ans = (x-(ll)l)*(r-x)+r-l;//x 108 T[rt].lsize = x == l? 1: x-l, T[rt].rsize = x == r? 1: r-x; 109 T[rt].lnum = x == l; T[rt].rnum = x == r; 110 if(l == r) return cnt; 111 112 int m = (l+r) >> 1; 113 if(x <= m) update(x, l, m, T[rt].l); 114 else update(x, m+1, r, T[rt].r); 115 return cnt; 116 } 117 118 void merg(int &x, int y, int l, int r){ 119 if(!x || !y){ 120 if(x == 0) x = y; 121 return ; 122 } 123 if(l == r) {testTle(x) ; return ;} //T[x].v += T[y].v; 124 int m = (l+r) >> 1; 125 merg(T[x].l, T[y].l, l, m); 126 merg(T[x].r, T[y].r, m+1, r); 127 pushup(x, l, r); 128 } 129 130 vector<int> ve[100005]; 131 int root[100005]; 132 ll ans = 0; 133 void dfs(int f, int x){ 134 //add 135 update(x, 1, n, root[x]); 136 for(int y: ve[x]) if(y != f){ 137 dfs(x, y); 138 merg(root[x], root[y], 1, n); 139 } 140 if(f) ans += T[ root[x] ].ans; 141 } 142 143 int main() { 144 //FIN //FOUT 145 scanf("%d", &n); 146 for(int u, v, i = 1; i < n; i++){ 147 scanf("%d%d", &u, &v); 148 ve[u].push_back(v); 149 ve[v].push_back(u); 150 } 151 dfs(0, 1); 152 printf("%lld\n", ans); 153 return 0; 154 }
诸神对凡人心生艳羡,厌倦天堂。