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的拼凑 ...
随机推荐
- asp.net请求管道和页面生命周期
- Golang (Go语言) Mac OS X下环境搭建 环境变量配置 开发工具配置 Sublime Text 2 【转】
一.安装Golang的SDK 在官网 http://golang.org/ 直接下载安装包安装即可.下载pkg格式的最新安装包,直接双击运行,一路按照提示操作即可完成安装. 安装完成后,打开终端,输入 ...
- win8.1系统如何激活
若是系统是win8.1或者win8系统,可能由于产品过期或者采用的系统不是正版的话,会出现windows 未激活的状态,想要激活需下载一个win8/win8.1系统 激活工具. http://www. ...
- Policy Gradient
Policy Gradient是区别于Q-Learning为代表的value based的方法.policy gradient又可以叫reinforce算法(Williams, 1992). 如今的A ...
- reduce内置高阶函数求和
>>> def f(x, y): ... return x+y ... >>> reduce(f, a, ) >>> reduce(lambda ...
- 技能UP:SAP CO掌上配置手册
No. 配置对象 事务代码 路径 1 Enterprise Structure and General Controlling configration Maintain EC-PCA : ...
- 【转】【Android】Android Studio打包全攻略
项目写完了,现在需要把应用上传到市场,问题出现—怎么把代码变成.apk(Android的可安装文件).1. 创建签名文件2. 填写好签名参数3. 生成APK注意:签名的密码和密匙的密码注意保管,不要忘 ...
- (笔记)Linux下的ioctl()函数详解
我这里说的ioctl函数是指驱动程序里的,因为我不知道还有没有别的场合用到了它,所以就规定了我们讨论的范围.写这篇文章是因为我前一阵子被ioctl给搞混了,这几天才弄明白它,于是在这里清理一下头脑. ...
- Android notifyDataSetChanged
notifyDataSetChanged()用于动态的更新ListView中的数据.最后还是会调用Adapter中的getView函数. notifyDataSetChanged()相比于setAda ...
- 转载: PHP错误:Warning: Cannot modify header information - headers already sent by ...
如果在执行php程序时看到这条警告:"Warning: Cannot modify header information - headers already sent by ....&quo ...