类似维护括号序列,给每个点建两个点,然后所有操作都能轻松支持了。注意sum和lastans是long long。

 #include<cstdio>
#include<algorithm>
#define rep(i,l,r) for (int i=(l); i<=(r); i++)
#define For(i,x) for (int i=h[x],k; i; i=nxt[i])
typedef long long ll;
using namespace std; const int N=;
ll ans,sm[N];
int n,nd,tim,rt,op,x,y,m,cnt,a[N],tag[N];
int v[N],f[N],L[N],R[N],sz[N],c[N][],h[N],to[N],nxt[N]; void add(int u,int v){ to[++cnt]=v; nxt[cnt]=h[u]; h[u]=cnt; } void dfs(int x,int fa){
L[x]=++tim; v[tim]=a[x];
For(i,x) if ((k=to[i])!=fa) dfs(k,x);
R[x]=++tim; v[tim]=a[x];
} void upd(int x){
sm[x]=sm[c[x][]]+sm[c[x][]]+v[x];
sz[x]=sz[c[x][]]+sz[c[x][]]+;
} void put(int x,int k){ v[x]+=k; sm[x]+=1ll*k*sz[x]; tag[x]+=k; } void push(int x){
if (!tag[x]) return;
if (c[x][]) put(c[x][],tag[x]);
if (c[x][]) put(c[x][],tag[x]);
tag[x]=;
} void pd(int x){ if (f[x]) pd(f[x]); push(x); } int build(int l,int r){
int mid=(l+r)>>,x=mid;
if (l<mid) c[x][]=build(l,mid-),f[c[x][]]=x;
if (mid<r) c[x][]=build(mid+,r),f[c[x][]]=x;
upd(x); return x;
} void rot(int &rt,int x){
int y=f[x],z=f[y],w=c[y][]==x;
if (y==rt) rt=x; else c[z][c[z][]==y]=x;
f[x]=z; f[y]=x; f[c[x][w^]]=y; c[y][w]=c[x][w^];
c[x][w^]=y; upd(y);
} void splay(int &rt,int x){
for (pd(x); x!=rt; rot(rt,x)){
int y=f[x],z=f[y];
if (y!=rt) (c[z][]==y)^(c[y][]==x) ? rot(rt,x) : rot(rt,y);
}
upd(x);
} int pre(int x){ splay(rt,x); for (x=c[x][]; c[x][]; x=c[x][]); return x; }
int lst(int x){ splay(rt,x); for (x=c[x][]; c[x][]; x=c[x][]); return x; } void Ins(int x,int y){
int k=lst(L[x]); splay(rt,L[x]); splay(c[rt][],k);
c[k][]=++nd; v[nd]=y; f[nd]=k; R[++n]=nd;
nd++; c[nd-][]=nd; v[nd]=y; f[nd]=nd-; L[n]=nd;
upd(nd); upd(nd-); upd(k); upd(rt);
} void Add(int x,int y){
int l=pre(L[x]),r=lst(R[x]); splay(rt,l); splay(c[rt][],r);
put(c[r][],y); upd(r); upd(rt);
} void Del(int x){
int l=pre(L[x]),r=lst(R[x]); splay(rt,l); splay(c[rt][],r);
c[r][]=f[c[r][]]=; upd(r); upd(rt);
} ll Que(int x){ int l=pre(L[x]),r=lst(R[x]); splay(rt,l); splay(c[rt][],r); return sm[c[r][]]; } int main(){
freopen("anudtq.in","r",stdin);
freopen("anudtq.out","w",stdout);
scanf("%d",&n);
rep(i,,n) scanf("%d",&a[i]);
rep(i,,n) scanf("%d%d",&x,&y),x++,y++,add(x,y),add(y,x);
tim=; dfs(,); rt=build(,tim+); nd=tim+; scanf("%d",&m);
rep(i,,m){
scanf("%d%d",&op,&x); x+=ans+;
if (op==) scanf("%d",&y),Ins(x,y);
if (op==) scanf("%d",&y),Add(x,y);
if (op==) Del(x);
if (op==) printf("%lld\n",ans=Que(x)/);
}
return ;
}

