ural1553 Caves and Tunnels
Caves and Tunnels
Memory limit: 64 MB
Input
Output
Sample
input | output |
---|---|
4 |
1 |
分析:树上点修改+区间极值查询,树链剖分;
代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+,mod=1e9+,inf=0x3f3f3f3f;
int n,m,k,t,tot;
int bl[maxn],pos[maxn],dep[maxn],sz[maxn],fa[maxn];
int mx[maxn<<];
vector<int>e[maxn];
void dfs(int x,int y=)
{
sz[x]=;
for(int i=;i<e[x].size();i++)
{
int z=e[x][i];
if(z==y)continue;
fa[z]=x;dep[z]=dep[x]+;
dfs(z,x);
sz[x]+=sz[z];
}
}
void dfs1(int x,int chain)
{
bl[x]=chain;
pos[x]=++tot;
int big=;
for(int i=;i<e[x].size();i++)
{
int z=e[x][i];
if(dep[z]<dep[x])continue;
if(sz[z]>sz[big])big=z;
}
if(!big)return;
dfs1(big,chain);
for(int i=;i<e[x].size();i++)
{
int z=e[x][i];
if(dep[z]<dep[x]||z==big)continue;
dfs1(z,z);
}
}
void pup(int rt){mx[rt]=max(mx[rt<<],mx[rt<<|]);}
void upd(int x,int y,int l,int r,int rt)
{
if(x==l&&x==r){mx[rt]+=y;return;}
int mid=l+r>>;
if(x<=mid)upd(x,y,l,mid,rt<<);
else upd(x,y,mid+,r,rt<<|);
pup(rt);
}
int q(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)return mx[rt];
int mid=l+r>>;
int ret=;
if(L<=mid)ret=max(ret,q(L,R,l,mid,rt<<));
if(mid+<=R)ret=max(ret,q(L,R,mid+,r,rt<<|));
return ret;
}
int main()
{
int i,j;
//freopen("in.txt","r",stdin);
scanf("%d",&n);
for(i=;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
e[x].push_back(y);
e[y].push_back(x);
}
dfs();
dfs1(,);
scanf("%d",&m);
while(m--)
{
char op[];
int x,y;
scanf("%s%d%d",op,&x,&y);
if(op[]=='I')upd(pos[x],y,,n,);
else
{
int ret=;
while(bl[x]!=bl[y])
{
if(dep[bl[x]]<dep[bl[y]])swap(x,y);
ret=max(ret,q(pos[bl[x]],pos[x],,n,));
x=fa[bl[x]];
}
if(pos[x]>pos[y])swap(x,y);
ret=max(ret,q(pos[x],pos[y],,n,));
printf("%d\n",ret);
}
}
return ;
}
ural1553 Caves and Tunnels的更多相关文章
- URAL1553 Caves and Tunnels 树链剖分 动态树
URAL1553 维护一棵树,随时修改某个节点的权值,询问(x,y)路径上权值最大的点. 树是静态的,不过套动态树也能过,时限卡的严就得上树链剖分了. 还是那句话 splay的核心是splay(x) ...
- URAL 题目1553. Caves and Tunnels(Link Cut Tree 改动点权,求两点之间最大)
1553. Caves and Tunnels Time limit: 3.0 second Memory limit: 64 MB After landing on Mars surface, sc ...
- URAL 1553. Caves and Tunnels 树链拆分
一颗树 每次出发点右键值是0 2操作模式1.第一i右键点值添加x 2.乞讨u至v在这条路上右上方值 树为主的连锁分裂称号 #include <cstdio> #include <cs ...
- Uva1553 Caves and Tunnels LCT
简单题,主要为了练手. #include <cstdio> #include <iostream> #define maxn 100010 using namespace st ...
- LCT(link cut tree) 动态树
模板参考:https://blog.csdn.net/saramanda/article/details/55253627 综合各位大大博客后整理的模板: #include<iostream&g ...
- Sorry, but the Android VPN API doesn’t currently allow TAP-based tunnels.
Sorry, but the Android VPN API doesn’t currently allow TAP-based tunnels. Edit .ovpn configfile “dev ...
- hdu 4856 Tunnels (记忆化搜索)
Tunnels Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- hdu 4856 Tunnels (bfs + 状压dp)
题目链接 The input contains mutiple testcases. Please process till EOF.For each testcase, the first line ...
- CSU1612Destroy Tunnels(强连通)
Destroy Tunnels 原来早忘记了离散里含有这么一个叫传递闭包的东西 矩阵A的闭包B = A U A^2 U A^3 U ... 所以这里直接如果A[i][j]!= 0,建边i->j跑 ...
随机推荐
- FUSE and File System
FUSE: File system in USErspace. So what is a file system? A file system maps file paths to file cont ...
- vim编程配置方法
vim简介Vim 有以下几个模式:1) 正常(normal)模式,缺省的编辑模式:下面如果不加特殊说明,提到的命令都直接在正常模式下输入:任何其它模式中都可以通过键盘上的 Esc 键回到正常模式.2) ...
- AJAX程序实验
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 < ...
- c++ map unordered_map
map operator<的重载一定要定义成const.因为map内部实现时调用operator<的函数好像是const. #include<string> #include& ...
- undefined与null
undefined 声明的变量尚未初始化 null 对象尚未存在 eg: var a; console.log(typeof a); 输出undefined var b= document.getEl ...
- JPA 系列教程9-双向一对一唯一外键
双向一对一唯一外键的ddl语句 CREATE TABLE `t_person` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(25 ...
- OVS - commands
journalctl -t ovs-vswitchd ovs-vsctl show ovs-ofctl show br0 set vlanid ovs-vsctl set port eth0 tag= ...
- java数据结构之有序表查找
这篇文章是关于有序表的查找,主要包括了顺序查找的优化用法.折半查找.插值查找.斐波那契查找: 顺序优化查找:效率极为底下,但是算法简单,适用于小型数据查找: 折半查找:又称为二分查找,它是从查找表的中 ...
- C++文件编程(文件流操作)
给出了比较常见的文件操作,包括二进制文件操作.代码如下: #include<iostream> #include<cstdio> #include<cstring> ...
- 使用Github搭建个人博客网站
1 新建一个repo,创建一个没有父节点的分支gh-pages(github规定,只有该分支中的页面,才会生成网页文件): mkdir jekyll_demo cd jekyll_demo git i ...