[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 ...
随机推荐
- jvm--工具
jps (java process status) == ps / top 作用:显示所有运行中的java进程. jstat 作用:查看类装载,内存,垃圾收集,jit编译的信息. jinfo 作用:实 ...
- 整合MyBatis
配置数据源相关属性(见前一章节 DataSource配置) 引入依赖 <dependency> <groupId>org.mybatis.spring.boot</gro ...
- [Done] Codeforces Round #562 (Div. 2) 题解
A - Circle Metro 模拟几百步就可以了. B - Pairs 爆搜一下,时间复杂度大概是 $O(4 * n)$ Code: 56306723 C - Increasing by Modu ...
- VS.NET(C#)--1.3_VS2005开始
VS2005开始 开始页 1.文件系统:这是默认,把网站创建到当前物理文件系统上(可以本地或网络).此时VS2005将使用内置的Web服务器,不使用IIS运行Web应用程序.2.HTTP使用IIS处理 ...
- ASP.NET Core利用拦截器 IActionFilter实现权限控制
“麦荻网教系统”采用了前后端代码分离的架构,即“Miidy.Cloud.Console”站与“Miidy.Cloud.Manage”站(两个前端站)同时通过web api的方式调用“Miidy.Clo ...
- 数据结构之队列(queue)
队列介绍 1.队列是一个有序列表,可以用数组或是链表来实现. 2.遵循先入先出的原则.即:先存入队列的数据,要先取出.后存入的要后取出. 应用场景 比如某某银行叫号系统: 数组模拟队列 队列本身是有序 ...
- 总结一下几个for循环常见用法和区别
1.for循环//有人喜欢使用一个临时变量把长度缓存起来,说是数据量大时效果更好(我本人没有去验证) for(j = 0,len=arr.length; j < len; j++) { //执行 ...
- SPI简述
SPI是器件的比较常用的通信协议. SPI总共有四根线: SS:片选线,每个设备都和主机MCU有一条单独片选线相连,片选线拉低意味主机输出,也就是说一个主机可以和多个从机相连,只需要有足够多的片选线. ...
- Java基本知识点o(1), o(n), o(logn), o(nlogn)的了解
在描述算法复杂度时,经常用到o(1), o(n), o(logn), o(nlogn)来表示对应算法的时间复杂度, 这里进行归纳一下它们代表的含义: 这是算法的时空复杂度的表示.不仅仅用于表示时间复杂 ...
- SQL SERVER中如何查找存储过程中一段代码
select b.name ,a.text from syscomments a,sysobjects b where and object_id(b.name)=a.id and b.xtype i ...