[CodeChef-ANUDTQ] Dynamic Trees and Queries的更多相关文章

  1. Codechef Dynamic Trees and Queries

    Home » Practice(Hard) » Dynamic Trees and Queries Problem Code: ANUDTQSubmit https://www.codechef.co ...

  2. CodeChef DGCD Dynamic GCD

    CodeChef题面 Time limit 210 ms Code length Limit //内存限制也不说一下,真是的-- 50000 B OS Linux Language limit C, ...

  3. 【CodeChef】Chef and Graph Queries

    Portal --> CC Chef and Graph Queries Solution 快乐数据结构题(然而好像有十分优秀的莫队+可撤销并查集搞法qwq) 首先考虑一种方式来方便一点地..计 ...

  4. [CodeChef - GERALD07 ] Chef and Graph Queries

    Read problems statements in Mandarin Chineseand Russian. Problem Statement Chef has a undirected gra ...

  5. codechef Chef And Easy Xor Queries

    做法:我们考虑前缀异或和,修改操作就变成了区间[i,n]都异或x 查询操作就变成了:区间[1,x]中有几个k 显然的分块,每个块打一个tag标记表示这个块中所有的元素都异或了tag[x] 然后处理出这 ...

  6. Creating dynamic/configurable parameterized queries in Entity Framework

    https://dillieodigital.wordpress.com/2013/05/09/creating-dynamicconfigurable-parameterized-queries-i ...

  7. [bzoj3514][CodeChef GERALD07] Chef ans Graph Queries [LCT+主席树]

    题面 bzoj上的强制在线版本 思路 首先可以确定,这类联通块相关的询问问题,都可以$LCT$+可持久化记录解决 用LCT维护生成树作为算法基础 具体而言,从前往后按照边的编号顺序扫一遍边 如果这条边 ...

  8. codechef FUN WITH TREES

    题目大意: 给一棵树root=1的树: 给一些操作:u  v 的路径所有节点的node + val: 最后m个询问:u 节点(包括u) sum%mod 是多少. LCA + RMQ: 我们每次mark ...

  9. BZOJ3514 / Codechef GERALD07 Chef and Graph Queries LCT、主席树

    传送门--BZOJ 传送门--VJ 考虑使用LCT维护时间最大生成树,那么对于第\(i\)条边,其加入时可能会删去一条边.记\(pre_i\)表示删去的边的编号,如果不存在则\(pre_i = 0\) ...

随机推荐

  1. 使用chrome浏览器调试移动端网页(非模拟访问)

    1. 使用数据线连接手机与电脑 2. 打开手机调试模式 参考:http://jingyan.baidu.com/article/f79b7cb35ada4d9145023e63.html 本人使用的m ...

  2. Android中百分比布局

    百分比布局的出现主要是因为LinearLayout中可以通过android:layout_weight="1"这种方法来支持按比例指定控件大小 但是FrameLayout和Rela ...

  3. PHP 之Mysql优化

    一.建立索引 普通索引 index: 对关键字没有要求. 唯一索引 unique index: 要求关键字不能重复.同时增加唯一约束. 主键索引 primary key: 要求关键字不能重复,也不能为 ...

  4. 记一次edusoho问题

    问题描述:Edusoho如何迁移到本地windows或另外一台Linux服务器上 解决问题步骤: 1.参考官方文档官方文档地址如下:http://www.qiqiuyu.com/my/course/3 ...

  5. 两个对象值相同 (x.equals(y) == true) ,但却可有不同的 hash code ,这 句话对不对

    答:不对,有相同的 hash code这是java语言的定义:1) 对象相等则hashCode一定相等:2) hashCode相等对象未必相等 1.如果是基本变量,没有hashcode和equals方 ...

  6. HBase 介绍

    HBase的列族式存储 列族式存储的概念 HBase Table的组成 Table = RowKey + Family + Column + Timestamp + Value 数据存储模式 (Row ...

  7. layui的layer.open()方法查看缩略图 原图缩放

    写在前面 需求是页面上的图片缩略图, 鼠标悬浮时显示原图片, 并按比例缩放. 操作步骤 官方文档 点击跳转 关键属性 1. type: 设置type=1, 以页面的形式展示图片 2.  content ...

  8. 【转】Python访问oracle数据库,DPI-1047: Cannot locate a 64-bit Oracle Client library: "The specified module could not be found"

    使用python连接Oracle,出现如下错误: DPI-1047: Cannot locate a 64-bit Oracle Client library: "The specified ...

  9. ubuntu 18.04屏幕共享 -------(转载) ( Windows远程登录Ubuntu )

    原文地址: https://my.oschina.net/michaelshu/blog/3018932 ----------------------------------------------- ...

  10. 不规则的Ifc构件顶点提取方法

    BIM模型中有很多不规则的构件,在IFC中这些不规则的构件一般用顶点的形式表示,顶点坐标提取路径:  IfcObject->IfcProductDefinitionShape->IfcSh ...