[other] Div
https://www.luogu.org/problemnew/show/U16765
解法一
随机输出一组合法解。
复杂度 O(1)
预计得分 10~???
解法二
看完题目基本能想到大力贪心,通过树上差分统计经过每一条边的查询次数,贪心取访问次数最多的到儿子的边作为重链。
复杂度 O(nlogn)
预计得分 60~70
解法三
为什么解法二不能拿到满分呢?
注意到以下数据:
5 3 1 2 2 4 2 5 2 3 1 5 3 4 4 3 标准答案为
2 5 0 0 0 次数为5。
而解法二的答案为
2 3(或4)
0 0 0 次数为6
它是一个十字架型,2为中心,3,4,5均为2的儿子,注意到3~4的查询如果使用重链对答案是没有贡献的,或者说如果一个查询中,两点的LCA如果为某个点的父亲,那么它到该儿子之间的边是不是重链对答案没有影响,在差分的时候这样实现:
if(g[u][0]!=r)
{
tree[u].cnt++;
tree[r].cnt--;
}
if(g[v][0]!=r)
{
tree[v].cnt++;
tree[r].cnt--;
}
其中g[u][0]为倍增数组,即u的父亲节点,r是u和v的LCA,tree[].cnt是差分的记录变量。 复杂度 O(nlogn)
预计得分 100
解法四
模拟退火等随机化算法乱搞,我没试过,但是似乎可能可以得到很好的结果。
#include <bits/stdc++.h> using namespace std;
const int N = 1e5 + ; #define gc getchar()
#define lson jd << 1
#define rson jd << 1 | 1
#define important T[jd].w = T[lson].w + T[rson].w struct Node_1{
int siz, son, fa, deep, tree, toop;
}P[N];
struct Node_2{
int v, nxt;
}G[N << ];
struct Node_3{
int l, r, f, w;
}T[N << ];
int n, Ti, now = , head[N], tim, bef[N], data[N]; inline int read(){
int x = ; char c = gc;
while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc;
return x;
} inline void add(int u, int v){
G[now].v = v;
G[now].nxt = head[u];
head[u] = now ++;
} void dfs_find_son(int u, int fa, int dep){
P[u].fa = fa;
P[u].deep = dep;
P[u].siz = ;
for(int i = head[u]; ~ i; i = G[i].nxt){
int v = G[i].v;
if(v != fa) {
dfs_find_son(v, u, dep + );
P[u].siz += P[v].siz;
if(P[v].siz > P[P[u].son].siz) P[u].son = v;
}
}
} void dfs_to_un(int u, int tp){
P[u].toop = tp;
P[u].tree = ++ tim;
bef[tim] = u;
if(!P[u].son) return ;
dfs_to_un(P[u].son, tp);
for(int i = head[u]; ~ i; i = G[i].nxt){
int v = G[i].v;
if(v != P[u].son && v != P[u].fa) dfs_to_un(v, v);
}
} void down(int jd){
int F = T[jd].f;
T[lson].w += (T[lson].r - T[lson].l + ) * F;
T[rson].w += (T[rson].r - T[rson].l + ) * F;
T[lson].f += F;
T[rson].f += F;
T[jd].f = ;
} void Sec_G(int l, int r, int jd, int x, int y){
if(x <= l && r <= y){
int yj = (r - l) + ;
T[jd].w += yj;
T[jd].f ++;
return ;
}
if(T[jd].f) down(jd);
int mid = (l + r) >> ;
if(x <= mid) Sec_G(l, mid, lson, x, y);
if(y > mid) Sec_G(mid + , r, rson, x, y);
important;
} inline void Sec_G_imp(int x, int y){
int tp1 = P[x].toop, tp2 = P[y].toop;
while(tp1 != tp2){
if(P[tp1].deep < P[tp2].deep) swap(x, y), swap(tp1, tp2);
Sec_G(, n, , P[tp1].tree, P[x].tree);
x = P[tp1].fa;
tp1 = P[x].toop;
}
if(P[x].deep < P[y].deep) Sec_G(, n, , P[x].tree, P[y].tree);
else Sec_G(, n, , P[y].tree, P[x].tree);
return ;
} void Ask_ans(int l, int r, int jd){
if(l == r) {
data[bef[l]] = T[jd].w;
return ;
}
if(T[jd].f) down(jd);
int mid = (l + r) >> ;
Ask_ans(l, mid, lson);
Ask_ans(mid + , r, rson);
} void debug(){
for(int i = ; i <= n; i ++) cout << data[i] << endl;
exit();
} void build_tree(int l, int r, int jd){
T[jd].l = l; T[jd].r = r;
if(l == r) return ;
int mid = (l + r) >> ;
build_tree(l, mid, lson);
build_tree(mid + , r, rson);
} int main()
{
n = read(); Ti = read();
for(int i = ; i <= n; i ++) head[i] = -;
for(int i = ; i < n; i ++){
int u = read(), v = read();
add(u, v); add(v, u);
}
dfs_find_son(, , );
dfs_to_un(, );
build_tree(, n, );
while(Ti --){
int x = read(), y = read();
Sec_G_imp(x, y);
}
Ask_ans(, n, );
//debug();
for(int i = ; i <= n; i ++){
int answer = -, whoo = ;
for(int j = head[i]; ~ j; j = G[j].nxt){
int v = G[j].v;
if(v == P[v].fa || P[v].tree < P[i].tree) continue ;
if(data[v] > answer){
answer = data[v];
whoo = v;
}
}
printf("%d\n", whoo);
}
return ;
}
/*
14 7
1 4
4 10
4 9
4 8
9 13
13 14
3 1
7 3
2 1
2 6
6 12
11 6
5 2
11 3
7 8
2 8
11 1
8 14
5 7
9 14 */
[other] Div的更多相关文章
- IE6/7下空div占用空间的问题
最近注意力没在前端上面,工作碰到这样一个问题,下意识的写了句 font-size:0;line-height:0;哪知道引发了更大的bug.后来插入数据进去的时候都不显示了..再后来百度一番找到,原来 ...
- div实现自适应高度的textarea,实现angular双向绑定
相信不少同学模拟过腾讯的QQ做一个聊天应用,至少我是其中一个. 过程中我遇到的一个问题就是QQ输入框,自适应高度,最高高度为3row. 如果你也像我一样打算使用textarea,那么很抱歉,你一开始就 ...
- 冒泡,setinterval,背景图的div绑定事件,匿名函数问题
1.会冒泡到兄弟元素么? $(function(){ $("#a").click(function(){alert("a")}) $("#b" ...
- css居中div的几种常用方法
在开发过程中,很多需求需要我们居中一个div,比如html文档流当中的一块div,比如弹出层内容部分这种脱离了文档流等.不同的情况有不同的居中方式,接下来就分享下一下几种常用的居中方式. 1.text ...
- Div Vertical Menu ver5
这个小功能,如果是算此次,已经是第5次修改了.可以从这里看到前4次:V1, http://www.cnblogs.com/insus/archive/2011/10/17/2215637.html V ...
- 计算Div标签内Checkbox个数或已被disabled的个数
先看下面的html: 计算div内的checkbox个数:$('#divmod input[type="checkbox"]').length 计算div内checkbox被dis ...
- [jquery]显示隐藏div标签的几种方法
1.$("#demo").attr("style","display:none;");//隐藏div $("#demo" ...
- CSS3变形记(上):千变万化的Div
传统上,css就是用来对网页进行布局和渲染网页样式的.然而,css3的出现彻底打破了这一格局.了解过css3的人都知道,css3不但可以对网页进行布局和渲染样式,还可以绘制一些图形.对元素进行2D和3 ...
- 如何隐藏DIV对象
DIV对象在网页里面,相当于一个容器,在其内部,可以显示文字.图片.视频控件等等. 以下的教程,和大家一起来学习,如何隐藏DIV对象. 这必须使用CSS来控制,才能达到隐藏的目的,那么,就得使用CSS ...
- css实现div,文字水平居中的方案。
本文的目的为记录个人开发中常用的几种居中方案,以供大家参考. //basic css html, body { height: 100%; width: 100%; margin: 0; paddin ...
随机推荐
- k8s安装ingress
1. 环境准备 安装nginx-ingress-controller和backend cd /etc/yum.repos.d/mainfests 下载镜像的脚本 vi ingressnginx.sh ...
- 如何加入 Skype for Business 会议?
参加一个线上培训,收到了Skype的参会地址,是这个样子的 然后就是一脸懵逼的不知道怎么参加会议了.找了半天终于在同事的帮助下参加成功. 我的参加方法:在Window上用Skype for Busin ...
- MySQL 5.7使用xtabackup报错解决
报错信息: InnoDB: An optimized (without redo logging) DDLoperation has been performed. All modified page ...
- 手动实现自己的spring事务注解
spring事务是基于同一个数据连接来实现的,认识到这一点是spring事务的关键,spring事务的关键点便在于在事务中不管执行几次db操作,始终使用的是同一个数据库连接.通过查看源码,我们可以看到 ...
- 此方法显式使用的 CAS 策略已被 .NET Framework 弃用。若要出于兼容性原因而启用 CAS 策略,请使用 NetFx40_LegacySecurityPolicy 配置开关
使用DEV8.3winform控件,框架从.net2.0升级到4.0后,程序报错,调用的目标异常. 此方法显式使用的 CAS 策略已被 .NET Framework 弃用.若要出于兼容性原因而启用 C ...
- Extending WCF using IServiceBehavior, IOperationBehavior, and IParameterInspector
[ServiceContract(Name = "PasswordGenerator")] public interface IPasswordGenerator { [Opera ...
- 简介:google ctemplate:简单易用的文字模板(转载)
转自:http://blog.csdn.net/aladdina/article/details/4531736 CTemplate 是一个简单实用.功能强大的文字模板(template langua ...
- 五、eureka客户端自动配置
所有文章 https://www.cnblogs.com/lay2017/p/11908715.html 正文 前面的几篇文章中,我们从eureka Server端的角度看了看eureka的几个核心要 ...
- php中需要注意的函数(持续更新)
explode 函数 $a = null; explode("#",$a); //不会报错会返回一个只包含空字符串的数组
- iOS - 毛玻璃特效
iOS7.0的SDK并没有提供给开发者实现毛玻璃效果的API,所以很多人都是通过一些别人封装的框架来实现 iOS7.0(包括)之前还是有系统的类可以实现毛玻璃效果的, 就是 UIToolbar这个类, ...