【SPOJ-QTREE3】树链剖分
http://www.spoj.com/problems/QTREE3/
时间限制:2s 代码长度限制:50000B 内存限制:1536MB
【题目描述】
给出N个点的一棵树(N-1条边),节点有白有黑,初始全为白
有两种操作:
0 i : 改变某点的颜色(原来是黑的变白,原来是白的变黑)
1 v : 询问1到v的路径上的第一个黑点,若无,输出-1
总共有12组数据。
1/3 的数据, N=5000, Q=400000.
1/3 的数据, N=10000, Q=300000.
1/3 的数据,N=100000, Q=100000.
【输入格式】
单组数据的。
第一行 N and Q.表示N个点和Q个操作
下来N-1条无向边
下来 Q行,每行一个操作"0 i" 或者"1 v" (1 ≤ i, v ≤ N).
【输出格式】
遇到 "1 v"操作的时候,输出结果。
这题就是树链剖分,线段树维护当前区间最左边的黑点的编号。因为是单点修改,所以根本不用lazy,也不用记录点的颜色(看它所维护的最左边的点这个值是否为0)。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std; const int N=;
struct trnode{
int lc,rc,l,r,c,d,lazy;
}t[*N];
struct node{
int x,y,next;
}a[*N];
int n,m,tl,z,len;
int first[N],tot[N],son[N],fa[N],dep[N],ys[N],yss[N],top[N]; void ins(int x,int y)
{
len++;
a[len].x=x;a[len].y=y;
a[len].next=first[x];first[x]=len;
} int maxx(int x,int y){return x>y ? x:y;}
int minn(int x,int y){return x<y ? x:y;} int build_tree(int l,int r)
{
int x=++tl;
t[x].l=l;t[x].r=r;
t[x].c=t[x].d=t[x].lazy=;
t[x].lc=t[x].rc=;
if(l<r)
{
int mid=(l+r)>>;
t[x].lc=build_tree(l,mid);
t[x].rc=build_tree(mid+,r);
}
return x;
} void change(int x,int p)
{
int lc=t[x].lc,rc=t[x].rc,mid=(t[x].l+t[x].r)>>;
if(t[x].l==t[x].r)
{
if(t[x].d==) t[x].d=t[x].l;
else t[x].d=;
return;
}
if(p<=mid) change(lc,p);
else change(rc,p);
if(t[lc].d) t[x].d=t[lc].d;
else if(t[rc].d) t[x].d=t[rc].d;
else t[x].d=;
} int query(int x,int l,int r)
{
if(t[x].l==l && t[x].r==r) return t[x].d;
int lc=t[x].lc,rc=t[x].rc,mid=(t[x].l+t[x].r)>>;
if(r<=mid) return query(lc,l,r);
else if(l>mid) return query(rc,l,r);
else
{
int t1=query(lc,l,mid);
if(t1) return t1;
int t2=query(rc,mid+,r);
if(t2) return t2;
return ;
}
} void dfs1(int x)
{
tot[x]=;son[x]=;
for(int i=first[x];i;i=a[i].next)
{
int y=a[i].y;
if(y==fa[x]) continue;
fa[y]=x;
dep[y]=dep[x]+;
dfs1(y);
if(tot[son[x]]<tot[y]) son[x]=y;
tot[x]+=tot[y];
}
} void dfs2(int x,int tp)
{
ys[x]=++z;yss[z]=x;top[x]=tp;
if(son[x]) dfs2(son[x],tp);
for(int i=first[x];i;i=a[i].next)
{
int y=a[i].y;
if(y==fa[x] || y==son[x]) continue;
dfs2(y,y);
}
} int solve(int y)
{
int ty=top[y],ans=-;
while(ty!=)
{
int t=query(,ys[ty],ys[y]);
if(t) ans=yss[t];
y=fa[ty];ty=top[y];
}
int t;
if(y==) t=query(,,);
else t=query(,,ys[y]);
if(t) ans=yss[t];
return ans;
} int main()
{
freopen("a.in","r",stdin);
// freopen("me.out","w",stdout);
scanf("%d%d",&n,&m);
tl=;len=;z=;
memset(first,,sizeof(first));
for(int i=;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
ins(x,y),ins(y,x);
}
build_tree(,n);
fa[]=;dep[]=;dfs1();
dfs2(,);
for(int i=;i<=m;i++)
{
int tmp,x;
scanf("%d%d",&tmp,&x);
if(!tmp) change(,ys[x]);
else
{
int ans=solve(x);
if(ans) printf("%d\n",ans);
else printf("-1\n");
}
}
return ;
}
【SPOJ-QTREE3】树链剖分的更多相关文章
- SPOJ 375 树链剖分
SPOJ太慢了,SPOJ太慢了, 题意:给定n(n<=10000)个节点的树,每条边有边权,有两种操作:1.修改某条变的边权:2.查询u,v之间路径上的最大边权. 分析:树链剖分入门题,看这里: ...
- SPOJ QTREE 树链剖分
树链剖分的第一题,易懂,注意这里是边. #include<queue> #include<stack> #include<cmath> #include<cs ...
- SPOJ 375 (树链剖分 - 边权剖分 - 修改单边权)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28982#problem/I 给你一棵有边权的树,有两个操作:一个操作是输出l到 ...
- SPOJ 375 (树链剖分+线段树)
题意:一棵包含N 个结点的树,每条边都有一个权值,要求模拟两种操作:(1)改变某条边的权值,(2)询问U,V 之间的路径中权值最大的边. 思路:最近比赛总是看到有树链剖分的题目,就看了论文,做了这题, ...
- SPOJ 375 树链剖分 QTREE - Query on a tree
人生第一道树链剖分的题目,其实树链剖分并不是特别难. 思想就是把树剖成一些轻链和重链,轻链比较少可以直接修改,重链比较长,用线段树去维护. 貌似大家都是从这篇博客上学的. #include <c ...
- Spoj Query on a tree SPOJ - QTREE(树链剖分+线段树)
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...
- spoj 375 树链剖分模板
/* 只是一道树链刨分的入门题,作为模板用. */ #include<stdio.h> #include<string.h> #include<iostream> ...
- 【学术篇】SPOJ QTREE 树链剖分
发现链剖这东西好久不写想一遍写对是有难度的.. 果然是熟能生巧吧.. WC的dalao们都回来了 然后就用WC的毒瘤题荼毒了我们一波, 本来想打个T1 44分暴力 然后好像是特判写挂了还是怎么的就只能 ...
- spoj 375 树链剖分 模板
QTREE - Query on a tree #tree You are given a tree (an acyclic undirected connected graph) with N no ...
- Qtree3 - 树链剖分
打完以后才发现写复杂了……算了懒得改了 #include <bits/stdc++.h> using namespace std; ],fa[][],size[],wson[],vis[] ...
随机推荐
- Spring MVC: 环境搭建并实现简易的HelloWorld
第一步:使用配置Tomcat服务器的Eclipse新建一个名为“TestSpringMVC”的web项目 第二步:将所使用的jar包复制到WEB-INF/lib目录下 第三步:在web.xml中配置D ...
- 利尔达NB-IOT的PSM和eDRX低功耗模式笔记
1. NB-IOT的技术优势,广覆盖,NB-IOT与GPRS和LTE相比较,最大链路预算提升了20dB,相当于提升了100倍,即使在地车车库.地下室.地下管道等普通无线网络信号难以到达的地方也容易覆盖 ...
- mongodb 安装使用遇到的问题记录
mongodb 常用命令: https://www.mongodb.org/downloads 官网64位下载链接 https://fastdl.mongodb.org/linux/mongodb-l ...
- java使用java.util.Properties读取properties文件的九种方法
直接上代码: package com.test.test; import java.io.BufferedInputStream; import java.io.FileInputStream; im ...
- linux下 su 与 su - 的区别和使用
Linux下su与su -命令的区别 在启动服务器ntpd服务时遇到一个问题 使用 su root 切换到root用户后,不可以使用service命令: 使用 su - 后,就可以使用servic ...
- Python 学习笔记之 Numpy 库——文件操作
1. 读写 txt 文件 a = list(range(0, 100)) a = np.array(a) # a.dtype = np.int64 np.savetxt("filename. ...
- android扁平化ProgressBar--progressWheel
ProgressWheel是git是一个开源项目,为开发者提供一个扁平化的ProgressBar,并可对其进行深度定制 1,将ProgressWheel的源码拷贝到项目中 public class ...
- ASP.NET 概述
https://msdn.microsoft.com/zh-cn/library/4w3ex9c2(VS.100).aspx ASP.NET 概述 更新:2007 年 11 月 ASP.NET 是一个 ...
- HDU G-免费馅饼
http://acm.hdu.edu.cn/showproblem.php?pid=1176 Problem Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然 ...
- 【PHP】- Apache设置
Apache配置 1.首先新建一个自己的amp目录(模仿wampserver安装目录),以后的apache,mysql,php都放在此目录下. 2.下载apache 根据自己的系统下载相应的压缩包,我 ...