一个叫 Euler-Tour-Tree 的数据结构,说白了就是用 Splay_Tree 维护欧拉序

#include <cstring>
#include <algorithm>
#include <string>
#include <cstdio>using namespace std;void setIO(string a){ freopen((a+".in").c_str(),"r",stdin);freopen((a+".out").c_str(),"w",stdout);
}#define maxn 300000
#define ll long longint euler[maxn], w[maxn], cnt=1;
int head[maxn],to[maxn],nex[maxn],edges,root;
ll sumv[maxn], val[maxn], lazy[maxn];
void addedge(int u,int v){nex[++edges]=head[u],head[u]=edges,to[edges]=v;
}void dfs(int u){euler[++cnt]=u*2, val[u*2]=w[u];for(int v=head[u];v;v=nex[v])dfs(to[v]);euler[++cnt]=u*2+1, val[u*2+1]=-w[u];
}struct Splay_Tree{int f[maxn],ch[maxn][2],sta[maxn],siz[maxn];int rson(int x){return ch[x][1];}int lson(int x){return ch[x][0];}int get(int x){return ch[f[x]][1]==x;}void update(int x,int c){val[x]+=(x%2==0?1:-1)*c;lazy[x]+=c;sumv[x]+=siz[x]*c;}void pushup(int x){ sumv[x]=sumv[lson(x)]+sumv[rson(x)]+val[x];siz[x]=siz[lson(x)]+siz[rson(x)];siz[x]+=(x>=200099 ? 0: (x%2==0?1:-1));}void pushdown(int x){if(lazy[x]) update(lson(x),lazy[x]), update(rson(x),lazy[x]),lazy[x]=0;}   int pre(int x){splay(x,root);x=lson(root);while(rson(x)) x=rson(x);return x;}int las(int x){splay(x,root);x=rson(root);while(lson(x)) x=lson(x);return x;}void build(int l,int r,int &o,int fa){if(l>r)return;int mid=(l+r)>>1;o=euler[mid], f[o]=fa;build(l,mid-1,ch[o][0],o);build(mid+1,r,ch[o][1],o);pushup(o);}void rotate(int x){int old=f[x], oldf=f[old], which=get(x);ch[old][which]=ch[x][which^1], f[ch[old][which]]=old;ch[x][which^1]=old,f[old]=x,f[x]=oldf;if(oldf) ch[oldf][ch[oldf][1]==old]=x;pushup(old),pushup(x);}void splay(int x,int &tar){int v=0,u=x,a=f[tar];while(u!=a) sta[++v]=u,u=f[u];while(v) pushdown(sta[v--]);for(int fa;(fa=f[x])!=a;rotate(x))if(f[fa]!=a) rotate(get(fa)==get(x)?fa:x);tar=x;}void opt1(int a){int x=las(a*2);splay(200100,root),splay(x,ch[root][1]);printf("%lld\n",sumv[ch[ch[root][1]][0]]);}void opt2(int a,int b){int x=pre(a*2),y=las(a*2+1),tmp;splay(x,root),splay(y,ch[root][1]);tmp=ch[ch[root][1]][0], ch[ch[root][1]][0]=f[tmp]=0;pushup(ch[root][1]), pushup(root);x=las(b*2);splay(b*2,root), splay(x,ch[root][1]);ch[ch[root][1]][0]=tmp,f[tmp]=ch[root][1];pushup(ch[root][1]),pushup(root);}void opt3(int a,int b){int x=pre(a*2),y=las(a*2+1);splay(x,root),splay(y,ch[root][1]);update(ch[ch[root][1]][0],b);pushup(ch[root][1]),pushup(root);}}tree;int main(){//setIO("input");int n,x,m;scanf("%d",&n);for(int i=2;i<=n;++i)scanf("%d",&x),addedge(x,i);for(int i=1;i<=n;++i) scanf("%d",&w[i]);dfs(1);euler[1]=200100, euler[++cnt]=200101;tree.build(1,cnt,root,0);scanf("%d",&m);while(m--){char opt[10];int a,b;scanf("%s",opt);if(opt[0]=='Q'){scanf("%d",&a);tree.opt1(a);}if(opt[0]=='C'){scanf("%d%d",&a,&b);tree.opt2(a,b);}if(opt[0]=='F'){scanf("%d%d",&a,&b);tree.opt3(a,b);}}return 0;
}

  

转载于:https://www.cnblogs.com/guangheli/p/10011464.html

