题目链接

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

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. wps不记录打开打开的文件

    “工具”中的“选项”后,进入选项界面,在“常规与保存”选项卡中把“最近文档管理列出文件”前面的勾取消或将后面的数字选为0.

  2. RavenDb学习(四)处理文档相关性

    RavenDb是文档型数据库,但是我们常常也需要定义对象之间的关系,那RavenDb当中是如何处理的呢? RavenDb提供了优雅的解决方式,使用正确的话,可以减少数据开销以及网络拥堵 Denorma ...

  3. mongo 杀掉慢的程序killMyRunningOps("12.23.32.21") #####这个是客户端的ip

    mongodb运维(3) db.currentOp与db.killOp命令 2018.08.12 23:55 113浏览  字号 好久没更新mongo运维这块知识了,这次介绍 db.currentOp ...

  4. Vue路由获取路由参数

    vue路由设置路由参数有2种方式: 1.通过query配置: <router-link :to="{ name:'login',query:{id:1} }">登录&l ...

  5. JSON常见操作

    1.JSON---> 字符串:JSON.stringify(json) 看如下代码: let json={"orderId":"E2018081400181122& ...

  6. 完全图解RNN、RNN变体、Seq2Seq、Attention机制

    完全图解RNN.RNN变体.Seq2Seq.Attention机制 本文主要是利用图片的形式,详细地介绍了经典的RNN.RNN几个重要变体,以及Seq2Seq模型.Attention机制.希望这篇文章 ...

  7. error: navicat 连接debian系列系统mysql 10038问题解决方案

    还特么有一种可能 阿里云 也有防火墙出口入口规则,这里也有问题

  8. 【转】【Python】python使用urlopen/urlretrieve下载文件时出现403 forbidden的解决方法

    第一:urlopen出现403 #!/usr/bin/env python # -*- coding: utf- -*- import urllib url = "http://www.go ...

  9. e822. 监听JScrollPane的滚动

    A scrollbar in a scroll pane fires adjustment events whenever its value changes. // Create a scrolla ...

  10. python调用ansible接口API执行命令

    python版本:Python 2.6.6 ansible版本:ansible 2.3.1.0      下载地址:https://releases.ansible.com/ansible/ 调用脚本 ...