Query on a tree again!

给出一棵树,树节点的颜色初始时为白色,有两种操作:

0.把节点x的颜色置反(黑变白,白变黑)。

1.询问节点1到节点x的路径上第一个黑色节点的编号。

分析:

先树链剖分,线段树节点维护深度最浅的节点编号。

注意到,如果以节点1为树根时,显然每条重链在一个区间,并且区间的左端会出现在深度浅的地方。所以每次查找时发现左区间有的话,直接更新答案。

9929151 2013-08-28 10:45:55 Query on a tree again! 100
edit  run
12.54 27M

C++

4.3.2

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll;
typedef unsigned long long ull; #define debug puts("here")
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define foreach(i,vec) for(unsigned i=0;i<vec.size();i++)
#define pb push_back
#define RD(n) scanf("%d",&n)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)
#define All(vec) vec.begin(),vec.end()
#define MP make_pair
#define PII pair<int,int>
#define PQ priority_queue
#define cmax(x,y) x = max(x,y)
#define cmin(x,y) x = min(x,y)
#define Clear(x) memset(x,0,sizeof(x))
/* #pragma comment(linker, "/STACK:1024000000,1024000000") int size = 256 << 20; // 256MB
char *p = (char*)malloc(size) + size;
__asm__("movl %0, %%esp\n" :: "r"(p) ); */ /******** program ********************/ const int MAXN = 200005; int son[MAXN],tid[MAXN],top[MAXN],dep[MAXN],fa[MAXN],sz[MAXN],tim;
bool use[MAXN];
int id[MAXN];
int po[MAXN],tol; struct segTree{
int l,r,pos,c;
inline int mid(){
return (l+r)>>1;
}
}tree[MAXN<<2]; struct Edge{
int y,next;
}edge[MAXN<<1]; inline void add(int x,int y){
edge[++tol].y = y;
edge[tol].next = po[x];
po[x] = tol;
} // 树链剖分部分
void dfsFind(int x,int pa,int depth){
dep[x] = depth;
fa[x] = pa;
sz[x] = 1;
son[x] = 0;
for(int i=po[x];i;i=edge[i].next){
int y = edge[i].y;
if(y==pa)continue;
dfsFind(y,x,depth+1);
sz[x] += sz[y];
if(sz[y]>sz[ son[x] ])
son[x] = y;
}
} void dfsCon(int x,int pa){
use[x] = true;
top[x] = pa;
tid[x] = ++ tim;
if(son[x])dfsCon(son[x],pa);
for(int i=po[x];i;i=edge[i].next){
int y = edge[i].y;
if(use[y])continue;
dfsCon(y,y);
}
} void build(int l,int r,int rt){
tree[rt].l = l;
tree[rt].r = r;
tree[rt].pos = 0;
tree[rt].c = 0;
if(l==r) return;
int mid = tree[rt].mid();
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
} void modify(int pos,int rt){
if(tree[rt].l==tree[rt].r){
tree[rt].c ^= 1;
if(tree[rt].c) tree[rt].pos = id[tree[rt].l];
else tree[rt].pos = 0;
return;
}
int mid = tree[rt].mid();
if(pos<=mid)modify(pos,rt<<1);
else modify(pos,rt<<1|1); if(tree[rt<<1].c){
tree[rt].c = tree[rt<<1].c;
tree[rt].pos = tree[rt<<1].pos;
}else{
tree[rt].c = tree[rt<<1|1].c;
tree[rt].pos = tree[rt<<1|1].pos;
}
} int ask(int l,int r,int rt){
if(tree[rt].c==0)return 0;
if(l<=tree[rt].l&&tree[rt].r<=r)
return tree[rt].pos;
int mid = tree[rt].mid();
if(r<=mid)return ask(l,r,rt<<1);
else if(l>mid)return ask(l,r,rt<<1|1);
else{
int t = ask(l,r,rt<<1);
if(t)return t;
return ask(l,r,rt<<1|1);
}
} inline int ask(int y){
int x = 1;
int ans = -1;
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]])
swap(x,y);
int t = ask(tid[top[x]],tid[x],1);
if(t)ans = t;
x = fa[top[x]];
}
if(dep[x]>dep[y])swap(x,y);
int t = ask(tid[x],tid[y],1);
if(t)ans = t;
return ans;
} int main(){ #ifndef ONLINE_JUDGE
freopen("sum.in","r",stdin);
//freopen("sum.out","w",stdout);
#endif int x,y,n,q,op;
while(~RD2(n,q)){
Clear(po);
tol = 0;
REP(i,2,n){
RD2(x,y);
add(x,y);
add(y,x);
} dfsFind(1,1,1);
tim = 0;
Clear(use);
dfsCon(1,1);
rep1(i,n)
id[ tid[i] ] = i; build(1,n,1); while(q--){
RD2(op,x);
if(op==0) modify(tid[x],1);
else printf("%d\n",ask(x));
}
} return 0;
}

  

