BZOJ 1103 DFS序+线段树
来源:互联网 发布:李小璐淘宝店关了吗 编辑:程序博客网 时间:2024/05/20 03:08
思路:
先搞出来DFS序
进入这个点 +1
出这个点 -1
线段树维护前缀和 (因为还要修改) 搞定
修改的时候只修改底下节点就OK了 (边权–>点权 不多说)
//By SiriusRen#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define N 555000char ch[55];int n,m,nn,sum,v[N],next[N],first[N],tree[N*4],tot,cnt,in[N],out[N],xx,yy;void add(int x,int y){ v[tot]=y,next[tot]=first[x],first[x]=tot++;}void insert(int l,int r,int pos,int x,int wei){ if(l==r){tree[pos]+=wei;return;} int mid=(l+r)>>1,lson=pos<<1,rson=pos<<1|1; if(mid<x)insert(mid+1,r,rson,x,wei); else insert(l,mid,lson,x,wei); tree[pos]=tree[lson]+tree[rson];}void dfs(int x,int fa){ in[x]=++cnt,insert(1,nn,1,cnt,1); for(int i=first[x];~i;i=next[i]) if(v[i]!=fa)dfs(v[i],x); out[x]=++cnt,insert(1,nn,1,cnt,-1);}int query(int l,int r,int pos){ if(r<=yy)return tree[pos]; int mid=(l+r)>>1,lson=pos<<1,rson=pos<<1|1; if(mid>=yy)return query(l,mid,lson); else return query(l,mid,lson)+query(mid+1,r,rson);}int main(){ memset(first,-1,sizeof(first)); scanf("%d",&n),nn=2*n; for(int i=1;i<n;i++){ scanf("%d%d",&xx,&yy); add(xx,yy),add(yy,xx); } dfs(1,-1); scanf("%d",&m); for(int i=1;i<n+m;i++){ scanf("%s%d",ch,&xx); if(ch[0]=='W')yy=in[xx],printf("%d\n",query(1,nn,1)-1); else{ scanf("%d",&yy); if(in[xx]>in[yy])swap(xx,yy); insert(1,nn,1,in[yy],-1),insert(1,nn,1,out[yy],1); } }}
0 0
- BZOJ 1103 DFS序+线段树
- bzoj 3306: 树 dfs序+线段树
- BZOJ 4034 线段树+DFS序
- BZOJ 2819 DFS序+线段树
- BZOJ 3252攻略 dfs序+线段树
- DFS序+线段树(bzoj 4034)
- bzoj 4551(DFS序+线段树)
- 【BZOJ 3306】树【LCA、DFS序、线段树】
- BZOJ 4551 树 dfs序+线段树 / 并查集
- 【BZOJ】【P3252】【攻略】【题解】【贪心+dfs序+线段树】
- BZOJ 4034 HAOI2015 T2 DFS序+线段树
- BZOJ 3779 重组病毒 LCT+线段树维护DFS序
- [LCT 线段树 dfs序] BZOJ 3779 重组病毒
- bzoj 3252: 攻略 (线段树+DFS序)
- bzoj 4034: [HAOI2015]树上操作(线段树+dfs序)
- bzoj 4530(DFS序+线段树合并)
- (线段树+dfs序)
- DFS序+线段树
- 关于圆(1)— 基础饼图
- 微机原理--第三章(5)逻辑运算指令
- Eclipse之printf
- 关于APP清理缓存
- 服务器虚拟化技术
- BZOJ 1103 DFS序+线段树
- IOS百度地图点击标注didSelectAnnotationView不响应
- 理解LSTM网络
- 欢迎使用CSDN-markdown编辑器
- mysql事务
- nyoj63 二叉树
- 手机销售系统(Java源码)
- 兰州之旅!!
- B树、B-树、B+树、B*树