BZOJ 3221: [Codechef FEB13] Obserbing the tree树上询问 树链剖分 主席树
来源:互联网 发布:淘宝里怎么分享 编辑:程序博客网 时间:2024/06/06 02:58
3221: [Codechef FEB13] Obserbing the tree树上询问
Time Limit: 20 Sec Memory Limit: 1280 MBSubmit: 439 Solved: 105
[Submit][Status][Discuss]
Description
小N最近在做关于树的题。今天她想了这样一道题,给定一棵N个节点的树,节点按1~N编号,一开始每个节点上的权值都是0,接下来有M个操作。第一种操作是修改,给出4个整数X,Y,A,B,对于X到Y路径上加上一个首项是A,公差是B的等差数列,因为小N十分谨慎,所以她每做完一个修改操作就会保存一次,初始状态是第0次保存的局面。第二种操作是求和,给出2个整数X,Y,输出X到Y路径上所有节点的权值和。第三种操作是读取之前第X次保存的局面,所有节点的状态回到之前第X次保存的状态。现在请你对每一个求和操作输出答案。
Input
第一行2个整数N,M表示节点个数和操作次数。
接下来N-1行每行2个整数Ui,Vi表示了这棵树中Ui和Vi这2个节点间有边相连。
接下来M行每行先有一个字符表示了操作的类型:
如果是’c’,那么代表了一个修改操作,接下来有4个整数X1,Y1,A,B,为了使得询问在线,正确的X=X1 xor上次输出的数,Y=Y1 xor上次输出的数,如果之前没有输出过那么当成0。
如果是’q’,那么代表了一个求和操作,接下来有2个整数X1,Y1,和修改操作一样需要xor上次输出。
如果是’l’,那么代表了一次读取操作,接下来1个整数X1,正确的X=X1 xor上次输出的数。
Output
对于每一个求和操作,输出求和后的值。
Sample Input
5 7
1 2
2 3
3 4
4 5
c 2 5 2 3
c 3 4 5 10
q 1 3
l 13
q 13 15
l 6
q 6 4
1 2
2 3
3 4
4 5
c 2 5 2 3
c 3 4 5 10
q 1 3
l 13
q 13 15
l 6
q 6 4
Sample Output
12
7
7
7
HINT
100%的数据中N,M<=100000,0<=A,B<=1000,0<=X1,Y1<=10^1,修改次数<M/2,不会读取没保存的局面
Source
持久化线段树
这个东西解法挺明显的
要求可持久化 直接想到线段树,在树上,就链剖呗。。。
之前没想过区间加等差数列。由于怠惰直接问了 neither_nor 挺水的 。。。 看代码。。。
有几点注意一下:
在线异或 a,b 不用(我只用了两个小时就调出来了=_=)
要标记永久化 本来觉得不用,应该挺行的,请教neither_nor 之后明白这是为了防止爆运行内存
#include<cmath>#include<ctime>#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>#include<iomanip>#include<vector>#include<string>#include<bitset>#include<queue>#include<set>#include<map>using namespace std;typedef long long ll;inline ll read(){ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch<='9'&&ch>='0'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}return x*f;}void print(ll x){if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}const int N=100100;int ecnt,last[N];struct EDGE{int to,nt;}e[N<<1];inline void add(int u,int v){e[++ecnt]=(EDGE){v,last[u]};last[u]=ecnt;}int n;int size[N],son[N],d[N],fa[N];void dfs1(int u){size[u]=1;for(int i=last[u];i;i=e[i].nt)if(fa[u]^e[i].to){d[e[i].to]=d[u]+1;fa[e[i].to]=u;dfs1(e[i].to);size[u]+=size[e[i].to];if(size[son[u]]<size[e[i].to])son[u]=e[i].to;}}int top[N],dfn[N],tim;void dfs2(int u,int tp){top[u]=tp;dfn[u]=++tim;if(son[u])dfs2(son[u],tp);for(int i=last[u];i;i=e[i].nt)if(e[i].to^son[u]&&e[i].to^fa[u])dfs2(e[i].to,e[i].to);}inline int getlca(int u,int v){while(top[u]^top[v]){if(d[top[u]]<d[top[v]])swap(u,v);u=fa[top[u]];}return d[u]<d[v]?u:v;}struct president_tree{int ls,rs;ll sum,a,b;}tr[N<<8];int tot,now,sz,root[N];void modify(int &k,int pre,int l,int r,int x,int y,ll a,ll b){k=++sz;tr[k].sum=tr[pre].sum;tr[k].a=tr[pre].a;tr[k].b=tr[pre].b;tr[k].ls=tr[pre].ls;tr[k].rs=tr[pre].rs;if(l==x&&r==y){tr[k].a+=a;tr[k].b+=b;return ;}tr[k].sum+=1ll*(a*2+b*(y-x))*(y-x+1)>>1;int mid=(l+r)>>1;if(y<=mid)modify(tr[k].ls,tr[pre].ls,l,mid,x,y,a,b);else if(x>mid)modify(tr[k].rs,tr[pre].rs,mid+1,r,x,y,a,b);else modify(tr[k].ls,tr[pre].ls,l,mid,x,mid,a,b),modify(tr[k].rs,tr[pre].rs,mid+1,r,mid+1,y,a+(mid+1-x)*b,b);}ll query(int k,int l,int r,int x,int y){ll res=1ll*(tr[k].a*2+(x+y-l*2)*tr[k].b)*(y-x+1)>>1;if(l==x&&r==y){return res+tr[k].sum;}int mid=(l+r)>>1;if(y<=mid)return res+query(tr[k].ls,l,mid,x,y);if(x>mid)return res+query(tr[k].rs,mid+1,r,x,y);return res+query(tr[k].ls,l,mid,x,mid)+query(tr[k].rs,mid+1,r,mid+1,y);}void modify_path(int u,int v,ll a,ll b){int lca=getlca(u,v),tmp1=0,tmp2=d[u]+d[v]-(d[lca]<<1)+2;while(top[u]^top[lca]){tmp1+=d[u]-d[top[u]]+1;modify(root[now],root[now],1,n,dfn[top[u]],dfn[u],a+(tmp1-1)*b,-b);u=fa[top[u]];}while(top[v]^top[lca]){tmp2-=d[v]-d[top[v]]+1;modify(root[now],root[now],1,n,dfn[top[v]],dfn[v],a+(tmp2-1)*b,b);v=fa[top[v]];}tmp1++;tmp2--;d[u]<d[v]?modify(root[now],root[now],1,n,dfn[u],dfn[v],a+(tmp1-1)*b,b):modify(root[now],root[now],1,n,dfn[v],dfn[u],a+(tmp2-1)*b,-b);}ll getans(int u,int v){ll res=0;while(top[u]^top[v]){if(d[top[u]]<d[top[v]])swap(u,v);res+=query(root[now],1,n,dfn[top[u]],dfn[u]);u=fa[top[u]];}if(dfn[u]>dfn[v])swap(u,v);res+=query(root[now],1,n,dfn[u],dfn[v]);return res;}int main(){n=read();int Q=read();register int i;ll u,v,a,b;for(i=1;i<n;++i){u=read();v=read();add(u,v);add(v,u);}dfs1(1);dfs2(1,1);ll ans=0;char opt[2];while(Q--){scanf("%s",opt);switch(opt[0]){case 'c':u=read()^ans;v=read()^ans;a=read();b=read();root[++tot]=root[now];now=tot;modify_path(u,v,a,b);break;case 'q':u=read()^ans;v=read()^ans;print(ans=getans(u,v));puts("");break;case 'l':a=read()^ans;now=a;break;}}return 0;}/*5 71 22 33 44 5c 2 5 2 3c 3 4 5 10q 1 3l 13q 13 15l 6q 6 41277*/
阅读全文
0 0
- BZOJ 3221 [Codechef FEB13] Obserbing the tree树上询问 树链剖分 主席树维护区间加等差数列
- BZOJ 3221: [Codechef FEB13] Obserbing the tree树上询问 树链剖分 主席树
- bzoj 3221: [Codechef FEB13] Obserbing the tree树上询问 (可持久化线段树+树链剖分)
- BZOJ3221 [Codechef FEB13] Obserbing the tree树上询问
- Codechef Aug2017 #Walks on the binary tree -- 主席树+Hash
- 【BZOJ 2588】 Spoj 10628. Count on a tree|树上K大|树链剖分|主席树
- bzoj 2588 树上主席树
- 【BZOJ 1316】 树上的询问 树分治
- 【 bzoj 4299 】 Codechef FRBSUM - 主席树乱搞
- 【BZOJ 4299】 Codechef FRBSUM 主席树
- BZOJ BZOJ 2588: Spoj 10628. Count on a tree 树上主席树
- BZOJ 2588 & SPOJ 10628:树上主席树
- bzoj 2588: Spoj 10628. Count on a tree(树上主席树)
- 【BZOJ 2588】Count on a tree 【树上路径第K大】【LCA+主席树】
- 【BZOJ 2588】Spoj 10628. Count on a tree 主席树+树上差分
- BZOJ 2588 Count on a tree(树上的主席树)
- [主席树维护HASH && SET维护DFS序] Codechef. Walks on the binary tree
- BZOJ 3514 Codechef MARCH14 GERALD07加强版 LCT+主席树
- PostgreSQL用户、数据库及表的管理、操作与授权
- 引导滤波_1
- MySQL 5.6 --> 5.7 的升级
- win10里的控制面板里没有Realtek,前置耳机孔无法使用
- break语句和continue语句的区别
- BZOJ 3221: [Codechef FEB13] Obserbing the tree树上询问 树链剖分 主席树
- linux下R安装RMySQL不成功 configuration failed for package ‘RMySQL’
- 使用扩展二叉树的方法建立二叉树
- Mybatis学习与使用
- CV_EVENT_FLAG_LBUTTON 中flags代表拖拽事件
- java网络编程--IP与InetAddress
- SQL获取SQL Server 根据表名获取表的所有列及属性
- 复制当前日期前一天的所有文件源码
- ubantu(linux)构建开发环境(JDK/intellij idea/maven)