【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[] ...
随机推荐
- 关于springboot 打包问题 jar包和 war包
起因:项目开发完成 需要打包部署了 发现自己不会打包 那么开始网上学习打包? 那么怎么来打包那? 我们以前没有采用springboot 时候我们都是直接将项目打成war包形式 然后放到tomc ...
- PRO*C 函数事例 2 -- 数据库操作
Pro*C Oracle 的嵌入式开发,数据库处理部分最好能提取到一个模块,按照对不同数据库表的操作分成不同的.pc文件(如 DbsInstStat.pc).将此模块编译成库(c文件编译时链接此库), ...
- springmvc+mybatis的两种配置和应用方式
一.不用写dao层实现的方式 1.导入依赖包,我的pom.xml文件配置如下: <project xmlns="http://maven.apache.org/POM/4.0.0&qu ...
- 你真的了解React吗
https://zhufengzhufeng.github.io/zhufengreact/index.html#t21.%E4%BB%80%E4%B9%88%E6%98%AFReact?
- guacamole实现剪切复制
主要功能是实现把堡垒机的内容复制到浏览器端,把浏览器端的文本复制到堡垒机上. 借助一个中间的文本框,现将堡垒机内容复制到一个文本框,然后把文本框内容复制出来.或者将需要传递到堡垒机的内容先复制到文本框 ...
- Java日志(二):log4j与XML配置文件
[Java日志(一):log4j与.properties配置文件]一文列举的几个案例以.properties文件作为log4j的配置文件,本文简单看一下log4j与XML配置文件 (1)XML配置文件 ...
- kickstart技术安装操作系统
kickstart是RedHat公司开源的软件,所以对CentOS兼容性最好. 原理:我们将手动安装的所有的详细步骤记录到一个文件中,然后kickstart通过读取这个文件就可以实现自动化安装系统. ...
- URAL 1736 Chinese Hockey(网络最大流)
Description Sergey and Denis closely followed the Chinese Football Championship, which has just come ...
- nopcommerce商城系统--文档整理
原址:http://www.nopcommerce.com/documentation.aspx nopCommerce文档可以帮助您一步一步的搭建属于您自己的在线商城.根据该文档说明,您可以选择您想 ...
- webmagic 二次开发爬虫 爬取网站图片
webmagic的是一个无须配置.便于二次开发的爬虫框架,它提供简单灵活的API,只需少量代码即可实现一个爬虫. webmagic介绍 编写一个简单的爬虫 webmagic的使用文档:http://w ...