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. OC:copy 与 retain 的区别

    copy与retain的区别: copy是创建一个新对象,retain是创建一个指针,引用对象计数加1.Copy属性表示两个对象内容相同,新的对象retain为1 ,与旧有对象的引用计数无关,旧有对象 ...

  2. tomcat解压war包的一点例外

    我在项目的开发过程中,发现Tomcat解压war 的一点例外.        现象如下: 使用ANT工具把web应用程序打包为war文件.然后把war文件放到tomcat的webapps,让tomca ...

  3. C++ Code_StatusBar

    主题 1. 创建状态栏 并显示 2. 在状态栏中显示进度条 3. MDI文档显示和隐藏状态栏 4. 5.     代码::创建状态栏 并显示 //手动添加3个ICON //////////////// ...

  4. j2ee项目后置类代码实现跳出iframe页面跳转

    response.getWriter().write("<script>top.location.href='../admin/login';</script>&qu ...

  5. Sqlserver事务发布实现数据同步

    事务的功能在sqlserver中由来已久,因为最近在做一个数据同步方案,所以有机会再次研究一下它以及快照等,发现还是有很多不错的功能和改进的.这 里以sqlserver2008的事务发布功能为例,对发 ...

  6. Citrix 服务器虚拟化之二十七 XenApp6.5发布服务器桌面

    Citrix 服务器虚拟化之二十七  XenApp6.5发布服务器桌面 XenApp可发布以下类型的资源向用户提供信息访问,这些资源可在服务器或桌面上虚拟化: 1)  服务器桌面:发布场中服务器的整个 ...

  7. 强连通分量(LRJ训练指南)

    #include <iostream> #include <queue> #include <string> #include <cstdio> #in ...

  8. 更换VS2012序列号的方法

    转自:http://blog.sina.com.cn/s/blog_58c506600101ja49.html 今天不小心把还在试用期内的VS2012 Ultimate填上盗版Key激活了……找遍了菜 ...

  9. linux修改history

    1.cat ~/.bash_history cat -n  ~/.bash_history [以行数的形式查看] 2.history | more Enter 键盘  ----------一行一行 空 ...

  10. redis 简介

    Redis是一种高级key-value数据库.它跟memcached类似,不过数据可以持久化,而且支持的数据类型很丰富.有字符串,链表,集 合和有序集合.支持在服务器端计算集合的并,交和补集(diff ...