类似维护括号序列,给每个点建两个点,然后所有操作都能轻松支持了。注意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. 分类模型的性能评价指标(Classification Model Performance Evaluation Metric)

    二分类模型的预测结果分为四种情况(正类为1,反类为0): TP(True Positive):预测为正类,且预测正确(真实为1,预测也为1) FP(False Positive):预测为正类,但预测错 ...

  2. [bzoj1001]狼抓兔子 最小割

    题意概述:给出一张无向图,每条边有一个权值,割掉这条边代价为它的权值,求使起点不能到达终点的最小代价. 显然能看出这是个最小割嘛,然后最小割=最大流,建图的时候特殊处理一下再跑个最大流就好了. #in ...

  3. 深度clone和浅clone

    首先等你有空的时候可以看这篇,要用可以看我这篇 https://www.cnblogs.com/echolun/p/7889848.html 1.有一点需要明确的是 深拷贝只是针对引用数据类型来说的, ...

  4. Jmeter(四十一)_图片爬虫

    今天教大家用元件组合,做一个网页图片爬虫. 需要用到的元件:循环控制器+计数器+xpath提前器+函数嵌套+beanshell代码 首先我们确定一下要爬取的图片网站:https://dp.pconli ...

  5. Java_jdbc 基础笔记之十五 数据库连接(取得数据库自动生成的主键)

    public class testGetKeyValue { /** * 取得数据库自动生成的主键 */ @Test public void testGeneratedKeys() { Connect ...

  6. gogs 邀请协作者 500错误

    触发原因: 对db文件的user表删了某个用户导致 解决: 注册个新用户,把id改成原来的id(默认都会自增长)

  7. 数据分析之--log文件自动化分析

    https://mp.weixin.qq.com/s?__biz=MjM5NjE2MTIyMw==&mid=2257483803&idx=1&sn=efe24b040397cd ...

  8. Java多线程的Semaphore

    Semaphore 信号量, 在多线程应用中, 用来控制同时访问某个特定资源的操作数量, 或者同时执行某个指定操作的数量, 还可以用来实现某种资源池限制, 或者对容器施加边界. 简单地说, Semap ...

  9. PostgreSQL中的partition-wise aggregation

    partition-wise aggregation允许对每个分区分别执行的分区表进行分组或聚合.如果GROUP BY子句不包括分区键,则只能在每个分区的基础上执行部分聚合,并且必须稍后执行最终处理. ...

  10. Freemarke

    本文介绍了freemarker的集成.FTL指令.内建函数.运算符等常用操作. 1.为什么要使用网页静态化技术 网页静态化解决方案在实际开发中运用比较多,例如新闻网站,门户网站中的新闻频道或者是文章类 ...