BZOJ 3786: 星系探索 欧拉游览树相关推荐

  1. BZOJ#3786. 星系探索(平衡树,fhq-treap,弱化版ETT)

    BZOJ#3786. 星系探索 Solution 子树加,换fatherfatherfather(保证还是树),询问到根路径和. 树上路径求和不好动态维护,于是转化到序列上,维护一个括号序,dfndf ...

  2. [BZOJ 3786] 星系探索

    BZOJ传送门 题目描述 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有 n n n个星球,其中有一个主星球(方便起见我们默认其为 1 1 1号星球),其余的所有星球均有且仅 ...

  3. 数学小白的探索-欧拉和费马

    文章目录 一些数学趣史 关于欧拉猜想以及欧拉公式 费马大定理特殊情况最简单证明 题外话-三千年一遇的一元二次方程解法 写在最后 一些数学趣史 很久没有发表文章了,也许是最近闲的让一向数学其差的我没事看 ...

  4. BZOJ 2818 Gcd 线性欧拉筛(Eratosthenes银幕)

    标题效果:定整N(N <= 1e7),乞讨1<=x,y<=N和Gcd(x,y)素数的数(x,y)有多少.. 思考:推,. 建立gcd(x,y) = p,然后,x / p与y / p互 ...

  5. BZOJ 2190: [SDOI2008]仪仗队( 欧拉函数 )

    假设C君为(0, 0), 则右上方为(n - 1, n - 1). 一个点(x, y) 能被看到的前提是gcd(x, y) = 1, 所以 answer = ∑ phi(i) * 2 + 2 - 1 ...

  6. Bzoj 2749: [HAOI2012]外星人 欧拉函数,数论,线性筛

    2749: [HAOI2012]外星人 Time Limit: 3 Sec  Memory Limit: 128 MB Submit: 568  Solved: 302 [Submit][Status ...

  7. bzoj3786星系探索 splay

    3786: 星系探索 Time Limit: 40 Sec  Memory Limit: 256 MB Submit: 1314  Solved: 425 [Submit][Status][Discu ...

  8. [BZOJ 4034][HAOI2015]树上操作(欧拉序列+线段树)

    Description 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子树中 ...

  9. bzoj 5245: [Fjwc2018]欧拉函数 线段树+bitset

    题意 对于正整数 n,定义欧拉函数 φ(n) 为小于等于 n 且与 n 互质的正整数个数.例如 φ(1) = 1, φ(8) = 4. 给定正整数序列 a1, a2, · · · , an,请依次执行 ...

最新文章

  1. YESLAB的数据中心课程介绍
  2. php跳出volist,thinkphpvolist
  3. mysql三范式_MySQL设计之三范式的理解
  4. mxnet基础到提高(24)-C++-符号,变量读写
  5. 停止Java线程,小心interrupt()方法
  6. vue3 数据双向绑定demo
  7. arduino i2c 如何写16位寄存器_Arduino之我见
  8. web高德地图怎么加载离线地图_怎么验证全国离线卫星地图缓存文件的完整性
  9. 34. 在排序数组中查找元素的第一个和最后一个位置 golang
  10. 在集设|参透海报设计中提取排版设计灵感
  11. ssh框架从页面传中文发生乱码时怎么解决,就是添加一个字符编码拦截器。用springframework自带的便可...
  12. 1041. 考试座位号(15)-PAT乙级真题
  13. CentOS 7 修改IP地址
  14. [转]Ubuntu下快速安装python
  15. Udp通信 暑期学习笔记(三)
  16. 知识图谱、Gremlin Traversal Language、RDF、Amazon Nepture 图数据库介绍
  17. Navitas GaN IC 驱动 Vivo 可折叠手机
  18. iPhone开发--在iPad上调用UIAlertController函数时发生crash
  19. MP4Box常用命令
  20. allegro 过孔删除不掉

热门文章

  1. python django admin.site.register注册应用
  2. 【收藏】使用springboot构建rest api远程提交spark任务
  3. Hadoop Yarn配置参数整理(非常全面)
  4. 关于linux分区与挂载的解释
  5. ReentrantLock可以是公平锁,sync只能是非公平锁。
  6. Scala赋值运算符分类
  7. 【软考-软件设计师】CPU的功能
  8. Java基础--String类底层实现原理及API示例详解
  9. 【webservice】spring整合webservice RS风格
  10. 功利性学习:别陷入勤奋陷阱