[洛谷U40581]树上统计treecnt
[洛谷U40581]树上统计treecnt
题目大意:
给定一棵\(n(n\le10^5)\)个点的树。
定义\(Tree[l,r]\)表示为了使得\(l\sim r\)号点两两连通,最少需要选择的边的数量。
求\(\sum_{l=1}^n\sum_{r=l}^nTree[l,r]\)。
思路:
对于每个边考虑贡献,若我们将出现在子树内的点记作\(1\),出现在子树外的点记作\(0\),那么答案就是\(\frac{n(n-1)}2-\)全\(0\)、全\(1\)串的个数。线段树合并,维护前缀/后缀最长全\(0\)/全\(1\)串即可。
时间复杂度\(\mathcal O(n\log n)\)。
源代码:
#include<cstdio>
#include<cctype>
#include<vector>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
typedef long long int64;
const int N=1e5+1,logN=18;
int n;
int64 ans;
std::vector<int> e[N];
inline void add_edge(const int &u,const int &v) {
e[u].push_back(v);
e[v].push_back(u);
}
inline int64 calc(const int &n) {
return 1ll*n*(n-1)/2;
}
struct Node {
int pre[2],suf[2],len;
int64 sum;
Node() {}
Node(const int &l,const bool &v) {
pre[!v]=suf[!v]=0;
pre[v]=suf[v]=len=l;
sum=calc(l);
}
friend Node operator + (const Node &l,const Node &r) {
Node ret;
ret.pre[0]=l.pre[0]+r.pre[0]*(l.pre[0]==l.len);
ret.pre[1]=l.pre[1]+r.pre[1]*(l.pre[1]==l.len);
ret.suf[0]=r.suf[0]+l.suf[0]*(r.suf[0]==r.len);
ret.suf[1]=r.suf[1]+l.suf[1]*(r.suf[1]==r.len);
ret.len=l.len+r.len;
ret.sum=l.sum+r.sum+1ll*l.suf[0]*r.pre[0]+1ll*l.suf[1]*r.pre[1];
return ret;
}
};
class SegmentTree {
#define mid ((b+e)>>1)
private:
Node node[N*logN];
int left[N*logN],right[N*logN];
int sz,new_node() {
return ++sz;
}
int len(const int &b,const int &e) {
return e-b+1;
}
void push_up(const int &p,const int &b,const int &e) {
if(!left[p]) node[p]=Node(len(b,mid),0)+node[right[p]];
if(!right[p]) node[p]=node[left[p]]+Node(len(mid+1,e),0);
if(left[p]&&right[p]) {
node[p]=node[left[p]]+node[right[p]];
}
}
public:
int root[N];
void insert(int &p,const int &b,const int &e,const int &x) {
if(!p) p=new_node();
if(b==e) {
node[p]=Node(1,1);
return;
}
if(x<=mid) insert(left[p],b,mid,x);
if(x>mid) insert(right[p],mid+1,e,x);
push_up(p,b,e);
}
void merge(int &p,const int &q,const int &b,const int &e) {
if(!p||!q) {
p=p|q;
return;
}
if(b==e) return;
merge(left[p],left[q],b,mid);
merge(right[p],right[q],mid+1,e);
push_up(p,b,e);
}
int64 query(const int &p) const {
return node[p].sum;
}
#undef mid
};
SegmentTree t;
void dfs(const int &x,const int &par) {
t.insert(t.root[x],1,n,x);
for(auto &y:e[x]) {
if(y==par) continue;
dfs(y,x);
t.merge(t.root[x],t.root[y],1,n);
}
if(x!=1) ans-=t.query(t.root[x]);
}
int main() {
n=getint();
ans=calc(n)*(n-1);
for(register int i=1;i<n;i++) {
add_edge(getint(),getint());
}
dfs(1,0);
printf("%lld\n",ans);
return 0;
}
[洛谷U40581]树上统计treecnt的更多相关文章
- 洛谷 P2664 树上游戏 解题报告
P2664 树上游戏 题目描述 \(\text{lrb}\)有一棵树,树的每个节点有个颜色.给一个长度为\(n\)的颜色序列,定义\(s(i,j)\) 为 \(i\) 到 \(j\) 的颜色数量.以及 ...
- 洛谷 P3177 树上染色 解题报告
P3177 [HAOI2015]树上染色 题目描述 有一棵点数为\(N\)的树,树边有边权.给你一个在\(0\) ~ \(N\)之内的正整数\(K\),你要在这棵树中选择\(K\)个点,将其染成黑色, ...
- 洛谷P2664 树上游戏(点分治)
题意 题目链接 Sol 神仙题..Orz yyb 考虑点分治,那么每次我们只需要统计以当前点为\(LCA\)的点对之间的贡献以及\(LCA\)到所有点的贡献. 一个很神仙的思路是,对于任意两个点对的路 ...
- 洛谷P4242 树上的毒瘤
解:首先有个套路是一条边的权值是[两端点颜色不同].这个用树剖直接维护,支持修改. 每次询问建虚树,查询虚树上每条边的权值.然后树形DP,用开店的方法,每个点链加链查. #include <bi ...
- 树上统计treecnt(dsu on tree 并查集 正难则反)
题目链接 dalao们怎么都写的线段树合并啊.. dsu跑的好慢. \(Description\) 给定一棵\(n(n\leq 10^5)\)个点的树. 定义\(Tree[L,R]\)表示为了使得\( ...
- 洛谷P2664 树上游戏(点分治)
传送门 题解 因为一个sb错误调了一个晚上……鬼晓得我为什么$solve(rt)$会写成$solve(v)$啊!!!一个$O(logn)$被我硬生生写成$O(n)$了竟然还能过$5$个点……话说还一直 ...
- 【刷题】洛谷 P2664 树上游戏
题目描述 lrb有一棵树,树的每个节点有个颜色.给一个长度为n的颜色序列,定义s(i,j) 为i 到j 的颜色数量.以及 \[sum_i=\sum_{j=1}^ns(i,j)\] 现在他想让你求出所有 ...
- 洛谷——P1608 路径统计
P1608 路径统计 题目描述 “RP餐厅”的员工素质就是不一般,在齐刷刷的算出同一个电话号码之后,就准备让HZH,TZY去送快餐了,他们将自己居住的城市画了一张地图,已知在他们的地图上,有N个地方, ...
- 洛谷 P1608 路径统计
P1608 路径统计 题目描述 “RP餐厅”的员工素质就是不一般,在齐刷刷的算出同一个电话号码之后,就准备让HZH,TZY去送快餐了,他们将自己居住的城市画了一张地图,已知在他们的地图上,有N个地方, ...
随机推荐
- XAF 与 CIIP
XAF 与 CIIP:网站:http://www.uims.top, XAF技术博客:http://www.cnblogs.com/foreachlife/ tylike 升级到 DevExpres ...
- 通过全备+主从同步恢复被drop的库或表
MySQL 中drop 等高危误操作后恢复方法 实验目的: 本次实验以恢复drop操作为例,使用不同方法进行误操作的数据恢复. 方法: 利用master同步(本文)] 伪master+Binlog+同 ...
- python中对列表的所有操作方法
列表: names = ['a','b','c','d'] 1.追加:names.append() >>> names.append('e') >>> names ...
- 一步步实现windows版ijkplayer系列文章之七——终结篇(附源码)
一步步实现windows版ijkplayer系列文章之一--Windows10平台编译ffmpeg 4.0.2,生成ffplay 一步步实现windows版ijkplayer系列文章之二--Ijkpl ...
- 002_mtr_a network diagnostic tool
一. mtr combines the functionality of the traceroute and ping programs in a single network diagnostic ...
- mysql5.5.48的my.cnf配置
MYSQL配置 [client] port = 3306 socket = /tmp/mysql.sock [mysqld] port = 3306 default-storage-engine=My ...
- python语法小应用---列表和元组
声明:本文章为参考总结CSDN上知识点所获,只是用来总结自己学习而用,如有侵权,会删除! 列表(list): 列表就像一个线性容器,但是比C++的 lis t扩展多得多 列表里的元素可以是相同类型,也 ...
- python 全栈开发,Day14(列表推导式,生成器表达式,内置函数)
一.列表生成式 生成1-100的列表 li = [] for i in range(1,101): li.append(i) print(li) 执行输出: [1,2,3...] 生成python1期 ...
- python 全栈开发,Day12(函数的有用信息,带参数的装饰器,多个装饰器装饰一个函数)
函数的执行时,*打散.函数的定义时,*聚合. from functools import wraps def wrapper(f): # f = func1 @wraps(f) def inner(* ...
- JQuery中的事件(三)
一:页面载入 ready(fn)当DOM载入就绪可以查询及操纵时绑定一个要执行的函数.这是事件模块中最重要的一个函数,因为它可以极大地提高web应用程序的响应速度.简单地说,这个方法纯粹是对向wind ...