30%:暴力

40%:枚举L,R从L~n枚举,R每增大一个,更新需要的边(bfs实现)60%:枚举每条边,

计算每条边的贡献另外20%的数据:枚举每条边,计算每条边的贡献100%:对于每一条边统计

有多少个区间跨过这条边即可统计这一问题的对偶问题,有多少个区间没跨过会更方便使用启发式合并+

并查集统计子树内的,使用启发式合并+set统计子树外的

代码:

#include<cstdio>
#include<cstdlib>
#include<set>
#include<vector>
#include<iostream>
#define LL long long
#define int long long
using namespace std;
const int MAXN = 1e5 + 10;
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, siz[MAXN], son[MAXN], dsu[MAXN], Son, vis[MAXN], ds[MAXN];
vector<int> v[MAXN];
set<int> s;
LL outt, inn, ans;
void dfs(int x, int _fa)
{
siz[x] = 1;
for(int i = 0; i < v[x].size(); i++) {
int to = v[x][i]; if(to == _fa) continue;
dfs(to, x);
siz[x] += siz[to];
if(siz[to] > siz[son[x]]) son[x] = to;
}
}
LL calc(LL x) {
return x * (x - 1) / 2;
}
void Clear() {
s.clear();
outt = calc(N);
inn = 0;
s.insert(0);
s.insert(N + 1);
}
int find(int x)
{
return dsu[x] == x ? dsu[x] : dsu[x] = find(dsu[x]);
}
void solve(int x)
{
s.insert(x);
set<int>::iterator s1, s2, it;
s1 = s2 = it = s.find(x);
s1--; s2++;
outt -= calc((*s2) - (*s1) - 1);
outt += calc((*s2) - (*it) - 1) + calc((*it) - (*s1) - 1);
vis[x] = 1;
if(vis[x - 1]) {
int fx = find(x - 1), fy = find(x);
inn += ds[fx] * ds[fy];
dsu[fx] = fy;
ds[fy] += ds[fx];
}
if(vis[x + 1]) {
int fx = find(x + 1), fy = find(x);
inn += ds[fx] * ds[fy];
dsu[fx] = fy;
ds[fy] += ds[fx];
}
}
void Add(int x, int fa)
{
solve(x);
for(int i = 0; i < v[x].size(); i++)
{
int to = v[x][i];
if(to == fa || to == Son) continue;
Add(to, x);
}
}
void Delet(int x, int fa) {
vis[x] = 0; ds[x] = 1; dsu[x] = x;
for(int i = 0; i < v[x].size(); i++) {
int to = v[x][i];
if(to == fa) continue;
Delet(to, x);
}
}
void dfs2(int x, int fa, int opt)
{
for(int i = 0; i < v[x].size(); i++)
{
int to = v[x][i];
if(to == fa || (to == son[x])) continue;
dfs2(to, x, 0);
}
if(son[x]) dfs2(son[x], x, 1); Son = son[x];
Add(x, fa);
ans += calc(N) - inn - outt;
if(opt == 0) Delet(x, fa), Clear(), Son = 0;
}
signed main()
{
#ifdef yilnr
#else
freopen("treecnt.in","r",stdin);
freopen("treecnt.out","w",stdout);
#endif
N = read();
for(int i = 1; i <= N - 1; i++)
{
int x = read(), y = read();
v[x].push_back(y);
v[y].push_back(x);
}
for(int i = 1; i <= N; i++) dsu[i] = i,ds[i] = 1;
dfs(1,0);
Clear();
dfs2(1,0,0);
printf("%lld\n",ans);
return 0;
}
/*
4
1 4
1 3
2 4
*/

