SPOJ QTREE5 lct
题目链接
对于每一个节点,记录这个节点所在链的信息:
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的更多相关文章
- SPOJ 2939 QTREE5 LCT
维护信息的方式十分巧妙~ 维护每一棵 splay 中深度最浅,深度最深的点距离最近的白点. 这样非常方便维护,进行区间合并,进行子树维护 很多时候在维护东西的时候最大/最小/深度最小/深度最大会相对容 ...
- SPOJ - OTOCI LCT
OTOCI Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/problem/viewProblem. ...
- SPOJ QTREE4 lct
题目链接 这个题已经处于花式tle了,改版后的spoj更慢了.. tle的话就多交几把... #include <iostream> #include <fstream> #i ...
- SPOJ QTREE3 lct
题目链接 题意: 给定n个点 q个询问 以下n-1行给出树边,点有黑或白色.初始化为白色 以下q行: 询问有2种: 1. 0 x 把x点黑变白,白变黑 2.1 x 询问Path(1,x)路径上第一个黑 ...
- SPOJ QTREE2 lct
题目链接 题意: 给一棵树.有边权 1.询问路径的边权和 2.询问沿着路径的第k个点标. 思路:lct裸题. #include <iostream> #include <fstrea ...
- SPOJ QTREE5
题意 一棵\(n\)个点的树,点从\(1\)到\(n\)编号.每个点可能有两种颜色:黑或白. 我们定义\(dist(a,b)\)为点\(a\)至点\(b\)路径上的边个数. 一开始所有的点都是黑色的. ...
- SPOJ QTREE6 lct
题目链接 岛娘出的题.还是比較easy的 #include <iostream> #include <fstream> #include <string> #inc ...
- SPOJ - QTREE5 Query on a tree V 边分治
题目传送门 题意:给你一棵树, 然后树上的点都有颜色,且原来为黑,现在有2个操作,1 改变某个点的颜色, 2 询问树上的白点到u点的最短距离是多少. 题解: 这里用的还是边分治的方法. 把所有东西都抠 ...
- QTREE5 - Query on a tree V——LCT
QTREE5 - Query on a tree V 动态点分治和动态边分治用Qtree4的做法即可. LCT: 换根后,求子树最浅的白点深度. 但是也可以不换根.类似平常换根的往上g,往下f的拼凑 ...
随机推荐
- jstorm之于storm
关于流处理框架,在先前的文章汇总已经介绍过Strom,今天学习的是来自阿里的的流处理框架JStorm.简单的概述Storm就是:JStorm 比Storm更稳定,更强大,更快,Storm上跑的程序,一 ...
- JavaScript(五):变量的作用域
一.变量的分类 在JavaScript中变量分为两种: 全局变量 局部变量 二.变量的作用域 1.局部变量的作用域 局部变量:在函数内部定义的变量称为局部变量,其作用域为该函数内部,在该函数外部不能被 ...
- "No user exists for uid 501"
"No user exists for uid 501" 问题表现:git操作远端失败. iterm2的问题,更改了配置可能导致这个问题,最简单的解决办法,退出客户端后重启.重 ...
- JS 二维数组排序
<script> //测试方法 var a = [ {name:'hdj', age:128}, {name:'hdj1', age:28}, {name:'hdj1', age:78}, ...
- 《FPGA全程进阶---实战演练》第二十一章之 几种常用电平分析及特性
TTL,CMOS以及LVTTL,LVCMOS TTL和CMOS是数字电路中两种常见的逻辑电平,LVTTL和LVCMOS是两者低电平版本.TTL是流控器件,输入电阻小,TTL电平器件速度快,驱动能力大, ...
- Spring加载静态资源的方式
解决方法1:在web.xml里添加如下的配置 <servlet-mapping> <servlet-name>default</servlet-name> < ...
- 动态SQL是什么??什么是静态SQL,动态SQL的动态体现在哪里???
首先,所谓SQL的动态和静态,是指SQL语句在何时被编译和执行,二者都是用在SQL嵌入式编程中的,这里所说的嵌入式是指将SQL语句嵌入在高级语言中,而不是针对于单片机的那种嵌入式编程.在某种高级语言中 ...
- SAP 物料移动类型查询表
Goods movement w/o referenceB Goods movement for purchase orderF Goods movement for production order ...
- (笔记)Linux中的终端、控制台、tty、pty
1>tty(终端设备的统称): tty一词源于teletypes,或者teletypewriters,原来指的是电传打字机,是通过串行线用打印机键盘阅读和发送信息的东西,后来这东西被键盘与显示器 ...
- JDBC创建数据库实例
在本教程将演示如何在JDBC应用程序中创建数据库. 在执行以下示例之前,请确保您已经准备好以下操作: 具有数据库管理员权限,以在给定模式中创建数据库. 要执行以下示例,需要用实际用户名和密码替换这里用 ...