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. mvc4帮助类

    @App @AppState @Ajax @Cache @Context @Culture @Html @IsPost @Layout @Model @Output @OutputStack @Pag ...

  2. CloudStack4.2 更新全局参数

    { "updateconfigurationresponse": { "configuration": { "category": &quo ...

  3. iOS开发-核心动画随笔

    核心动画可以让View旋转,缩放,平移(主要是操作View的layer(层)属性)但是核心动画改变的位置不是真实的位置,一切都是假象所以有时候要用到其他动画,如UIView本来封装的动画,还有定时器 ...

  4. HTML第二天学习笔记

    今天看视频学习的第一个知识是HTML中的块元素<div>和行内元素<span>. <!doctype html> <html lang="en&qu ...

  5. ExtJs内的datefield控件选择日期过后的事件监听select

    [摘要]: 选择时间过后我们为什么需要监听事件?一般有这样一种情况,那就是用于比较两个时间大小或者需要判断在哪个时间点上需要做什么样的操作.基于这样的种种情况,我们很有必要琢磨一下datefield控 ...

  6. Oracle 建表常用数据类型的详解

    创建表时,必须为表的各个列指定数据类型.如果实际的数据与该列的数据类型不相匹配,则数据库会拒绝保存.如为学生指定出生日期为“1980-13-31”. 在Oracle中,常见的数据类型有: 字符串:字符 ...

  7. Cookie和Session专题

    一.cookie机制和session机制的区别***************************************************************************** ...

  8. Objective-C的对象模型和runtime机制

    内容列表 对象模型(结构定义,类对象.元类和实例对象的关系) 消息传递和转发机制 runtime系统功能理解 对象模型 结构定义 对象(Object): OC中基本构造单元 (building blo ...

  9. [Node.js] npm init && npm install

    npm init: For create package.json file which will recode the dependence. npm install: You can also w ...

  10. java生成汉字验证码

    java实现的汉字输入验证码,主要包含两个类,一个是生成验证码,一个是判断验证码输入是否正确,实现原理非常简单,将汉字和干扰线生成图片并将汉字保存到session,前台获取每次生成验证码图片并用文本框 ...