【csp模拟赛6】树上统计-启发式合并,线段树合并的更多相关文章

  1. 【CSP模拟赛】God knows (李超线段树)

    题面 CODE 稍微分析一下,发现把(i,pi)(i,p_i)(i,pi​)看做二维数点,就是求极长上升子序列的权值最小值. 直接李超线段树 #include <bits/stdc++.h> ...

  2. 5.20 省选模拟赛 T1 图 启发式合并 线段树合并 染色计数问题

    LINK:图 在说这道题之前吐槽一下今天的日子 520 = 1+1+4+514. /cy 这道题今天做的非常失败 一点分都没拿到手 关键是今天的T3 把我整个人给搞崩了. 先考虑 如果得到了这么一张图 ...

  3. HDU - 4358 Boring counting (树上启发式合并/线段树合并)

    题目链接 题意:统计树上每个结点中恰好出现了k次的颜色数. dsu on tree/线段树合并裸题. 启发式合并1:(748ms) #include<bits/stdc++.h> usin ...

  4. [XJOI NOI2015模拟题13] C 白黑树 【线段树合并】

    题目链接:XJOI - NOI2015-13 - C 题目分析 使用神奇的线段树合并在 O(nlogn) 的时间复杂度内解决这道题目. 对树上的每个点都建立一棵线段树,key是时间(即第几次操作),动 ...

  5. 启发式合并 splay合并 线段树合并基础

    Gold is everywhen! - somebody 启发式合并 将小的集合一个个插入到大的集合. 每次新集合大小至少比小集合大一倍,因此每个元素最多合并\(\log n\)次,总复杂度为\(n ...

  6. 启发式合并&线段树合并/分裂&treap合并&splay合并

    启发式合并 有\(n\)个集合,每次让你合并两个集合,或询问一个集合中是否存在某个元素. ​ 我们可以用平衡树/set维护集合. ​ 对于合并两个\(A,B\),如果\(|A|<|B|\),那么 ...

  7. [BZOJ2733][HNOI2010]永无乡 解题报告 启发式合并,线段树合并

    好久没更新博客了,前段时间一直都在考试,都没时间些,现在终于有点闲了(cai guai)... 写了一道题,[HNOI2012]永无乡,其实是一道板子题,我发现我写了好多板子题...还是太菜了... ...

  8. bzoj2733: [HNOI2012]永无乡(splay+启发式合并/线段树合并)

    这题之前写过线段树合并,今天复习Splay的时候想起这题,打算写一次Splay+启发式合并. 好爽!!! 写了长长的代码(其实也不长),只凭着下午的一点记忆(没背板子...),调了好久好久,过了样例, ...

  9. [NOIP10.6模拟赛]2.equation题解--DFS序+线段树

    题目链接: 咕 闲扯: 终于在集训中敲出正解(虽然与正解不完全相同),开心QAQ 首先比较巧,这题是\(Ebola\)出的一场模拟赛的一道题的树上强化版,当时还口胡出了那题的题解 然而考场上只得了86 ...

  10. 【CSP模拟赛】避难向导(倍增lca&树的直径)

    耐力OIer,一天7篇博客 题目描述 “特大新闻,特大新闻!全国爆发了一种极其可怕的病毒,已经开始在各个城市 中传播开来!全国陷入了巨大的危机!大量居民陷入恐慌,想要逃到其它城市以 避难!经调查显示, ...

随机推荐

  1. 【动态规划】Concerts

    Concerts 题目描述 John enjoys listening to several bands, which we shall denote using A through Z. He wa ...

  2. scratch少儿编程第一季——04、想要做到有的放矢,瞄准方向很重要

    各位小伙伴大家好: 上期我们学习了动作模块的前面三个指令,今天我们继续学习下面的5个指令. 首先来看第一个(控制方向): 面向90方向默认就是屏幕的右边. 点击白色文本框上面的▼可以打开下拉菜单. 大 ...

  3. 【5号课堂】scratch制作电子生日贺卡

    贺卡在我国的使用由来已久,在古代,上层士大夫有用名帖互相问候的习俗 唐宋以后,贺卡的名称及功能有所进步,称为”门状“或“飞帖“,到了明清,又叫“红单“.“贺年帖“等等,听着名字就知功能越来越世俗化,文 ...

  4. SAS学习笔记9 利用SAS绘制地图

    绘制世界地图 proc gmap过程: map=指定绘图的map数据集 data=指定地图的对应数据集 id指定map数据集和对应数据集中都有的变量,一般为各区域的代码,作为两个数据集的连接变量 分色 ...

  5. Spring (1)框架

    Spring第一天笔记   1. 说在前面 怎样的架构的程序,我们认为是一个优秀的架构? 我们考虑的标准:可维护性好,可扩展性好,性能. 什么叫可扩展性好? 答:就是可以做到,不断的增加代码,但是可以 ...

  6. .netcore 和.netFrameWork

    netcore 是一个流程,可以调用,netcore 框架下,选择netFrameWork.可以使用netFrameWork的库,比如画图等.只是管道是netcore的.

  7. 解决tensorflow 调用bug Running model failed:Invalid argument: NodeDef mentions attr 'dilations' not in Op<name=Conv2D; signature=input:T, filter:T ->

    将tensorflow C++ 版本更新为何训练版本一致即可

  8. canvas验证码实现

    1 <!DOCTYPE html> <html> <!-- head --> <head> <meta charset="utf-8&q ...

  9. Vivado问题集锦

    1.添加包含子IP的模块到block design,报错如下所示: 错误的后面提供了解决方法:在tcl命令行中输入如下指令,添加子IP的xci文件即可. set_property generate_s ...

  10. openstack安装部署——计算服务(控制节点&计算节点)前言

    1.前言Openstack计算服务通过认证服务获取认证:通过镜像服务获取镜像:通过仪表盘提供的用户界面与用户交互.镜像的存取受工程和用户的限制,配额受工程的限制(例如不同工程允许虚拟机实例数量不同). ...