[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 ...
随机推荐
- ubuntu修改密码
ubuntu修改密码 本文链接:https://blog.csdn.net/heybob/article/details/9095727 修改root密码: 1,$sudo su,输入密码进入root ...
- Angular 学习笔记 (Angular 9 & ivy)
refer : https://blog.angularindepth.com/all-you-need-to-know-about-ivy-the-new-angular-engine-9cde47 ...
- 在论坛中出现的比较难的sql问题:18(字符合并 整数解析星期几)
原文:在论坛中出现的比较难的sql问题:18(字符合并 整数解析星期几) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉得 ...
- Extending WCF using IServiceBehavior, IOperationBehavior, and IParameterInspector
[ServiceContract(Name = "PasswordGenerator")] public interface IPasswordGenerator { [Opera ...
- git bash push 本地的commit到远程 -- ssh keys设置
1. 检查是否已经创建 ssh keys git bash 下,cd ~/.ssh 如何出现“No such file or directory”,则表示需要创建一个ssh keys. 2. 创建新 ...
- ORA-01790 错误处理 SQL同一数据库中,两个查询结果数据类型不同时的union all 合
转自: 出现这种错误,要先看一下是不是sql中有用到连接:union,unio all之类的,如果有,需要注意相同名称字段的数据类型一定要相同. 所以在union 或者union all 的时候造成了 ...
- 解决 google 浏览器记住密码导致输入框样式改变(变成淡黄色背景)
直接在页面上使用css代码: input:-webkit-autofill , textarea:-webkit-autofill, select:-webkit-autofill { -webkit ...
- impala 表迁移方式 partquet数据文件移动方法
1.原表查询:select count(*) from edm.ucard_wxd0123 where stat_dt = '2024-01-09' and id_no = '110101199003 ...
- 解决 React-Native: Android project not found. Maybe run react-native android first?
在终端运行命令react-native run-android时报错Android project not found. Maybe run react-native android first? 解 ...
- Java--8--新特性--Stream API
Stream API 提供了一种高效且易于使用的处理数据的方式,(java.util.stream.*) 他可以对数组,集合等做一些操作,最终产生一个新的流,原数据是不会发生改变的. “集合”讲的是数 ...