SPOJ375 QTREE - Query on a tree
2015-12-16 20:49
465 查看
果然不在状态,树剖都写跪了。。。。。。。。
RE了三次,又是多组数据忘了什么该初始化的。
WA了一次,忘了每个点代表它与它父亲连线的边权。
我这拙计的智商啊。。。。。
RE了三次,又是多组数据忘了什么该初始化的。
WA了一次,忘了每个点代表它与它父亲连线的边权。
我这拙计的智商啊。。。。。
#include<iostream> #include<cstdio> #include<cstring> #define lc o<<1 #define rc o<<1|1 using namespace std; const int N=10000+5; const int inf=1e9; struct Node{ int l,r,mx; }tr[N*4]; struct Edge{int to,next,v;}e[N*2]; int head ,cnt,d [2]; int dep ,siz ,son ,fa ,top ,pos ,sz,cost ,rank ; void ins(int u,int v,int w){ e[++cnt].to=v;e[cnt].next=head[u];head[u]=cnt;e[cnt].v=w; } void dfs1(int u){ siz[u]=1;son[u]=0; for(int i=head[u];i;i=e[i].next){ int v=e[i].to; if(v==fa[u])continue; fa[v]=u;dep[v]=dep[u]+1; cost[v]=e[i].v; dfs1(v); siz[u]+=siz[v]; if(siz[v]>siz[son[u]])son[u]=v; } } void dfs2(int u,int tp){ pos[u]=++sz;top[u]=tp;rank[sz]=u; if(son[u])dfs2(son[u],top[u]); for(int i=head[u];i;i=e[i].next){ int v=e[i].to; if(v!=fa[u]&&v!=son[u]) dfs2(v,v); } } inline void pushup(int o){tr[o].mx=max(tr[lc].mx,tr[rc].mx);} void update(int o,int p,int v){ int l=tr[o].l,r=tr[o].r; if(l==r)tr[o].mx=v; else{ int m=l+r>>1; if(p<=m)update(lc,p,v); else update(rc,p,v); pushup(o); } } int query(int o,int a,int b){ int l=tr[o].l,r=tr[o].r; if(l==a&&b==r)return tr[o].mx; else{ int m=l+r>>1; if(b<=m)return query(lc,a,b); else if(m<a)return query(rc,a,b); else return max(query(lc,a,m),query(rc,m+1,b)); } } void build(int o,int l,int r){ tr[o].l=l;tr[o].r=r; if(l==r)tr[o].mx=cost[rank[l]]; else{ int m=l+r>>1; build(lc,l,m);build(rc,m+1,r); pushup(o); } } int ask(int u,int v){ int ans=-inf; while(top[u]!=top[v]){ if(dep[top[u]]<dep[top[v]])swap(u,v); ans=max(ans,query(1,pos[top[u]],pos[u])); u=fa[top[u]]; } if(u==v)return ans; if(dep[u]>dep[v])swap(u,v); return max(ans,query(1,pos[son[u]],pos[v])); } int main(){ int T;scanf("%d",&T); while(T--){ int n;scanf("%d",&n); memset(head,0,sizeof(head));cnt=0;sz=0; int u,v,w; for(int i=1;i<n;i++){ scanf("%d%d%d",&u,&v,&w); d[i][0]=u;d[i][1]=v; ins(u,v,w);ins(v,u,w); } dfs1(1);dfs2(1,1);build(1,1,n); for(int i=1;i<n;i++) if(dep[d[i][0]]<dep[d[i][1]])swap(d[i][0],d[i][1]); char opt[10]; while(true){ scanf("%s",opt); if(opt[0]=='D')break; if(opt[0]=='Q'){ scanf("%d%d",&u,&v); printf("%d\n",ask(u,v)); }else{ scanf("%d%d",&u,&w); update(1,pos[d[u][0]],w); } } } return 0; }
相关文章推荐
- iOS UIAlertController提示框
- Unknown class ViewController in Interface Builder file.
- 利用软妹纸ui写的前端
- UI数据库
- MPAndroidChart 教程:数据格式器 ValueFormatter(五)
- 关于UITableView的重用机制浅谈
- POJ 2524 Ubiquitous Religions
- plist文件及UITableView的高级应用
- IOS UIVideoPlayer 视频不退出界面
- 关于导入GPUimage到工程以及简单使用
- iOS8以上的版本,使用UIAlertController 替代 UIAlert 弹窗
- IOS吐槽狂人:UITabelView全局表头和每个分区的表头和大小,设置区分表头的标题
- 关于UIViewController和UIView类中方法的使用
- leetcode -- Unique Binary Search Trees I&II -- 重点
- Repeated DNA Sequences
- 提升含有动态高度UITableViewCell的UITableView的运行效率
- 动态改变UITableView中的Cell高度
- MiniGUI 如何显示繁体字
- HDU 3177 Crixalis's Equipment(贪婪)
- UITableView中heightForRowAtIndexPath 产生 EXC_BAD_ACCESS 的原因