题目链接

题意:

给定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. MySQL 一张表中两个字段值互换

    update table a, table b set a.filed1= b.field2, a.field2= b.field1where a.id = b.id

  2. 【动态规划】bzoj1649 [Usaco2006 Dec]Cow Roller Coaster

    很像背包. 这种在一个数轴上进行操作的题常常需要对区间排序. f[i][j]表示距离到i时,花费为j时的权值之和. f[x[i]+l[i]][j+c[i]]=max{f[x[i]][j]+w[i]}( ...

  3. tomcat访问(access)日志配置、记录Post请求参数(转)

    一.配置与说明 tomcat访问日志格式配置,在config/server.xml里Host标签下加上 <Valve className="org.apache.catalina.va ...

  4. 配置.net程序集搜索路径

    默认情况下,.net程序对外部程序集dll的搜索路径是exe文件所在的目录,虽然这种方式没有什么太多不好的地方,但是当我们引用外部程序集较多的时候显得非常杂乱.一种比较常用的解决方式是通过配置在app ...

  5. vue生命周期钩子,一张图片

  6. Kubernetes环境下调整WebLogic JVM基本参数

    基于Kubernetes启动WebLogic后,发现JVM的最大heap size一直在700多M左右,通过 kubectl logs 察看pod启动状态,发现日志中并没有-Xms和-Xmx参数.日志 ...

  7. touch: cannot touch ‘/var/jenkins_home/copy_reference_file.log’: Permission denied Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?

    问题:在从 https://c.163.com/hub#/m/repository/?repoId=3093 下载镜像 docker pull hub.c.163.com/library/jenkin ...

  8. Arduino+GPRS 的环境监控方案

    设备前台界面:http://www.lewei50.com/home/gatewaystatus/361#576 本实采用的硬件,除了一个串口模块以外(约200元),其他均可以从taobo上面找到标准 ...

  9. cpp面向对象编程

    如下图,先建好文件, 这里用的是Visual studio 2010 当然也可以用eclipse for cpp,如下图: AbstractShape.h #ifndef ABSTRACTSHAPE_ ...

  10. javascript快速入门8--值,类型与类型转换

    原始值和引用值 在ECMAScript中,变量可以存放两种类型的值,即原始值和引用值. 原始值(primitive value)是存储在栈(stack)中的简单数据段,也就是说,它们的值直接存储在变量 ...