QTREE3 spoj 2798. Query on a tree again! 树链剖分+线段树的更多相关文章

  1. Spoj Query on a tree SPOJ - QTREE(树链剖分+线段树)

    You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...

  2. Aizu 2450 Do use segment tree 树链剖分+线段树

    Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show ...

  3. 【POJ3237】Tree(树链剖分+线段树)

    Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...

  4. POJ3237 Tree 树链剖分 线段树

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ3237 题意概括 Description 给你由N个结点组成的树.树的节点被编号为1到N,边被编号为1 ...

  5. 【CF725G】Messages on a Tree 树链剖分+线段树

    [CF725G]Messages on a Tree 题意:给你一棵n+1个节点的树,0号节点是树根,在编号为1到n的节点上各有一只跳蚤,0号节点是跳蚤国王.现在一些跳蚤要给跳蚤国王发信息.具体的信息 ...

  6. Water Tree CodeForces 343D 树链剖分+线段树

    Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...

  7. SPOJ QTREE Query on a tree 树链剖分+线段树

    题目链接:http://www.spoj.com/problems/QTREE/en/ QTREE - Query on a tree #tree You are given a tree (an a ...

  8. spoj QTREE - Query on a tree(树链剖分+线段树单点更新,区间查询)

    传送门:Problem QTREE https://www.cnblogs.com/violet-acmer/p/9711441.html 题解: 树链剖分的模板题,看代码比看文字解析理解来的快~~~ ...

  9. SPOJ QTREE Query on a tree ——树链剖分 线段树

    [题目分析] 垃圾vjudge又挂了. 树链剖分裸题. 垃圾spoj,交了好几次,基本没改动却过了. [代码](自带常数,是别人的2倍左右) #include <cstdio> #incl ...

随机推荐

  1. DateTable与List<T>相互转换 及JSON与DataTable(DataSet)相互转化

    http://www.360doc.com/content/13/0712/09/10504424_299336674.shtml Linq处理List数据 http://blog.163.com/l ...

  2. 关于刘冬大侠Spring.NET系列学习笔记3的一点勘正

    诚如他第22楼“只因渴求等待”提出的疑问一样,他的下面那一段代码是存在一点点问题的, XElement root = XElement.Load(fileName); var objects = fr ...

  3. css中的img和input标签

    一般情况下,行内元素设置宽高是无效的,常见的有a标签.img和input也属于行内元素,但他们却可以设置宽高!!!! 查阅了一些资料才明白,原来css的元素还有另外一种分类方法,可替换元素,不可替换元 ...

  4. 转载:mysql update更新带子查询的实现方式

    出自:http://576017120.iteye.com/blog/1947154 mysql中更新时不能直接将更新的表作为查询的表,可以通过临时中间表的形式. 总结一下: 一:单表更新时 例如: ...

  5. 8.实现(Realization)

    实现关系是用来描述接口和实现接口的类或者构建结构之间的关系,接口是操作的集合,而这些操作就用于规定类或者构建结构的一种服务. 在接口和类之间的实现关系中,类实现了接口,类中的操作实现了接口中所声明的操 ...

  6. 解析Ceph: Snapshot

    经常有开发者在邮件列表中会问到Ceph Snapshot的实现方式,受限于目前有限的实现文档和复杂的代码结构和代码量,弄清楚Ceph Snapshot并不是一件容易的事.正好最近在重构Ceph存储引擎 ...

  7. perl学习笔记(3)—— 坑

    (1)用perl来ls一个目录: 写perl的时候,经常要调用到系统命令,perl有很多等价的函数可以用,但是,不小心任性了,就想用system来实现了,好吧,来个ls把,列出指定的一个目录,直接上代 ...

  8. 【M27】要求或者禁止对象产生于heap之中

    1.要求对象只能产生于heap之中,该怎么办? 栈上的对象肯定调用构造方法和析构方法(离开作用域的时候),因此,要求对象只能产生于heap之中,也就是禁止栈上产生对象,解决办法有两种:将所有的构造方法 ...

  9. EasyUI-在iframe里获取现阶段选中的tab的标题

    在iframe里获取当前选中的tab的标题(easyui) var currTab =$$('#tabs').tabs('getSelected'); console.info(currTab.pan ...

  10. Codeforces Gym 100015F Fighting for Triangles 状压DP

    Fighting for Triangles 题目连接: http://codeforces.com/gym/100015/attachments Description Andy and Ralph ...