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. SQLServer2005 提示 '其他会话正在使用事务的上下文'

    MSDN上看了一下说是sql server 2005不支持在分布式事务处理中存在指向本地的链接服务器(环回链接服务器) 这个是官方的回答 个人认为,应该是在事务中,使用了链接服务器访问进行跨库访问引起 ...

  2. vs2008 release下调试状态设置[转]

    这是一个老生常谈的话题,但还是有时候会漏洞一些设置.总结一些,总共需要三个地方设置, 分别是1)c\c++-> General->Debug Information Format. 2) ...

  3. Extjs 实现输入数量,实时更改总价

    // 总价 var totalNum = '0.00'; //总价初始值 var $total = new Ext.form.Label({ text: '消费金额 : ¥' + totalNum + ...

  4. canvas绘制直线和多边形基本操作

    <!DOCTYPE HTML> <html lang="en"> <body> <canvas id="canvas" ...

  5. CorelDraw x6【Cdr x6】官方简体中文破解版(64位)安装图文教程、破解注册方法

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  6. memcached Logging

    For reasons now relegated to history, Spy has its own logging implementation. However, it is compati ...

  7. WaitForMultipleObject与MsgWaitForMultipleObjects用法

    http://blog.csdn.net/byxdaz/article/details/5638680 用户模式的线程同步机制效率高,如果需要考虑线程同步问题,应该首先考虑用户模式的线程同步方法. 但 ...

  8. WSB备份到远程共享文件夹的限制

    WSB备份存储类型: 远程共享文件夹: 可以将一次性(临时)备份和计划备份存储在远程共享文件夹上.(将计划备份存储在远程共享文件夹上的功能是 Windows Server 2008 R2 的新增功能. ...

  9. 【转】myeclipse的破解方法

    获得myeclipse的长期使用权限 以下主要内容来自:http://www.sxrczx.com/t/article/8ad0ed7521434d278d401bdeea5fd820.htm 1.下 ...

  10. VS 统计代码行数

    使用正则表达式 :    ^:b*[^:b#/]+.*$ 简单说明如下: ^:行头 $:行尾 +:一个或多个 *:0个或多个 :b:表示空白,匹配空格或者制表符 # 表示后面是include,/表示注 ...