题目链接

题意:

给定n个点 q个询问

以下n-1行给出树边,点有黑或白色。初始化为白色

以下q行:

询问有2种:

1、 0 x 把x点黑变白,白变黑

2、1 x 询问Path(1,x)路径上第一个黑点的点标, 若不存在黑点则输出-1

思路:

lct裸题

#include <iostream>
#include <fstream>
#include <string>
#include <time.h>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <stack>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
using namespace std;
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');
}
typedef long long ll;
typedef pair<int, int> pii;
const int N = 30005;
const int inf = 10000000;
struct Node *null;
struct Node{
Node *fa, *ch[2];
int size;
int val, ma, sum, id;
bool rev;
inline void put(){
printf("%d:id, %d,%d,%d (%d,%d) fa:%d \n", id, val, ma, sum, ch[0]->id, ch[1]->id, fa->id);
}
inline void clear(int _val, int _id){
fa = ch[0] = ch[1] = null;
size = 1;
rev = 0;
id = _id;
val = ma = sum = _val;
}
inline void push_up(){
size = 1 + ch[0]->size + ch[1]->size; sum = ma = val;
if (ch[0] != null) {
sum += ch[0]->sum;
ma = max(ma, ch[0]->ma);
}
if (ch[1] != null){
sum += ch[1]->sum;
ma = max(ma, ch[1]->ma);
}
}
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;
}
inline Node* access(){//access后this就是到根的一条splay,而且this已经是这个splay的根了
for (Node *p = this, *q = null; p != null; q = p, p = p->fa){
p->splay()->setc(q, 1);
p->push_up();
}
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];
int n, q;
void debug(Node *x){
if (x == null)return;
x->put();
debug(x->ch[0]);
debug(x->ch[1]);
}
inline void change(Node* x){
x->access();
x->val ^= 1;
x->push_up();
}
inline int ask(Node* x){
node[1]->make_root();
x->access();
node[1]->splay();
// for (int i = 1; i <= n; i++)debug(node[i]), putchar('\n');
Node *r = node[1];
if (r->sum == 0)return -1;
while (true){
if (r->ch[0]->sum == 0 && r->val)return r->id;
r = r->ch[r->ch[0]->sum==0];
}
}
struct Edge{
int from, to, nex;
}edge[N << 1];
int head[N], edgenum;
void add(int u, int v){
Edge E = { u, v, head[u] };
edge[edgenum] = E;
head[u] = edgenum++;
}
void dfs(int u, int fa){
for (int i = head[u]; ~i; i = edge[i].nex){
int v = edge[i].to; if (v == fa)continue;
dfs(v, u);
node[v]->push_up();
node[v]->fa = node[u];
}
}
int main(){
while (cin>>n>>q){
memset(head, -1, sizeof head); edgenum = 0;
tail = pool;
null = tail++; null->clear(0, 0);
null->size = 0; null->sum = 0;
for (int i = 1; i <= n; i++) {
node[i] = tail++;
node[i]->clear(0, i);
}
for (int i = 1, u, v; i < n; i++){
rd(u); rd(v);
add(u, v); add(v, u);
}
dfs(1, 1);
int u, v;
while (q--){
rd(u); rd(v);
if (u == 0)change(node[v]);
else pt(ask(node[v])), putchar('\n');
}
}
return 0;
}
/*
9 8
1 2
1 3
2 4
2 9
5 9
7 9
8 9
6 8 1 3
0 8
1 6
1 7
0 2
1 9
0 2
1 9
*/

SPOJ QTREE3 lct的更多相关文章

  1. SPOJ QTREE3 Query on a tree again! ——Link-Cut Tree

    [题目分析] QTREE2,一看是倍增算法,太懒了,不写了.( ̄_, ̄ ) QTREE3,树链剖分可以做,发现链上的问题LCT也很好做. 要是子树问题貌似可以DFS序. 然后就成LCT模板题了. 考前 ...

  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. Query on a tree again! SPOJ - QTREE3

    https://vjudge.net/problem/SPOJ-QTREE3 https://www.luogu.org/problemnew/show/P4116 一个log(LCT)比两个log( ...

  5. SPOJ QTREE2 lct

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

  6. SPOJ QTREE5 lct

    题目链接 对于每一个节点,记录这个节点所在链的信息: ls:(链的上端点)距离链内部近期的白点距离 rs:(链的下端点)距离链内部近期的白点距离 注意以上都是实边 虚边的信息用一个set维护. set ...

  7. SPOJ QTREE3 - Query on a tree again!

    You are given a tree (an acyclic undirected connected graph) with N nodes. The tree nodes are number ...

  8. SPOJ QTREE6 lct

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

  9. 【模板时间】◆模板·II◆ 树链剖分

    [模板·II]树链剖分 学长给我讲树链剖分,然而我并没有听懂,还是自学有用……另外感谢一篇Blog +by 自为风月马前卒+ 一.算法简述 树链剖分可以将一棵普通的多叉树转为线段树计算,不但可以实现对 ...

随机推荐

  1. 第3天-DIV+CSS布局

    盒子模型 margin 设置外边距宽度 有4个值的时候: maigin 10px 5px 15px 20px; (上.右.下.左)有3个值的时候: margin: 10px 5px 15px; (上. ...

  2. Xamarin XAML语言教程构建ControlTemplate控件模板 (四)

    Xamarin XAML语言教程构建ControlTemplate控件模板 (四) 2.在页面级别中构建控件模板 如果开发者要在页面级别中构建控件模板,首先必须将ResourceDictionary添 ...

  3. 软件配置篇-MySQL下载及安装

    1.进入MySQL官网下载 下载地址:https://dev.mysql.com/downloads/mysql/ 选择合适的版本下载. 2.安装MySQL 解压后文件夹一般名为:mysql-x.x. ...

  4. 使用GIT进行源码管理 —— VisualStudio官方GIT教程

    我之前在文章使用GIT进行源码管理 —— 在VisualStudio中使用GIT中简单的介绍了一下如何使用VS中自带的Git工具,今天发现MSDN上现在也有了非常完整的教程,感兴趣的朋友可以看一下: ...

  5. [HTML/CSS]uploadify自定义按钮样式

    概述 在项目中经常用到uploadify上传插件,但是FLASH按钮的外观往往跟我们网页的设计的主题色不太搭配.这时就需要对其样式进行修改. 样式文件是uploadify.css. 打开这个文件后,你 ...

  6. zip压缩与解压文件夹或文件

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...

  7. html5:localStorage储存

    实例:刷新值会增长,关掉浏览器,再打开,值会在原基础上增长 if(localStorage.pagecount){ localStorage.pagecount=Number(localStorage ...

  8. virtualbox 设置windows 于ubuntu虚拟机共享文件夹

    会弹出错误, .. ..还是会有错误, modprobe       -a vboxsf 之后就可以挂载成功了!!

  9. Socket网络通讯开发总结之:Java 与 C进行Socket通讯(转)

    先交待一下业务应用背景:服务端:移动交费系统:基于C语言的Unix系统客户端:增值服务系统:基于Java的软件系统通迅协议:采用TCP/IP协议,使用TCP以异步方式接入数据传输:基于Socket流的 ...

  10. BIN文件如何打开

    有些BIN文件用DAEMON Tools也无法打开 但是UltraISO可以打开,我们看到有Setup.exe,但是如果直接双击无法运行.我们可以先把所有东西都提取出来.   这样之后再点击Setup ...