Spoj Query on a tree III
题目描述
给出N个点的一棵树(N-1条边),节点有白有黑,初始全为白
有两种操作:
0 i : 改变某点的颜色(原来是黑的变白,原来是白的变黑)
1 v : 询问1到v的路径上的第一个黑点,若无,输出-1
输入输出格式
输入格式:
第一行 N,Q,表示N个点和Q个操作
第二行到第N行N-1条无向边
再之后Q行,每行一个操作"0 i" 或者"1 v" (1 ≤ i, v ≤ N).
输出格式:
对每个1 v操作输出结果
输入输出样例
9 8
1 2
1 3
2 4
2 9
5 9
7 9
8 9
6 8
1 3
0 8
1 6
1 7
0 2
1 9
0 2
1 9
-1
8
-1
2
-1
说明
For 1/3 of the test cases, N=5000, Q=400000.
For 1/3 of the test cases, N=10000, Q=300000.
For 1/3 of the test cases, N=100000, Q=100000.
(假装我是在spoj上做的一样hhh)
贼裸的树剖+线段树,,,练手题(今天建模能力为0,全在码模板hhh)
#include<bits/stdc++.h>
#define ll long long
#define maxn 100005
#define pb push_back
using namespace std;
vector<int> g[maxn];
int dfn[maxn],dy[maxn],pos;
int siz[maxn],f[maxn],opt;
int cl[maxn],n,m,son[maxn],dc=;
int sum[maxn<<|],mxp[maxn<<|];
int le,ri; void dfs1(int x,int fa){
f[x]=fa,siz[x]=;
int to;
for(int i=g[x].size()-;i>=;i--){
to=g[x][i];
if(to==fa) continue;
dfs1(to,x),siz[x]+=siz[to];
if(!son[x]||siz[to]>siz[son[x]]) son[x]=to;
}
} void dfs2(int x,int tp){
dfn[x]=++dc,dy[dc]=x,cl[x]=tp;
if(son[x]) dfs2(son[x],tp);
int to;
for(int i=g[x].size()-;i>=;i--){
to=g[x][i];
if(to==f[x]||to==son[x]) continue;
dfs2(to,to);
}
} void update(int o,int l,int r){
if(l==r){
sum[o]^=;
if(sum[o]) mxp[o]=dy[l];
else mxp[o]=;
return;
} int mid=l+r>>,lc=o<<,rc=(o<<)|;
if(le<=mid) update(lc,l,mid);
else update(rc,mid+,r);
if(sum[lc]) mxp[o]=mxp[lc];
else mxp[o]=mxp[rc];
sum[o]=sum[lc]+sum[rc];
} int query(int o,int l,int r){
if(l>=le&&r<=ri) return mxp[o];
int mid=l+r>>,lc=o<<,rc=(o<<)|,an=;
if(le<=mid) an=query(lc,l,mid);
if(!an&&ri>mid) an=query(rc,mid+,r);
return an;
} inline void tolt(int x){
le=dfn[x],update(,,n);
} inline int answer(int x){
int an=-,po;
while(x){
le=dfn[cl[x]],ri=dfn[x];
po=query(,,n);
if(po) an=po;
x=f[cl[x]];
}
return an;
} int main(){
int uu,vv;
scanf("%d%d",&n,&m);
for(int i=;i<n;i++){
scanf("%d%d",&uu,&vv);
g[uu].pb(vv),g[vv].pb(uu);
} dfs1(,);
dfs2(,); while(m--){
scanf("%d%d",&opt,&pos);
if(opt) printf("%d\n",answer(pos));
else tolt(pos);
} return ;
}
Spoj Query on a tree III的更多相关文章
- SPOJ Query on a tree III (树剖(dfs序)+主席树 || Splay等平衡树)(询问点)
You are given a node-labeled rooted tree with n nodes. Define the query (x, k): Find the node whose ...
- SPOJ PT07J - Query on a tree III(划分树)
PT07J - Query on a tree III #tree You are given a node-labeled rooted tree with n nodes. Define the ...
- SP1487 PT07J - Query on a tree III (主席树)
SP1487 PT07J - Query on a tree III 题意翻译 你被给定一棵带点权的n个点的有根数,点从1到n编号. 定义查询 query(x,k): 寻找以x为根的k大点的编号(从小 ...
- 【BZOJ1803】Spoj1487 Query on a tree III 主席树+DFS序
[BZOJ1803]Spoj1487 Query on a tree III Description You are given a node-labeled rooted tree with n n ...
- 「SPOJ1487」Query on a tree III
「SPOJ1487」Query on a tree III 传送门 把树的 \(\text{dfs}\) 序抠出来,子树的节点的编号位于一段连续区间,然后直接上建主席树区间第 \(k\) 大即可. 参 ...
- SPOJ 1487 Query on a tree III(划分树)
题目链接:http://www.spoj.com/problems/PT07J/ 题意:给出一个有根树,1为根节点,每个节点有权值.若干询问,询问以u为根的子树中权值第K小的节点编号. 思路:DFS一 ...
- [ SPOJ PT07J ] Query on a tree III
\(\\\) Description 其实这题才是正版的 Qtree3...... 给定 \(n\) 个点,以 \(1\) 号节点为根的树,点有点权. \(m\) 次询问 以 \(x\) 为根的子树内 ...
- bzoj1803: Spoj1487 Query on a tree III
Description You are given a node-labeled rooted tree with n nodes. Define the query (x, k): Find the ...
- SPOJ Query on a tree 树链剖分 水题
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...
随机推荐
- 浅析JavaScript的垃圾回收机制
JavaScript语言是一门优秀的脚本语言.其中包含脚本语言的灵活性外还拥有许多高级语言的特性.例如充许构建和实例化一个对象,垃圾回收机制(GC:Garbage Collecation).通常我们使 ...
- The 13th Zhejiang Provincial Collegiate Programming Contest - C
Defuse the Bomb Time Limit: 2 Seconds Memory Limit: 65536 KB The bomb is about to explode! Plea ...
- angular js 自定义添加依赖
代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- 旋转数组 [ LeetCode ]
原题地址:https://leetcode-cn.com/problems/rotate-array/description/ 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. ...
- Spring学习--切面优先级及重用切点表达式
指定切面的优先级: 在同一个链接点上应用不止一个切面时 , 除非明确指定 , 否则它们的优先级是不确定的. 切面的优先级可以通过实现 Ordered 接口或利用 @Order 注解指定. 实现 Ord ...
- 网络编程:connect函数
TCP客户用connect函数来建立与TCP服务器的连接: cpp #include<sys/socket.h> int connect(int sockfd, const struct ...
- 汕头市队赛 SRM 06 C 秀恩爱
C 秀恩爱 SRM 06 背景&&描述 KPM坐在直升机上俯瞰小渔村景象. 渔村可看作二维平面,密密麻麻地到处都是单身狗,KPM当前所在坐标为(sx,s ...
- 【EOJ3652】乘法还原(二分图)
题意: 思路:Orz Claris 先找出所有平方项,将与有平方项的数有关的数对暂时忽略,剩下的直接连边就是一张二分图,暴力DFS染色 将有平方项的数两边都加一个,再判字典序即可 我不会判字典序……耽 ...
- [bzoj3226][Sdoi2008]校门外的区间——线段树
题目 略 题解 直接套黄学长模板. Orz 代码 #include <bits/stdc++.h> using namespace std; #define ll long long #d ...
- bzoj 2662&bzoj 2763 SPFA变形
我们用dis[i,j]代表到i这个点,用j张票的最短路程,那么我们只需要在SPFA更新 的时候,用dis[i,j]更新dis[p,j]和dis[p,j+1]就行了 /***************** ...