题目链接

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

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. jQuery源代码解析(3)—— ready载入、queue队列

    ready.queue放在一块写,没有特殊的意思,仅仅是相对来说它俩可能源代码是最简单的了.ready是在dom载入完毕后.以最高速度触发,非常实用. queue是队列.比方动画的顺序触发就是通过默认 ...

  2. http免费升级https 攻略(超简单)

    1.注册沃通SSL免费证书 https://buy.wosign.com/FreeSSL.html 2.验证邮件域名,并下载证书 3.打开IIS,找到服务器证书选择导入,选择下载下来的证书 4.设置网 ...

  3. 【嵌入式】——arm裸机开发 step by step 之 按键控制 LED 和 蜂鸣器

    一.arm-9 TQ2440 key.h #ifndef __KEY_H__ #define __KEY_H__ #define GPFCON (*(volatile unsigned long *) ...

  4. 解决springmvc使用ResponseBody注解返回json中文乱码问题

    spring版本:4.2.5.RELEASE 查看“org.springframework.http.converter.StringHttpMessageConverter”源码,中有一段说明: B ...

  5. Java设计模式(5)共享模式/享元模式(Flyweight模式)

    Flyweight定义:避免大量拥有相同内容的小类的开销(如耗费内存),使大家共享一个类(元类). 为什么使用共享模式/享元模式 面向对象语言的原则就是一切都是对象,但是如果真正使用起来,有时对象数可 ...

  6. Html5學習重點清單

    SVG webSQL 數據庫 SSE 服務推送 MathML 基於xml語法 Web 存储 webSockets通信 canvas 畫布操作 音頻和視頻 地理位置 Geolocation API We ...

  7. C++ vector清空元素的三种方法

    #include <iostream> #include <vector> using namespace std; //STL vector的几种清空容器(删除)办法 voi ...

  8. mysql游标的使用

    这是一个游标的使用例子. 但是其中有几点需要注意,就是为什么要加入 declare CONTINUE HANDLER FOR SQLSTATE '02000' SET tmpname = null;这 ...

  9. hashMap put方法 第二行代码 1

    public interface Zi<K,V> { //我是接口 //通过Zi<K,V> zi的方式可以让我实现多态 //多态的好处是: //1. 应用程序不必为每一个派生类 ...

  10. 关于Unity中的刚体和碰撞器的相关用法(一)

    1.创建一个3D工程 2.构造项目文件目录 3.保存场景为game_scene到文件夹scenes中 4.创建一个Plane平面类型的GameObject节点和一个Sphere球体类型的GameObj ...