【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[] ...
随机推荐
- PIC32MZ 通过USB在线升级 -- USB CDC bootloader
了解bootloader 的实现,请加QQ: 1273623966 (验证填 bootloader):欢迎咨询或定制bootloader:我的博客主页www.cnblogs.com/geekygeek ...
- Deep Learning 之 最优化方法
Deep Learning 之 最优化方法 2017年05月21日 22:18:40 阅读数:5910 写在前面本文主要是对Deep Learning一书最优化方法的总结,具体详细的算法,另起博文展开 ...
- 对工具的反思 & deadlines与致歉
人和动物最大的区别就是使用工具的水平. 有些人只凭着对工具的熟练掌握便成了牛人. 工具,到底应该以何种态度去看待? 在我小的时候,工具仅仅是指树枝.线.粉笔,可以让自己有更多游戏可玩:上学之后,便又有 ...
- ABP官方文档
https://aspnetboilerplate.com/Pages/Documents/Introduction
- 使用polarssl进行RSA加密解密
RSA算法的原理就不提了,网上有很多介绍的文章,因为项目中使用RSA加密,所以需要找一个RSA加密的算法,之前尝试过使用Crypto++库,无奈Crypto++其中使用了大量的模版,各种继承,看着头大 ...
- 「日常训练」 Soldier and Cards (CFR304D2C)
题意 (Codeforces 546C) 按照指定的规则打牌,问谁胜或无穷尽. 分析 又是一条模拟,用set+queue(这里手写了)处理即可.注意到两种局势"1 234"和&qu ...
- Spring常用注解用法总结
转自http://www.cnblogs.com/leskang/p/5445698.html 1.@Controller 在SpringMVC 中,控制器Controller 负责处理由Dispat ...
- python 基础篇 12 装饰器进阶
本节主要内容:1. 通⽤装饰器回顾2. 函数的有⽤信息3. 带参数的装饰器4. 多个装饰器同时装饰⼀个函数 ⼀. 通⽤装饰器的回顾开闭原则: 对增加功能开放. 对修改代码封闭装饰器的作⽤: 在不改变原 ...
- python 动态函数调用及可变参数传入
定义类及方法 class ParameterFactory(object): ..... def fullLinkTag(self, fromDate, toDate, status, cate='全 ...
- 简单的素数问题(C++)
[问题描述] 已知三个素数的和为 n ,正整数 n 由键盘输入,计算并输出这三个素数乘积的最大值. [代码展示] # include<iostream>using namespace st ...