题目链接

对于每一个节点,记录这个节点所在链的信息:

ls:(链的上端点)距离链内部近期的白点距离

rs:(链的下端点)距离链内部近期的白点距离

注意以上都是实边

虚边的信息用一个set维护。

set维护的是对于每一个不是链上,可是this的子树,那些子树中距离this近期的白点距离。

#include <stdio.h>
#include <string.h>
#include <set>
#include <algorithm>
#include <iostream>
#include <vector>
template <class T>
inline bool rd(T &ret) {
char c; int sgn;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && (c<'0' || c>'9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
template <class T>
inline void pt(T x) {
if (x <0) {
putchar('-');
x = -x;
}
if (x>9) pt(x / 10);
putchar(x % 10 + '0');
}
using namespace std;
typedef long long ll;
const int inf = 1e9;
const int N = 1e5 + 10;
struct Node *null;
struct Node{
multiset<int>chain;
Node *fa, *ch[2];
int size;
int col, ls, rs, id, len, edge;
//黑色col=-inf, 白色col=0 len是链长
bool rev;
inline void put(){
printf("%d son:%d,%d fa:%d len:%d (%d,%d) col:%d\n", id, ch[0]->id, ch[1]->id, fa->id, len, ls, rs, col);
for (auto i : chain)printf("%d ", i); puts("");
}
inline void clear(int _id){
fa = ch[0] = ch[1] = null;
size = 1;
rev = 0;
len = edge = 0;
id = _id;
chain.clear(); chain.insert(inf);
col = inf;
ls = rs = inf;
}
inline void push_up(){
size = 1 + ch[0]->size + ch[1]->size;
len = edge + ch[0]->len + ch[1]->len;
int m0 = min(col, *chain.begin()), ml = min(m0, ch[0]->rs + edge), mr = min(m0, ch[1]->ls);
ls = min(ch[0]->ls, ch[0]->len + edge + mr);
rs = min(ch[1]->rs, ch[1]->len + ml);
}
inline void push_down(){
if (rev){
ch[0]->flip();
ch[1]->flip();
rev = 0;
}
}
inline void setc(Node *p, int d){
ch[d] = p;
p->fa = this;
}
inline bool d(){
return fa->ch[1] == this;
}
inline bool isroot(){
return fa == null || fa->ch[0] != this && fa->ch[1] != this;
}
inline void flip(){
if (this == null)return;
swap(ch[0], ch[1]);
rev ^= 1;
}
inline void go(){//从链头開始更新到this
if (!isroot())fa->go();
push_down();
}
inline void rot(){
Node *f = fa, *ff = fa->fa;
int c = d(), cc = fa->d();
f->setc(ch[!c], c);
this->setc(f, !c);
if (ff->ch[cc] == f)ff->setc(this, cc);
else this->fa = ff;
f->push_up();
}
inline Node*splay(){
go();
while (!isroot()){
if (!fa->isroot())
d() == fa->d() ? fa->rot() : rot();
rot();
}
push_up();
return this;
}
void debug(Node *x){
if (x == null)return;
x->put();
debug(x->ch[0]);
debug(x->ch[1]);
}
inline Node* access(){//access后this就是到根的一条splay,而且this已经是这个splay的根了
for (Node *p = this, *q = null; p != null; q = p, p = p->fa){
p->splay();
// debug(q); puts(""); debug(p); puts("");
if (p->ch[1] != null)
p->chain.insert(p->ch[1]->ls);
if (q != null)
p->chain.erase(p->chain.find( q->ls ));
p->setc(q, 1);
p->push_up();
// debug(q); puts(""); debug(p); puts("");
}
return splay();
}
inline Node* find_root(){
Node *x;
for (x = access(); x->push_down(), x->ch[0] != null; x = x->ch[0]);
return x;
}
void make_root(){
access()->flip();
}
void cut(){//把这个点的子树脱离出去
access();
ch[0]->fa = null;
ch[0] = null;
push_up();
}
void cut(Node *x){
if (this == x || find_root() != x->find_root())return;
else {
x->make_root();
cut();
}
}
void link(Node *x){
if (find_root() == x->find_root())return;
else {
make_root(); fa = x;
}
}
};
Node pool[N], *tail;
Node *node[N]; vector<int>G[N];
void dfs(int u, int fa){
for (int v : G[u]){
if (v == fa)continue;
node[v]->fa = node[u];
node[v]->edge = 1;
dfs(v, u);
node[u]->chain.insert(node[v]->ls);
}
node[u]->push_up();
}
int n, q;
void debug(Node *x){
if (x == null)return;
x->put();
debug(x->ch[0]);
debug(x->ch[1]);
}
int main(){
while (cin >> n){
tail = pool;
null = tail++;
null->clear(0);
null->size = 0;
for (int i = 1; i <= n; i++)
{
G[i].clear();
node[i] = tail++;
node[i]->clear(i);
}
for (int i = 1, u, v; i < n; i++){
rd(u); rd(v);
G[u].push_back(v); G[v].push_back(u);
}
dfs(1, 1);
// for (int i = 1; i <= n; i++)debug(node[i]), puts("");
rd(q);
while (q--){
int u, v; rd(u); rd(v);
if (u == 0)
{
node[v]->access();
if (node[v]->col == inf)node[v]->col = 0;
else node[v]->col = inf;
node[v]->push_up();
}
else
{
node[v]->access();
int ans = min(*node[v]->chain.begin(), node[v]->rs);
if (ans == inf)ans = -1;
pt(ans); puts("");
}
// for (int i = 1; i <= n; i++)debug(node[i]), puts("");
}
}
return 0;
}

SPOJ QTREE5 lct的更多相关文章

  1. SPOJ 2939 QTREE5 LCT

    维护信息的方式十分巧妙~ 维护每一棵 splay 中深度最浅,深度最深的点距离最近的白点. 这样非常方便维护,进行区间合并,进行子树维护 很多时候在维护东西的时候最大/最小/深度最小/深度最大会相对容 ...

  2. SPOJ - OTOCI LCT

    OTOCI Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/problem/viewProblem. ...

  3. SPOJ QTREE4 lct

    题目链接 这个题已经处于花式tle了,改版后的spoj更慢了.. tle的话就多交几把... #include <iostream> #include <fstream> #i ...

  4. SPOJ QTREE3 lct

    题目链接 题意: 给定n个点 q个询问 以下n-1行给出树边,点有黑或白色.初始化为白色 以下q行: 询问有2种: 1. 0 x 把x点黑变白,白变黑 2.1 x 询问Path(1,x)路径上第一个黑 ...

  5. SPOJ QTREE2 lct

    题目链接 题意: 给一棵树.有边权 1.询问路径的边权和 2.询问沿着路径的第k个点标. 思路:lct裸题. #include <iostream> #include <fstrea ...

  6. SPOJ QTREE5

    题意 一棵\(n\)个点的树,点从\(1\)到\(n\)编号.每个点可能有两种颜色:黑或白. 我们定义\(dist(a,b)\)为点\(a\)至点\(b\)路径上的边个数. 一开始所有的点都是黑色的. ...

  7. SPOJ QTREE6 lct

    题目链接 岛娘出的题.还是比較easy的 #include <iostream> #include <fstream> #include <string> #inc ...

  8. SPOJ - QTREE5 Query on a tree V 边分治

    题目传送门 题意:给你一棵树, 然后树上的点都有颜色,且原来为黑,现在有2个操作,1 改变某个点的颜色, 2 询问树上的白点到u点的最短距离是多少. 题解: 这里用的还是边分治的方法. 把所有东西都抠 ...

  9. QTREE5 - Query on a tree V——LCT

    QTREE5 - Query on a tree V 动态点分治和动态边分治用Qtree4的做法即可. LCT: 换根后,求子树最浅的白点深度. 但是也可以不换根.类似平常换根的往上g,往下f的拼凑 ...

随机推荐

  1. map-reduce 优化

    map阶段优化 参数:io.sort.mb(default 100) 当map task开始运算,并产生中间数据时,其产生的中间结果并非直接就简单的写入磁盘. 而是会利用到了内存buffer来进行已经 ...

  2. 【html】param 以及 embed 的有关 flash 属性详解

    本文主要介绍 param 和 embed 标签中有关 flash 的一些属性及其属性值. 首先我们需要知道 param 和 embed 标签是单独出现的,而不是成对出现的,下面的代码是一般 flash ...

  3. maven工程如何引用css和js文件

    工程目录结构如下图: 目的: 在index.jsp中引用hello.js和base.css文件 实现: 在web.xml中,新增<servlet-mapping>     <serv ...

  4. webpack4--热更新

    所谓热更新,就是在浏览器能同步刷新你的代码.webpack 热更新依赖 webpack-dev-server.具体实现步骤如下: 1.局部安装依赖 webpack-dev-server npm ins ...

  5. Java HashSet和HashMap源码剖析

    转自: Java HashSet和HashMap源码剖析 总体介绍 之所以把HashSet和HashMap放在一起讲解,是因为二者在Java里有着相同的实现,前者仅仅是对后者做了一层包装,也就是说Ha ...

  6. Ogre2.1 结合OpenGL3+高效渲染

    在DX10与OpenGL3+之前,二者都是固定管线与可编程管线的混合,其中对应Ogre1.x的版本,也是结合固定与可编程管线设计.转眼到了OpenGL3+与DX10后,固定管线都被移除了,相对应着色器 ...

  7. 【WPF】自定义鼠标样式

    /// <summary> /// This class allow you create a Cursor form a Bitmap /// </summary> inte ...

  8. 第三百三十二节,web爬虫讲解2—Scrapy框架爬虫—Scrapy使用

    第三百三十二节,web爬虫讲解2—Scrapy框架爬虫—Scrapy使用 xpath表达式 //x 表示向下查找n层指定标签,如://div 表示查找所有div标签 /x 表示向下查找一层指定的标签 ...

  9. .NET Best Practices

    Before starting with best practices tobe followed, it is good to have clear understanding of how mem ...

  10. 有术:DIY代理服务器

    FQ有术:DIY代理服务器 公司HTTP代理穿透+手機ShadowSocks+SSH翻牆 利用SSH代理爬墙 http://bestvpnchina.net/