QTREE3 spoj 2798. 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! 树链剖分+线段树的更多相关文章
- 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, ...
- 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 ...
- 【POJ3237】Tree(树链剖分+线段树)
Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...
- POJ3237 Tree 树链剖分 线段树
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ3237 题意概括 Description 给你由N个结点组成的树.树的节点被编号为1到N,边被编号为1 ...
- 【CF725G】Messages on a Tree 树链剖分+线段树
[CF725G]Messages on a Tree 题意:给你一棵n+1个节点的树,0号节点是树根,在编号为1到n的节点上各有一只跳蚤,0号节点是跳蚤国王.现在一些跳蚤要给跳蚤国王发信息.具体的信息 ...
- Water Tree CodeForces 343D 树链剖分+线段树
Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...
- 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 ...
- spoj QTREE - Query on a tree(树链剖分+线段树单点更新,区间查询)
传送门:Problem QTREE https://www.cnblogs.com/violet-acmer/p/9711441.html 题解: 树链剖分的模板题,看代码比看文字解析理解来的快~~~ ...
- SPOJ QTREE Query on a tree ——树链剖分 线段树
[题目分析] 垃圾vjudge又挂了. 树链剖分裸题. 垃圾spoj,交了好几次,基本没改动却过了. [代码](自带常数,是别人的2倍左右) #include <cstdio> #incl ...
随机推荐
- JavaScript一些不常用的写法
如何写JavaScript才能逼格更高呢?怎样才能组织JavaScript才能让别人一眼看出你不简单呢?是否很期待别人在看完你的代码之后感叹一句“原来还可以这样写”呢?下面列出一些在JavaScrip ...
- MVC中Model用法
Model:对于MVC来说,Model可不简单只是实体,它应该叫视图模型,这是我自己的理解,即视图中的数据由Model来提供,当视图的数据需要改变时,我们不用打开aspx或ascx文件,只需要修改相应 ...
- GIT 中提示 please tell me who you are
如果使用git过程中出现了,please tell me who you are ,需要设置一下使用者的身份. 1.git config user.name "username" ...
- KVO机制
KVO,全称为Key-Value Observing,是iOS中的一种设计模式,用来监测对象的某些属性的实时变化情况并作出响应 首先,假设我们的目标是在一个UITableViewController内 ...
- ubuntu完美卸载JDK
要删除 OpenJDK (如果已安装的话).首先,检查是安装的哪个 OpenJDK包. # dpkg --list | grep -i jdk 移除 openjdk包: # apt-get purge ...
- jq获取表单值与赋值代码
jq获取表单值与赋值代码 jq获取表单值与赋值代码 $("#keyword")[0].value = ""; /*获得TEXT.AREATEXT的值*/ var ...
- Linux中/usr与/var目录详解
/usr文件系统 /usr 文件系统经常很大,因为所有程序安装在这里. /usr 里的所有文件一般来自Linux distribution:本地安装的程序和其他东西在/usr/local 下.这样可能 ...
- Android获取网页上的图片的代码
public Bitmap getWebBitmap(String imgUrl) { Bitmap bitmap =null; try { InputStream inputStream = nul ...
- Windows下memcache安装使用
Windows下Memcache安装 随着时间的推移,网上现在能找到的在 Windows下安装 Memcache 的文档大多已经过时.雪峰这里再简要介绍一下当下最新版的安装和配置方法. Memcach ...
- iOS 如何判断当前网络连接状态 网络是否正常 网络是否可用
网络资源:出处http://blog.csdn.net/mad1989/article/details/8987368 众所周知,我们在开发APP时,涉及网络连接的时候,都会想着提前判断一下当前的 ...