BC ROUND 43# 03 HDU 5266
弱啊弱啊,我用了扩展指令,然后大牛告诉我,只对VC++有用,对G++没用的。。shit,三题就这样没了。
方法是使用ST在线算法,O(1)查询,然后用线段树维护。。呃感觉这个好慢。看了大斌神的是用LCA倍增+维护一个表 ,得学习一下。。。
先贴弱的代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std; const int N=300015; struct Edge{
int u,v,next;
}edge[N*2];
int head[N],tot,dtot;
int dp[N*2][20];
int ver[2*N],R[2*N],first[N],dir[N];
bool vis[N];
int ans;
int tag[N*4]; void addedge(int u,int v){
edge[tot].u=u;
edge[tot].v=v;
edge[tot].next=head[u];
head[u]=tot++;
} void dfs(int u ,int dep) {
vis[u] = true; ver[++dtot] = u; first[u] = dtot; R[dtot] = dep;
for(int k=head[u]; k!=-1; k=edge[k].next)
if( !vis[edge[k].v] ) {
int v = edge[k].v ;
dfs(v,dep+1);
ver[++dtot] = u; R[dtot] = dep;
}
} void ST(int n) {
for(int i=1;i<=n;i++)
dp[i][0] = i;
for(int j=1;(1<<j)<=n;j++) {
for(int i=1;i+(1<<j)-1<=n;i++) {
int a = dp[i][j-1] , b = dp[i+(1<<(j-1))][j-1];
dp[i][j] = R[a]<R[b]?a:b;
}
}
} int RMQ(int l,int r) {
int k=0;
while((1<<(k+1))<=r-l+1)
k++;
int a = dp[l][k], b = dp[r-(1<<k)+1][k];
return R[a]<R[b]?a:b;
} int LCA(int u ,int v) {
int x = first[u] , y = first[v];
if(x > y) swap(x,y);
int res = RMQ(x,y);
return ver[res];
} void query(int rt,int l,int r,int L,int R){
// cout<<L<<" "<<R<<endl;
if(tag[rt]!=-1&&l<=L&&R<=r){
if(ans==-1) ans=tag[rt];
else {
ans=LCA(ans,tag[rt]);
}
return ;
}
else if(L==R){
// cout<<L<<" "<<ans<<endl;
tag[rt]=L;
if(ans==-1)
ans=L;
else{
ans=LCA(ans,L);
}
return ;
}
int m=(L+R)>>1;
if(r<=m){
query(rt<<1,l,r,L,m);
}
else if(l>=m+1){
query(rt<<1|1,l,r,m+1,R);
}
else{
query(rt<<1,l,r,L,m);
query(rt<<1|1,l,r,m+1,R);
}
if(tag[rt<<1]!=-1&&tag[rt<<1|1]!=-1)
tag[rt]=LCA(tag[rt<<1],tag[rt<<1|1]);
} int main(){
int n,q,u,v;
while(scanf("%d",&n)!=EOF){
memset(head,-1,sizeof(int)*(n+5));
memset(vis,false,sizeof(bool)*(n+5));
memset(tag,-1,sizeof(int)*(n*4));
tot=0;
for(int i=1;i<n;i++){
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
dtot=0;
dfs(1,1);
ST(2*n-1);
tag[1]=1;
scanf("%d",&q);
// printf("%d",LCA(2,3));
// printf("%d",LCA(1,2));
// printf("%d",LCA(3,4));
while(q--){
scanf("%d%d",&u,&v);
ans=-1;
query(1,u,v,1,n);
printf("%d\n",ans);
}
}
return 0;
}
BC ROUND 43# 03 HDU 5266的更多相关文章
- BestCoder Round #43
T1:pog loves szh I(hdu 5264) 题目大意: 给出把AB两个字符串交叉拼起来的结果,求出原串. 题解: 不解释..直接每次+2输出. T2:pog loves szh II(h ...
- HDU 5266 pog loves szh III (线段树+在线LCA转RMQ)
题目地址:HDU 5266 这题用转RMQ求LCA的方法来做的很easy,仅仅须要找到l-r区间内的dfs序最大的和最小的就能够.那么用线段树或者RMQ维护一下区间最值就能够了.然后就是找dfs序最大 ...
- hdu 5266 pog loves szh III(lca + 线段树)
I - pog loves szh III Time Limit:6000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
- 字符串处理 BestCoder Round #43 1001 pog loves szh I
题目传送门 /* 字符串处理:是一道水题,但是WA了3次,要注意是没有加'\0'的字符串不要用%s输出,否则在多组测试时输出多余的字符 */ #include <cstdio> #incl ...
- 贪心/二分查找 BestCoder Round #43 1002 pog loves szh II
题目传送门 /* 贪心/二分查找:首先对ai%=p,然后sort,这样的话就有序能使用二分查找.贪心的思想是每次找到一个aj使得和为p-1(如果有的话) 当然有可能两个数和超过p,那么an的值最优,每 ...
- Educational Codeforces Round 43
Educational Codeforces Round 43 A. Minimum Binary Number 显然可以把所有\(1\)合并成一个 注意没有\(1\)的情况 view code / ...
- HDU 5266 bc# 43 LCA+跳表
学了一发LCA的倍增算法+跳表维护. 先说说LCA倍增算法,思路是fa[i][j]求的是i结点的2^j倍的祖先,其中2^0就是父结点了.所以可以递推fa[i][j]=fa[fa[i][j-1]][j- ...
- HDU 5618 Jam's problem again CDQ分治 BC ROUND 70
题意:给你1e5个点(x,y,z),对于每一个点询问有多少个点(x1,y1,z1)满足x1<=x&&y1<=y&&z1<=z 分析:(官方题解奉上)很 ...
随机推荐
- Ubuntu中安装部署Intel CS WebRTC
1环境要求 组件 版本要求 OS CentOS* 7.4, Ubuntu 14.04/16.04 LTS Node 8.11.* (推荐8.11.1) MongoDB 2.4.9 Boost 1.65 ...
- python自动化测试学习笔记-2-字典、元组、字符串方法
一.字典 Python字典是另一种可变容器模型,且可存储任意类型对象,如字符串.数字.元组等其他容器模型. 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割, ...
- ashx 文件的使用
它就类似.aspx文件,用于处理传入到服务器的HTTP请求,但它不会像.aspx文件那样要返回处理结果和大量HTML,它可以返回简单的字符串.图片等. 百度百科定义链接:http://baike.ba ...
- Django总结三
回顾: - 装饰器 模板 - def wrapper(func): def inner(*args, **args): 执行被执行函数之前的操作 res = func(*args, **args) 执 ...
- GitLab Runner and CICD
# Linux x86-64 sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaw ...
- B树、B+树、红黑树、AVL树比较
B树是为了提高磁盘或外部存储设备查找效率而产生的一种多路平衡查找树. B+树为B树的变形结构,用于大多数数据库或文件系统的存储而设计. B树相对于红黑树的区别 在大规模数据存储的时候,红黑树往往出现由 ...
- [ CTSC 2007 / BZOJ 2151 ] Backup / 种树
\(\\\) \(Description\) 给出一个数轴上\(N\)个点的坐标\(A_i\),选择\(K\)个点对,使得这\(K\)个点对每个点对的距离之和尽可能小. \(N\in [0,10^5] ...
- cordova科大讯飞语音识别
cordova-plugin-IFlyspeech 科大讯飞的语音听说读写的cordova插件 Supported Platforms iOS android Installation 插件安装命令: ...
- 电源管理POWER_SUPPLY_PROP_CAPACITY_LEVEL
电量计节点中有capacity_level 节点,这个是反应当前电池电流高低水平的参数. 分为critical low full normal 一般是由fg的芯片来判断,通过IIC读取,具体判断可参考 ...
- Ruby开发环境的搭建
1.Ruby的下载 https://rubyinstaller.org/downloads/ 2.Ruby的安装 3.Eclipse配置Ruby开发环境 插件地址:http://rubyeclipse ...